1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 600006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) #define P 998244353 typedef long long ll; int n , s; int A[MAXN];
int Pow( int x , int a ) { int ret = 1; while( a ) { if( a & 1 ) ret = 1ll * ret * x % P; x = 1ll * x * x % P , a >>= 1; } return ret; }
int Wn[2][MAXN]; void getwn( int len ) { for( int mid = 1 ; mid < len ; mid <<= 1 ) { int w0 = Pow( 3 , ( P - 1 ) / ( mid << 1 ) ) , w1 = Pow( 3 , P - 1 - ( P - 1 ) / ( mid << 1 ) ); Wn[0][mid] = Wn[1][mid] = 1; rep( i , 1 , mid - 1 ) Wn[0][mid + i] = 1ll * Wn[0][mid + i - 1] * w0 % P, Wn[1][mid + i] = 1ll * Wn[1][mid + i - 1] * w1 % P; } } int rev[MAXN]; void getr( int len ) { int t = __builtin_ctz( len ); for( int i = 1 ; i < len ; ++ i ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << t - 1 ); } void NTT( vi& A , int len , int f ) { for( int i = 0 ; i < len ; ++ i ) if( i < rev[i] ) A[i] ^= A[rev[i]] , A[rev[i]] ^= A[i] , A[i] ^= A[rev[i]]; for( int mid = 1 ; mid < len ; mid <<= 1 ) for( int i = 0 ; i < len ; i += ( mid << 1 ) ) for( int k = 0 ; k < mid ; ++ k ) { int t1 = A[i + k] , t2 = 1ll * A[i + k + mid] * Wn[f][mid + k] % P; A[i + k] = ( t1 + t2 ) % P , A[i + k + mid] = ( t1 + P - t2 ) % P; } if( f ) for( int i = 0 , iv = Pow( len , P - 2 ) ; i < len ; ++ i ) A[i] = 1ll * A[i] * iv % P; }
int J[MAXN] , iJ[MAXN];
int C( int a , int b ) { if( a < b || a < 0 || b < 0 ) return 0; return J[a] * 1ll * iJ[b] % P * iJ[a - b] % P; }
vi& mul( vi& a , vi& b , int fuck = 0 ) { int len = 1 , sz = ( a.size() + b.size() - 2 ); while( len <= sz ) len <<= 1; getr( len ) , getwn( len ); a.resize( len ) , b.resize( len ); NTT( a , len , 0 ) , NTT( b , len , 0 ); rep( i , 0 , len - 1 ) a[i] = 1ll * a[i] * b[i] % P; NTT( a , len , 1 ); if( fuck ) NTT( b , len , 1 ); a.resize( sz + 1 ); return a; }
int flg; vi tmp; void getit( int m , vi& fun ) { tmp.clear(); tmp.resize( m + 1 ) , fun.resize( m + 1 ); rep( i , 1 , m ) { tmp[i] = C( m + i - 1 , 2 * i - 1 ) * 1ll * ( ( i & 1 ) ? P - 1 : 1 ) % P * J[i - 1] % P; if( flg ) tmp[i] = 1ll * tmp[i] * Pow( i , P - 2 ) % P; fun[m - i] = iJ[i]; } fun[m] = iJ[0]; mul( fun , tmp ); fun[0] = 0; rep( i , 1 , m ) { fun[i] = fun[i + m] * 1ll * iJ[i - 1] % P * ((i & 1) ? P - 1 : 1) % P * iJ[i] % P; } fun.resize( m + 1 ); }
vi fs[MAXN];
vi& solve( int l , int r ) { if( l == r ) return fs[l]; int mid = l + r >> 1; return mul( solve( l , mid ) , solve( mid + 1 , r ) ); }
void solve() { J[0] = iJ[0] = 1; rep( i , 1 , MAXN - 1 ) J[i] = J[i - 1] * 1ll * i % P , iJ[i] = Pow( J[i] , P - 2 ); cin >> n; flg = 1; rep( i , 1 , n ) scanf("%d",A + i) , s += A[i] , getit( A[i] , fs[i] ) , flg = 0;
vi& re = solve( 2 , n ); vi t( A[1] , 0 ); rep( i , 0 , A[1] - 1 ) t[i] = ( fs[1][i + 1] * 1ll * ( i + 1 ) ) % P; mul( t , re , 1 ); int ans = 0; rep( i , 0 , t.size() - 1 ) ans = ( ans + t[i] * 1ll * J[i] % P ) % P; if( A[1] == 1 ) return void( cout << ans * 1ll * s % P << endl );
t.clear(); t.resize( A[1] - 1 ); rep( i , 0 , A[1] - 2 ) t[i] = ( fs[1][i + 2] * 1ll * ( i + 2 ) % P * ( i + 1 ) % P ) % P; mul( t , re ); rep( i , 0 , t.size() - 1 ) ans = ( ans + P - t[i] * 1ll * J[i] % P ) % P; cout << 1ll * ans * s % P << endl; }
signed main() {
solve(); }
|