#ifdef NACHIA // #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include #include #include #include #include #include using namespace std; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i<(i64)(n); i++) #define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--) const i64 INF = 1001001001001001001; const char* yn(bool x){ return x ? "Yes" : "No"; } template void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } template using nega_queue = priority_queue,greater>; using Modint = atcoder::static_modint<998244353>; //#include "nachia/vec.hpp" namespace nachia{ template std::vector MultipointEvaluation( const Fps& poly, const std::vector& points ) { if(poly.size() == 0) return std::vector(points.size(), Modint()); if(points.size() == 0) return {}; int N = poly.size(); int M = points.size(); int Z = 1; while(Z < M) Z *= 2; auto One = Modint(1); std::vector st(2*Z, Fps(0)); std::vector stsz(2*Z, 0); for(int i=0; i=1; i--){ if(stsz[i*2+1]){ int n = stsz[i*2] * 2; st[i] = st[i*2].clip().mulEach(st[i*2+1]); st[i] = st[i].nttDouble(); if(stsz[i*2+1] * 2 == n){ for(int j=n; j Fps { return A.clip(0, W.size()).ntt().mulEach(W).intt().clip(wsz - 1, wsz - 1 + sz); }; st[1] = tConb(poly, st[1].intt().reverse(stsz[1]+1).inv(N).reverse() .clip(0,Fps::BestNttSize(N+M)).ntt(), M, N); for(int i=1; i ans(M); for(int i=0; i namespace nachia{ template struct PrimitiveRoot{ using u64 = unsigned long long; static constexpr u64 powm(u64 a, u64 i) { u64 res = 1, aa = a; while(i){ if(i & 1) res = res * aa % MOD; aa = aa * aa % MOD; i /= 2; } return res; } static constexpr bool ExamineVal(unsigned int g){ unsigned int t = MOD - 1; for(u64 d=2; d*d<=t; d++) if(t % d == 0){ if(powm(g, (MOD - 1) / d) == 1) return false; while(t % d == 0) t /= d; } if(t != 1) if(powm(g, (MOD - 1) / t) == 1) return false; return true; } static constexpr unsigned int GetVal(){ for(unsigned int x=2; x class Comb{ private: static constexpr int MOD = Modint::mod(); std::vector F; std::vector iF; public: void extend(int newN){ int prevN = (int)F.size() - 1; if(newN >= MOD) newN = MOD - 1; if(prevN >= newN) return; F.resize(newN+1); iF.resize(newN+1); for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i); iF[newN] = F[newN].inv(); for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i); } Comb(int n = 1){ F.assign(2, Modint(1)); iF.assign(2, Modint(1)); extend(n); } Modint factorial(int n) const { return F[n]; } Modint invFactorial(int n) const { return iF[n]; } Modint invOf(int n) const { return iF[n] * F[n-1]; } Modint comb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[r] * iF[n-r]; } Modint invComb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[r] * F[n-r]; } Modint perm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[n-r]; } Modint invPerm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[n-r]; } Modint operator()(int n, int r) const { return comb(n,r); } }; } // namespace nachia namespace nachia{ int Popcount(unsigned long long c) noexcept { #ifdef __GNUC__ return __builtin_popcountll(c); #else c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3)); c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5)); c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17)); c = (c * (~0ull/257)) >> 56; return c; #endif } // please ensure x != 0 int MsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(x); #else using u64 = unsigned long long; int q = (n >> 32) ? 32 : 0; auto m = n >> q; constexpr u64 hi = 0x8888'8888; constexpr u64 mi = 0x1111'1111; m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35; m = (((m | ~(hi - (n & ~hi))) & hi) * mi) >> 31; q += (m & 0xf) << 2; q += 0x3333'3333'2222'1100 >> (((n >> q) & 0xf) << 2) & 0xf return q; #endif } // please ensure x != 0 int LsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return __builtin_ctzll(x); #else return MsbIndex(x & -x); #endif } } namespace nachia { template struct NttInterface{ template void Butterfly(Iter, int) const {} template void IButterfly(Iter, int) const {} template void BitReversal(Iter a, int N) const { for(int i=0, j=0; j>1; k > (i^=k); k>>=1); } } }; } // namespace nachia #include namespace nachia{ constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } template struct NttFromAcl : NttInterface { using u32 = unsigned int; using u64 = unsigned long long; static int ceil_pow2(int n) { int x = 0; while ((1U << x) < (u32)(n)) x++; return x; } struct fft_info { static constexpr u32 g = nachia::PrimitiveRoot::val; static constexpr int rank2 = bsf_constexpr(mint::mod()-1); std::array root; std::array iroot; std::array rate2; std::array irate2; std::array rate3; std::array irate3; fft_info(){ root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for(int i=rank2-1; i>=0; i--){ root[i] = root[i+1] * root[i+1]; iroot[i] = iroot[i+1] * iroot[i+1]; } mint prod = 1, iprod = 1; for(int i=0; i<=rank2-2; i++){ rate2[i] = root[i+2] * prod; irate2[i] = iroot[i+2] * iprod; prod *= iroot[i+2]; iprod *= root[i+2]; } prod = 1; iprod = 1; for(int i=0; i<=rank2-3; i++){ rate3[i] = root[i+3] * prod; irate3[i] = iroot[i+3] * iprod; prod *= iroot[i+3]; iprod *= root[i+3]; } } }; template void Butterfly(RandomAccessIterator a, int n) const { int h = ceil_pow2(n); static const fft_info info; int len = 0; while(len < h){ if(h-len == 1){ int p = 1 << (h-len-1); mint rot = 1; for(int s=0; s<(1< void IButterfly(RandomAccessIterator a, int n) const { int h = ceil_pow2(n); static const fft_info info; constexpr int MOD = mint::mod(); int len = h; while(len){ if(len == 1){ int p = 1 << (h-len); mint irot = 1; for(int s=0; s<(1<<(len-1)); s++){ int offset = s << (h-len+1); for(int i=0; i> struct FpsNtt { public: using Fps = FpsNtt; using ElemTy = Elem; static constexpr unsigned int MOD = Elem::mod(); static constexpr int CONV_THRES = 30; static const NttInst nttInst; static const unsigned int zeta = nachia::PrimitiveRoot::GetVal(); private: using u32 = unsigned int; static Elem ZeroElem() noexcept { return Elem(0); } static Elem OneElem() noexcept { return Elem(1); } static Comb comb; std::vector a; int RSZ(int& sz) const { return sz = (sz < 0 ? size() : sz); } public: int size() const noexcept { return a.size(); } Elem& operator[](int x) noexcept { return a[x]; } const Elem& operator[](int x) const noexcept { return a[x]; } Elem getCoeff(int x) const noexcept { return (0 <= x && x < size()) ? a[x] : ZeroElem(); } static Comb& GetComb() { return comb; } static int BestNttSize(int x) noexcept { assert(x); return 1 << MsbIndex(x*2-1); } Fps move(){ return std::move(*this); } Fps& set(int i, Elem c){ a[i] = c; return *this; } Fps& removeLeadingZeros(){ int newsz = size(); while(newsz && a[newsz-1].val() == 0) newsz--; a.resize(newsz); if((int)a.capacity() / 4 > newsz) a.shrink_to_fit(); return *this; } FpsNtt(){} FpsNtt(int sz) : a(sz, ZeroElem()) {} FpsNtt(int sz, Elem e) : a(sz, e) {} FpsNtt(std::vector&& src) : a(std::move(src)) {} FpsNtt(const std::vector& src) : a(src) {} Fps& ntt() { capSize(BestNttSize(size())); nttInst.Butterfly(a.begin(), size()); return *this; } Fps& intt() { nttInst.IButterfly(a.begin(), a.size()); return times(Elem::raw(size()).inv()); } Fps nttDouble(Fps vanilla) const { int n = size(); assert(n == (n&-n)); // n is a power of 2 Elem q = Elem::raw(zeta).pow((Elem::mod() - 1) / (n*2)); Elem qq = OneElem(); for(int i=0; i srcR = max(srcL, size()); // if resSz is unspecified -> resSz = destL + srcR - srcL Fps clip(int srcL, int srcR = -1, int destL = 0, int resSz = -1) const { srcR = RSZ(srcR); if(resSz < 0) resSz = destL + srcR - srcL; int rj = std::min(std::min(srcR, size()) - srcL, resSz - destL); Fps res(resSz); for(int j=std::max(0, -srcL); j b.size()) return convolution(b, a, sz); if(sz < 0) sz = std::max(0, a.size() + b.size() - 1); std::vector res(sz); for(int i=0; i=1; i--) a[i] = a[i-1] * comb.invOf(i); return set(0, ZeroElem()); } Fps& EgfToOgf(){ comb.extend(size()); for(int i=0; i= (n-1) / k + 1) return Fps(n); Elem a0 = a[ctz]; return clip(ctz, ctz+n-ctz*k).times(a0.inv()).log().times(Elem(k)).exp().times(a0.pow(k)).clip(0, -1, ctz*k); } auto begin(){ return a.begin(); } auto end(){ return a.end(); } auto begin() const { return a.begin(); } auto end() const { return a.end(); } std::string toString(std::string beg = "[ ", std::string delim = " ", std::string en = " ]") const { std::string res = beg; bool f = false; for(auto x : a){ if(f){ res += delim; } f = true; res += std::to_string(x.val()); } res += en; return res; } std::vector getVectorMoved(){ return std::move(a); } Fps& operator+=(const Fps& r){ capSize(std::max(size(), r.size())); for(int i=0; i=0; i--) res = res * x + a[i]; return res; } }; template Comb FpsNtt::comb; template const NttInst FpsNtt::nttInst; } // namespace nachia namespace nachia{ template class FpsNttSetupManager { using ElemTy = typename Fps::ElemTy; using MyType = FpsNttSetupManager; Fps raw; mutable Fps ntt; static const int THRESH = 30; FpsNttSetupManager(Fps _raw) : raw(_raw.move()) , ntt() {} public: FpsNttSetupManager() : FpsNttSetupManager(Fps()) {} FpsNttSetupManager(Fps _raw, Fps _ntt) : raw(_raw.move()) , ntt(_ntt.move()) {} const Fps& getRaw() const { return raw; } int size() const { return raw.size(); } int Least(){ return Fps::BestNttSize(raw.size()); } static MyType FromRaw(Fps _raw){ return FpsNttSetupManager(_raw.move()); } static MyType FromNtt(Fps _ntt){ Fps x = _ntt.clip(); return MyType(x.intt().removeLeadingZeros().move(), _ntt.move()); } void doubling() const { if(ntt.size() == 0) ntt = raw.clip(0, Fps::BestNttSize(raw.size())).ntt().move(); else ntt = ntt.nttDouble(raw.clip(0, ntt.size())); } Fps& ensureNtt(int sz) const { if(sz / 8 >= ntt.size()) ntt = raw.clip(0, sz).ntt().move(); while(ntt.size() < sz) doubling(); return ntt; } Fps nttClip(int sz) const { return ensureNtt(sz).clip(0,sz); } std::pair destruct(){ return std::make_pair(raw.move(), ntt.move()); } MyType operator+(const MyType& r) const { Fps nntt; int z1 = std::min(ntt.size(), r.ntt.size()); if(z1 >= std::max(size(), r.size())){ nntt.capSize(std::min(ntt.size(), r.ntt.size())); for(int i=0; i Fps ProductOfManyPolynomials(std::vector poly){ using Modint = typename Fps::ElemTy; using Fps2 = FpsNttSetupManager; if(poly.empty()) return std::vector{Modint(1)}; for(auto& p : poly) p.removeLeadingZeros(); for(auto& p : poly) if(p.size() == 0) return Fps(); std::vector poly2; for(auto& p : poly) poly2.push_back(Fps2::FromRaw(p.move())); int OFF_K = 16; for(int K=OFF_K; poly2.size() != 1; K*=2){ size_t pos = poly2.size(); for(size_t i=0; i K){ pos = i; continue; } poly2[pos] = poly2[pos] * poly2[i]; std::swap(poly2[i--], poly2.back()); poly2.pop_back(); } } return poly2[0].destruct().first.move(); } // sum_{a in F} a^k for 0 <= k <= maxIdx template std::vector SumOfPower(std::vector F, int maxIdx){ using Fps = nachia::FpsNtt; std::vector polys(F.size()); for(std::size_t i=0; i(polys).log(maxIdx+1); for(int i=0; i<=maxIdx; i++) a[i] *= -Modint::raw(i); a[0] = F.size(); return a.getVectorMoved(); } } // namespace nachia using Fps = nachia::FpsNtt; void testcase(){ int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; Modint f = 1, ff = 1; for(int i=1; i points(N); rep(i,N) points[i] = A[i] + i; vector polys(N); rep(i,N) polys[i] = Fps(2).set(0,-points[i]).set(1,1).move(); auto multieval = nachia::MultipointEvaluation(nachia::ProductOfManyPolynomials(move(polys)).difference(), points); Modint q = 1; rep(i,N) q *= multieval[i]; if(N%2 == 1 && (N-1)/2%2 == 1) q = -q; q /= ff * ff; cout << q.val() << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); #ifdef NACHIA int T; cin >> T; for(int t=0; t