#line 1 "NumberTheoreticalTransform.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1307" #include #include #include #include using namespace std; #line 1 "/home/tfuruta/repos/compro-library/lib/util/ModInt.cpp" /* * @title ModInt * @docs md/util/ModInt.md */ template class ModInt { public: long long x; constexpr ModInt():x(0) {} constexpr ModInt(long long y) : x(y>=0?(y%mod): (mod - (-y)%mod)%mod) {} ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator+=(const long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator+=(const int y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;} ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;} ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;} ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;} ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;} ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;} ModInt operator-() const {return ModInt(-x); } ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;} ModInt operator--() {x--;if(x<0) x+=mod;return *this;} ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } ModInt operator^(const ModInt &p) const { return ModInt(*this) ^= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inv() const {int a=x,b=mod,u=1,v=0,t;while(b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);} return ModInt(u);} ModInt pow(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;} friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;} friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt(t);return (is);} }; //using modint = ModInt; #line 1 "/home/tfuruta/repos/compro-library/lib/math/NumberTheoreticalTransform.cpp" /* * @title NumberTheoreticalTransform - 数論変換 * @docs md/math/NumberTheoreticalTransform.md */ template class NumberTheoreticalTransform { inline static constexpr int prime1 =1004535809; inline static constexpr int prime2 =998244353; inline static constexpr int prime3 =985661441; inline static constexpr int inv21 =332747959; // ModInt(mod1).inv().x; inline static constexpr int inv31 =766625513; // ModInt(mod1).inv().x; inline static constexpr int inv32 =657107549; // ModInt(mod2).inv().x; inline static constexpr long long prime12=(1002772198720536577LL); inline static constexpr int log2n_max = 21; static unordered_map> base_map; using Mint = T; using Mint1 = ModInt; using Mint2 = ModInt; using Mint3 = ModInt; inline static Mint garner(const Mint1& b1,const Mint2& b2,const Mint3& b3) {Mint2 t2 = (b2-b1.x)*inv21;Mint3 t3 = ((b3-b1.x)*inv31-t2.x)*inv32;return Mint(Mint(prime12)*t3.x+b1.x+prime1*t2.x);} template inline static array,log2n_max> get_base(int inv=0) { array,log2n_max> base, es, ies; ModInt e = ModInt(3).pow((prime - 1) >> log2n_max), ie = e.inv(); for (int i = log2n_max; i >= 2; --i) { es[i - 2] = e, ies[i - 2] = ie; e *= e, ie *= ie; } ModInt acc = 1; if(!inv) for (int i = 0; i < log2n_max - 2; ++i) { base[i] = es[i] * acc; acc *= ies[i]; } else for (int i = 0; i < log2n_max - 2; ++i) { base[i] = ies[i] * acc; acc *= es[i]; } return base; } template inline static void butterfly(vector>& a, const array,log2n_max>& base) { int h = __builtin_ctz(a.size()); for (int i = 0; i < h; i++) { int w = 1 << i, p = 1 << (h - (i+1)); ModInt acc = 1; for (int s = 0; s < w; s++) { int offset = s << (h - i); for (int j = 0; j < p; ++j) { auto l = a[j + offset]; auto r = a[j + offset + p] * acc; a[j + offset] = l + r; a[j + offset + p] = l - r; } acc *= base[__builtin_ctz(~(unsigned int)(s))]; } } } template inline static void ibutterfly(vector>& a, const array,log2n_max>& base) { int h = __builtin_ctz(a.size()); for (int i = h-1; 0 <= i; i--) { int w = 1 << i, p = 1 << (h - (i+1)); ModInt acc = 1; for (int s = 0; s < w; s++) { int offset = s << (h - i); for (int j = 0; j < p; ++j) { auto l = a[j + offset]; auto r = a[j + offset + p]; a[j + offset] = l + r; a[j + offset + p] = (l - r) * acc; } acc *= base[__builtin_ctz(~(unsigned int)(s))]; } } } template inline static vector> convolution_friendrymod(vector a,vector b){ int n = a.size(), m = b.size(); if (!n || !m) return {}; if (min(n, m) <= 60) { if (n < m) swap(n, m),swap(a, b); vector> f(n+m-1); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) f[i+j]+=a[i].x*b[j].x; return f; } int N,M=n+m-1; for(N=1;N inverse(N); inverse = inverse.inv(); vector> g(N,0),h(N,0); for(int i=0;i,log2n_max> base; if(!base.front().x) base = get_base(); static array,log2n_max> ibase; if(!ibase.front().x) ibase = get_base(1); butterfly(g,base); butterfly(h,base); for(int i = 0; i < N; ++i) g[i] *= h[i]; ibutterfly(g,ibase); for (int i = 0; i < n + m - 1; i++) g[i] *= inverse; return g; } inline static vector convolution_arbitrarymod(const vector& g,const vector& h){ auto f1 = convolution_friendrymod(g, h); auto f2 = convolution_friendrymod(g, h); auto f3 = convolution_friendrymod(g, h); vector f(f1.size()); for(int i=0; i convolution(const vector& g,const vector& h) { vector a(g.size()), b(h.size()); transform(g.begin(),g.end(),a.begin(),[&](long long x){return T(x);}); transform(h.begin(),h.end(),b.begin(),[&](long long x){return T(x);}); auto c = convolution_arbitrarymod(a,b); vector f(g.size()+h.size()-1); transform(c.begin(),c.end(),f.begin(),[&](T x){return x.x;}); return f; } inline static vector> convolution(const vector>& g,const vector>& h){return convolution_friendrymod<998244353>(g,h);} inline static vector> convolution(const vector>& g,const vector>& h){return convolution_arbitrarymod(g,h);} // inline vector> convolution(const vector>& g,const vector>& h){return convolution_arbitrarymod(g,h);} }; #line 10 "NumberTheoreticalTransform.test.cpp" constexpr long long mod = 998244353; int main() { cin.tie(0);ios::sync_with_stdio(false); int N,Q; cin >> N >> Q; vector> A(N),B(N,0),D(N,0); for(int i=0;i> A[i]; while(Q--){ int r; cin >> r; B[N-1-r]+=1; } auto C = NumberTheoreticalTransform>::convolution(A,B); for(int i=0;i<2*N-1;++i) { D[(i+1)%N]+=C[i]; } for(int i=0;i