#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } //////////////////////////////////////////////////////////////////////////////// template struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// constexpr unsigned MO = 998244353U; constexpr unsigned MO2 = 2U * MO; constexpr int FFT_MAX = 23; using Mint = ModInt; constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U}; constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U}; constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U}; constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U}; // as[rev(i)] <- \sum_j \zeta^(ij) as[j] void fft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = n; if (m >>= 1) { for (int i = 0; i < m; ++i) { const unsigned x = as[i + m].x; // < MO as[i + m].x = as[i].x + MO - x; // < 2 MO as[i].x += x; // < 2 MO } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } for (; m; ) { if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 4 MO as[i].x += x; // < 4 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } } for (int i = 0; i < n; ++i) { as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x; // < MO } } // as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)] void invFft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = 1; if (m < n >> 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } m <<= 1; } for (; m < n >> 1; m <<= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + (m >> 1); ++i) { const unsigned long long y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } for (int i = i0 + (m >> 1); i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } } if (m < n) { for (int i = 0; i < m; ++i) { const unsigned y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i + m].x = y; // < 4 MO } } const Mint invN = Mint(n).inv(); for (int i = 0; i < n; ++i) { as[i] *= invN; } } void fft(vector &as) { fft(as.data(), as.size()); } void invFft(vector &as) { invFft(as.data(), as.size()); } vector convolve(vector as, vector bs) { if (as.empty() || bs.empty()) return {}; const int len = as.size() + bs.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); bs.resize(n); fft(bs); for (int i = 0; i < n; ++i) as[i] *= bs[i]; invFft(as); as.resize(len); return as; } vector square(vector as) { if (as.empty()) return {}; const int len = as.size() + as.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); for (int i = 0; i < n; ++i) as[i] *= as[i]; invFft(as); as.resize(len); return as; } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // inv: log, exp, pow // fac: shift // invFac: shift constexpr int LIM_INV = 1 << 20; // @ Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV]; struct ModIntPreparator { ModIntPreparator() { inv[1] = 1; for (int i = 2; i < LIM_INV; ++i) inv[i] = -((Mint::M / i) * inv[Mint::M % i]); fac[0] = 1; for (int i = 1; i < LIM_INV; ++i) fac[i] = fac[i - 1] * i; invFac[0] = 1; for (int i = 1; i < LIM_INV; ++i) invFac[i] = invFac[i - 1] * inv[i]; } } preparator; // polyWork0: *, inv, div, divAt, log, exp, pow, sqrt, shift // polyWork1: inv, div, divAt, log, exp, pow, sqrt, shift // polyWork2: divAt, exp, pow, sqrt // polyWork3: exp, pow, sqrt static constexpr int LIM_POLY = 1 << 20; // @ static_assert(LIM_POLY <= 1 << FFT_MAX, "Poly: LIM_POLY <= 1 << FFT_MAX must hold."); static Mint polyWork0[LIM_POLY], polyWork1[LIM_POLY], polyWork2[LIM_POLY], polyWork3[LIM_POLY]; struct Poly : public vector { Poly() {} explicit Poly(int n) : vector(n) {} Poly(const vector &vec) : vector(vec) {} Poly(std::initializer_list il) : vector(il) {} int size() const { return vector::size(); } Mint at(long long k) const { return (0 <= k && k < size()) ? (*this)[k] : 0U; } int ord() const { for (int i = 0; i < size(); ++i) if ((*this)[i]) return i; return -1; } int deg() const { for (int i = size(); --i >= 0; ) if ((*this)[i]) return i; return -1; } Poly mod(int n) const { return Poly(vector(data(), data() + min(n, size()))); } friend std::ostream &operator<<(std::ostream &os, const Poly &fs) { os << "["; for (int i = 0; i < fs.size(); ++i) { if (i > 0) os << ", "; os << fs[i]; } return os << "]"; } Poly &operator+=(const Poly &fs) { if (size() < fs.size()) resize(fs.size()); for (int i = 0; i < fs.size(); ++i) (*this)[i] += fs[i]; return *this; } Poly &operator-=(const Poly &fs) { if (size() < fs.size()) resize(fs.size()); for (int i = 0; i < fs.size(); ++i) (*this)[i] -= fs[i]; return *this; } // 3 E(|t| + |f|) Poly &operator*=(const Poly &fs) { if (empty() || fs.empty()) return *this = {}; const int nt = size(), nf = fs.size(); int n = 1; for (; n < nt + nf - 1; n <<= 1) {} assert(n <= LIM_POLY); resize(n); fft(data(), n); // 1 E(n) memcpy(polyWork0, fs.data(), nf * sizeof(Mint)); memset(polyWork0 + nf, 0, (n - nf) * sizeof(Mint)); fft(polyWork0, n); // 1 E(n) for (int i = 0; i < n; ++i) (*this)[i] *= polyWork0[i]; invFft(data(), n); // 1 E(n) resize(nt + nf - 1); return *this; } // 13 E(deg(t) - deg(f) + 1) // rev(t) = rev(f) rev(q) + x^(deg(t)-deg(f)+1) rev(r) Poly &operator/=(const Poly &fs) { const int m = deg(), n = fs.deg(); assert(n != -1); if (m < n) return *this = {}; Poly tsRev(m - n + 1), fsRev(min(m - n, n) + 1); for (int i = 0; i <= m - n; ++i) tsRev[i] = (*this)[m - i]; for (int i = 0, i0 = min(m - n, n); i <= i0; ++i) fsRev[i] = fs[n - i]; const Poly qsRev = tsRev.div(fsRev, m - n + 1); // 13 E(m - n + 1) resize(m - n + 1); for (int i = 0; i <= m - n; ++i) (*this)[i] = qsRev[m - n - i]; return *this; } // 13 E(deg(t) - deg(f) + 1) + 3 E(|t|) Poly &operator%=(const Poly &fs) { const Poly qs = *this / fs; // 13 E(deg(t) - deg(f) + 1) *this -= fs * qs; // 3 E(|t|) resize(deg() + 1); return *this; } Poly &operator*=(const Mint &a) { for (int i = 0; i < size(); ++i) (*this)[i] *= a; return *this; } Poly &operator/=(const Mint &a) { const Mint b = a.inv(); for (int i = 0; i < size(); ++i) (*this)[i] *= b; return *this; } Poly operator+() const { return *this; } Poly operator-() const { Poly fs(size()); for (int i = 0; i < size(); ++i) fs[i] = -(*this)[i]; return fs; } Poly operator+(const Poly &fs) const { return (Poly(*this) += fs); } Poly operator-(const Poly &fs) const { return (Poly(*this) -= fs); } Poly operator*(const Poly &fs) const { return (Poly(*this) *= fs); } Poly operator/(const Poly &fs) const { return (Poly(*this) /= fs); } Poly operator%(const Poly &fs) const { return (Poly(*this) %= fs); } Poly operator*(const Mint &a) const { return (Poly(*this) *= a); } Poly operator/(const Mint &a) const { return (Poly(*this) /= a); } friend Poly operator*(const Mint &a, const Poly &fs) { return fs * a; } // 10 E(n) // f <- f - (t f - 1) f Poly inv(int n) const { assert(!empty()); assert((*this)[0]); assert(1 <= n); assert(n == 1 || 1 << (32 - __builtin_clz(n - 1)) <= LIM_POLY); Poly fs(n); fs[0] = (*this)[0].inv(); for (int m = 1; m < n; m <<= 1) { memcpy(polyWork0, data(), min(m << 1, size()) * sizeof(Mint)); memset(polyWork0 + min(m << 1, size()), 0, ((m << 1) - min(m << 1, size())) * sizeof(Mint)); fft(polyWork0, m << 1); // 2 E(n) memcpy(polyWork1, fs.data(), min(m << 1, n) * sizeof(Mint)); memset(polyWork1 + min(m << 1, n), 0, ((m << 1) - min(m << 1, n)) * sizeof(Mint)); fft(polyWork1, m << 1); // 2 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 2 E(n) memset(polyWork0, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 2 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 2 E(n) for (int i = m, i0 = min(m << 1, n); i < i0; ++i) fs[i] = -polyWork0[i]; } return fs; } // 9 E(n) // Need (4 m)-th roots of unity to lift from (mod x^m) to (mod x^(2m)). // f <- f - (t f - 1) f // (t f^2) mod ((x^(2m) - 1) (x^m - 1^(1/4))) /* Poly inv(int n) const { assert(!empty()); assert((*this)[0]); assert(1 <= n); assert(n == 1 || 3 << (31 - __builtin_clz(n - 1)) <= LIM_POLY); assert(n <= 1 << (FFT_MAX - 1)); Poly fs(n); fs[0] = (*this)[0].inv(); for (int h = 2, m = 1; m < n; ++h, m <<= 1) { const Mint a = FFT_ROOTS[h], b = INV_FFT_ROOTS[h]; memcpy(polyWork0, data(), min(m << 1, size()) * sizeof(Mint)); memset(polyWork0 + min(m << 1, size()), 0, ((m << 1) - min(m << 1, size())) * sizeof(Mint)); { Mint aa = 1; for (int i = 0; i < m; ++i) { polyWork0[(m << 1) + i] = aa * polyWork0[i]; aa *= a; } for (int i = 0; i < m; ++i) { polyWork0[(m << 1) + i] += aa * polyWork0[m + i]; aa *= a; } } fft(polyWork0, m << 1); // 2 E(n) fft(polyWork0 + (m << 1), m); // 1 E(n) memcpy(polyWork1, fs.data(), min(m << 1, n) * sizeof(Mint)); memset(polyWork1 + min(m << 1, n), 0, ((m << 1) - min(m << 1, n)) * sizeof(Mint)); { Mint aa = 1; for (int i = 0; i < m; ++i) { polyWork1[(m << 1) + i] = aa * polyWork1[i]; aa *= a; } for (int i = 0; i < m; ++i) { polyWork1[(m << 1) + i] += aa * polyWork1[m + i]; aa *= a; } } fft(polyWork1, m << 1); // 2 E(n) fft(polyWork1 + (m << 1), m); // 1 E(n) for (int i = 0; i < (m << 1) + m; ++i) polyWork0[i] *= polyWork1[i] * polyWork1[i]; invFft(polyWork0, m << 1); // 2 E(n) invFft(polyWork0 + (m << 1), m); // 1 E(n) // 2 f0 + (-f2), (-f1) + (-f3), 1^(1/4) (-f1) - (-f2) - 1^(1/4) (-f3) { Mint bb = 1; for (int i = 0, i0 = min(m, n - m); i < i0; ++i) { unsigned x = polyWork0[i].x + (bb * polyWork0[(m << 1) + i]).x + MO2 - (fs[i].x << 1); // < 4 MO fs[m + i] = Mint(static_cast(FFT_ROOTS[2].x) * x) - polyWork0[m + i]; fs[m + i].x = ((fs[m + i].x & 1) ? (fs[m + i].x + MO) : fs[m + i].x) >> 1; bb *= b; } } } return fs; } */ // 13 E(n) // g = (1 / f) mod x^m // h <- h - (f h - t) g Poly div(const Poly &fs, int n) const { assert(!fs.empty()); assert(fs[0]); assert(1 <= n); if (n == 1) return {at(0) / fs[0]}; // m < n <= 2 m const int m = 1 << (31 - __builtin_clz(n - 1)); assert(m << 1 <= LIM_POLY); Poly gs = fs.inv(m); // 5 E(n) gs.resize(m << 1); fft(gs.data(), m << 1); // 1 E(n) memcpy(polyWork0, data(), min(m, size()) * sizeof(Mint)); memset(polyWork0 + min(m, size()), 0, ((m << 1) - min(m, size())) * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= gs[i]; invFft(polyWork0, m << 1); // 1 E(n) Poly hs(n); memcpy(hs.data(), polyWork0, m * sizeof(Mint)); memset(polyWork0 + m, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) memcpy(polyWork1, fs.data(), min(m << 1, fs.size()) * sizeof(Mint)); memset(polyWork1 + min(m << 1, fs.size()), 0, ((m << 1) - min(m << 1, fs.size())) * sizeof(Mint)); fft(polyWork1, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 1 E(n) memset(polyWork0, 0, m * sizeof(Mint)); for (int i = m, i0 = min(m << 1, size()); i < i0; ++i) polyWork0[i] -= (*this)[i]; fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= gs[i]; invFft(polyWork0, m << 1); // 1 E(n) for (int i = m; i < n; ++i) hs[i] = -polyWork0[i]; return hs; } // (4 (floor(log_2 k) - ceil(log_2 |f|)) + 16) E(|f|) for |t| < |f| // [x^k] (t(x) / f(x)) = [x^k] ((t(x) f(-x)) / (f(x) f(-x)) // polyWork0: half of (2 m)-th roots of unity, inversed, bit-reversed Mint divAt(const Poly &fs, long long k) const { assert(k >= 0); if (size() >= fs.size()) { const Poly qs = *this / fs; // 13 E(deg(t) - deg(f) + 1) Poly rs = *this - fs * qs; // 3 E(|t|) rs.resize(rs.deg() + 1); return qs.at(k) + rs.divAt(fs, k); } int h = 0, m = 1; for (; m < fs.size(); ++h, m <<= 1) {} if (k < m) { const Poly gs = fs.inv(k + 1); // 10 E(|f|) Mint sum; for (int i = 0, i0 = min(k + 1, size()); i < i0; ++i) sum += (*this)[i] * gs[k - i]; return sum; } assert(m << 1 <= LIM_POLY); polyWork0[0] = Mint(2U).inv(); for (int hh = 0; hh < h; ++hh) for (int i = 0; i < 1 << hh; ++i) polyWork0[1 << hh | i] = polyWork0[i] * INV_FFT_ROOTS[hh + 2]; const Mint a = FFT_ROOTS[h + 1]; memcpy(polyWork2, data(), size() * sizeof(Mint)); memset(polyWork2 + size(), 0, ((m << 1) - size()) * sizeof(Mint)); fft(polyWork2, m << 1); // 2 E(|f|) memcpy(polyWork1, fs.data(), fs.size() * sizeof(Mint)); memset(polyWork1 + fs.size(), 0, ((m << 1) - fs.size()) * sizeof(Mint)); fft(polyWork1, m << 1); // 2 E(|f|) for (; ; ) { if (k & 1) { for (int i = 0; i < m; ++i) polyWork2[i] = polyWork0[i] * (polyWork2[i << 1 | 0] * polyWork1[i << 1 | 1] - polyWork2[i << 1 | 1] * polyWork1[i << 1 | 0]); } else { for (int i = 0; i < m; ++i) { polyWork2[i] = polyWork2[i << 1 | 0] * polyWork1[i << 1 | 1] + polyWork2[i << 1 | 1] * polyWork1[i << 1 | 0]; polyWork2[i].x = ((polyWork2[i].x & 1) ? (polyWork2[i].x + MO) : polyWork2[i].x) >> 1; } } for (int i = 0; i < m; ++i) polyWork1[i] = polyWork1[i << 1 | 0] * polyWork1[i << 1 | 1]; if ((k >>= 1) < m) { invFft(polyWork2, m); // 1 E(|f|) invFft(polyWork1, m); // 1 E(|f|) // Poly::inv does not use polyWork2 const Poly gs = Poly(vector(polyWork1, polyWork1 + k + 1)).inv(k + 1); // 10 E(|f|) Mint sum; for (int i = 0; i <= k; ++i) sum += polyWork2[i] * gs[k - i]; return sum; } memcpy(polyWork2 + m, polyWork2, m * sizeof(Mint)); invFft(polyWork2 + m, m); // (floor(log_2 k) - ceil(log_2 |f|)) E(|f|) memcpy(polyWork1 + m, polyWork1, m * sizeof(Mint)); invFft(polyWork1 + m, m); // (floor(log_2 k) - ceil(log_2 |f|)) E(|f|) Mint aa = 1; for (int i = m; i < m << 1; ++i) { polyWork2[i] *= aa; polyWork1[i] *= aa; aa *= a; } fft(polyWork2 + m, m); // (floor(log_2 k) - ceil(log_2 |f|)) E(|f|) fft(polyWork1 + m, m); // (floor(log_2 k) - ceil(log_2 |f|)) E(|f|) } } // 13 E(n) // D log(t) = (D t) / t Poly log(int n) const { assert(!empty()); assert((*this)[0].x == 1U); assert(n <= LIM_INV); Poly fs = mod(n); for (int i = 0; i < fs.size(); ++i) fs[i] *= i; fs = fs.div(*this, n); for (int i = 1; i < n; ++i) fs[i] *= ::inv[i]; return fs; } // (16 + 1/2) E(n) // f = exp(t) mod x^m ==> (D f) / f == D t (mod x^m) // g = (1 / exp(t)) mod x^m // f <- f - (log f - t) / (1 / f) // = f - (I ((D f) / f) - t) f // == f - (I ((D f) / f + (f g - 1) ((D f) / f - D (t mod x^m))) - t) f (mod x^(2m)) // = f - (I (g (D f - f D (t mod x^m)) + D (t mod x^m)) - t) f // g <- g - (f g - 1) g // polyWork1: DFT(f, 2 m), polyWork2: g, polyWork3: DFT(g, 2 m) Poly exp(int n) const { assert(!empty()); assert(!(*this)[0]); assert(1 <= n); assert(n == 1 || 1 << (32 - __builtin_clz(n - 1)) <= min(LIM_INV, LIM_POLY)); if (n == 1) return {1U}; if (n == 2) return {1U, at(1)}; Poly fs(n); fs[0].x = polyWork1[0].x = polyWork1[1].x = polyWork2[0].x = 1U; int m; for (m = 1; m << 1 < n; m <<= 1) { for (int i = 0, i0 = min(m, size()); i < i0; ++i) polyWork0[i] = i * (*this)[i]; memset(polyWork0 + min(m, size()), 0, (m - min(m, size())) * sizeof(Mint)); fft(polyWork0, m); // (1/2) E(n) for (int i = 0; i < m; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m); // (1/2) E(n) for (int i = 0; i < m; ++i) polyWork0[i] -= i * fs[i]; memset(polyWork0 + m, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) memcpy(polyWork3, polyWork2, m * sizeof(Mint)); memset(polyWork3 + m, 0, m * sizeof(Mint)); fft(polyWork3, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork3[i]; invFft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m; ++i) polyWork0[i] *= ::inv[m + i]; for (int i = 0, i0 = min(m, size() - m); i < i0; ++i) polyWork0[i] += (*this)[m + i]; memset(polyWork0 + m, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 1 E(n) memcpy(fs.data() + m, polyWork0, m * sizeof(Mint)); memcpy(polyWork1, fs.data(), (m << 1) * sizeof(Mint)); memset(polyWork1 + (m << 1), 0, (m << 1) * sizeof(Mint)); fft(polyWork1, m << 2); // 2 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] = polyWork1[i] * polyWork3[i]; invFft(polyWork0, m << 1); // 1 E(n) memset(polyWork0, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork3[i]; invFft(polyWork0, m << 1); // 1 E(n) for (int i = m; i < m << 1; ++i) polyWork2[i] = -polyWork0[i]; } for (int i = 0, i0 = min(m, size()); i < i0; ++i) polyWork0[i] = i * (*this)[i]; memset(polyWork0 + min(m, size()), 0, (m - min(m, size())) * sizeof(Mint)); fft(polyWork0, m); // (1/2) E(n) for (int i = 0; i < m; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m); // (1/2) E(n) for (int i = 0; i < m; ++i) polyWork0[i] -= i * fs[i]; memcpy(polyWork0 + m, polyWork0 + (m >> 1), (m >> 1) * sizeof(Mint)); memset(polyWork0 + (m >> 1), 0, (m >> 1) * sizeof(Mint)); memset(polyWork0 + m + (m >> 1), 0, (m >> 1) * sizeof(Mint)); fft(polyWork0, m); // (1/2) E(n) fft(polyWork0 + m, m); // (1/2) E(n) memcpy(polyWork3 + m, polyWork2 + (m >> 1), (m >> 1) * sizeof(Mint)); memset(polyWork3 + m + (m >> 1), 0, (m >> 1) * sizeof(Mint)); fft(polyWork3 + m, m); // (1/2) E(n) for (int i = 0; i < m; ++i) polyWork0[m + i] = polyWork0[i] * polyWork3[m + i] + polyWork0[m + i] * polyWork3[i]; for (int i = 0; i < m; ++i) polyWork0[i] *= polyWork3[i]; invFft(polyWork0, m); // (1/2) E(n) invFft(polyWork0 + m, m); // (1/2) E(n) for (int i = 0; i < m >> 1; ++i) polyWork0[(m >> 1) + i] += polyWork0[m + i]; for (int i = 0; i < m; ++i) polyWork0[i] *= ::inv[m + i]; for (int i = 0, i0 = min(m, size() - m); i < i0; ++i) polyWork0[i] += (*this)[m + i]; memset(polyWork0 + m, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 1 E(n) memcpy(fs.data() + m, polyWork0, (n - m) * sizeof(Mint)); return fs; } // (29 + 1/2) E(n) // g <- g - (log g - a log t) g Poly pow(Mint a, int n) const { assert(!empty()); assert((*this)[0].x == 1U); assert(1 <= n); return (a * log(n)).exp(n); // 13 E(n) + (16 + 1/2) E(n) } // (29 + 1/2) E(n - a ord(t)) Poly pow(long long a, int n) const { assert(a >= 0); assert(1 <= n); if (a == 0) { Poly gs(n); gs[0].x = 1U; return gs; } const int o = ord(); if (o == -1 || o > (n - 1) / a) return Poly(n); const Mint b = (*this)[o].inv(), c = (*this)[o].pow(a); const int ntt = min(n - a * o, size() - o); Poly tts(ntt); for (int i = 0; i < ntt; ++i) tts[i] = b * (*this)[o + i]; tts = tts.pow(Mint(a), n - a * o); // (29 + 1/2) E(n - a ord(t)) Poly gs(n); for (int i = 0; i < n - a * o; ++i) gs[a * o + i] = c * tts[i]; return gs; } // (10 + 1/2) E(n) // f = t^(1/2) mod x^m, g = 1 / t^(1/2) mod x^m // f <- f - (f^2 - h) g / 2 // g <- g - (f g - 1) g // polyWork1: DFT(f, m), polyWork2: g, polyWork3: DFT(g, 2 m) Poly sqrt(int n) const { assert(!empty()); assert((*this)[0].x == 1U); assert(1 <= n); assert(n == 1 || 1 << (32 - __builtin_clz(n - 1)) <= LIM_POLY); if (n == 1) return {1U}; if (n == 2) return {1U, at(1) / 2}; Poly fs(n); fs[0].x = polyWork1[0].x = polyWork2[0].x = 1U; int m; for (m = 1; m << 1 < n; m <<= 1) { for (int i = 0; i < m; ++i) polyWork1[i] *= polyWork1[i]; invFft(polyWork1, m); // (1/2) E(n) for (int i = 0, i0 = min(m, size()); i < i0; ++i) polyWork1[i] -= (*this)[i]; for (int i = 0, i0 = min(m, size() - m); i < i0; ++i) polyWork1[i] -= (*this)[m + i]; memset(polyWork1 + m, 0, m * sizeof(Mint)); fft(polyWork1, m << 1); // 1 E(n) memcpy(polyWork3, polyWork2, m * sizeof(Mint)); memset(polyWork3 + m, 0, m * sizeof(Mint)); fft(polyWork3, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork1[i] *= polyWork3[i]; invFft(polyWork1, m << 1); // 1 E(n) for (int i = 0; i < m; ++i) { polyWork1[i] = -polyWork1[i]; fs[m + i].x = ((polyWork1[i].x & 1) ? (polyWork1[i].x + MO) : polyWork1[i].x) >> 1; } memcpy(polyWork1, fs.data(), (m << 1) * sizeof(Mint)); fft(polyWork1, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] = polyWork1[i] * polyWork3[i]; invFft(polyWork0, m << 1); // 1 E(n) memset(polyWork0, 0, m * sizeof(Mint)); fft(polyWork0, m << 1); // 1 E(n) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork3[i]; invFft(polyWork0, m << 1); // 1 E(n) for (int i = m; i < m << 1; ++i) polyWork2[i] = -polyWork0[i]; } for (int i = 0; i < m; ++i) polyWork1[i] *= polyWork1[i]; invFft(polyWork1, m); // (1/2) E(n) for (int i = 0, i0 = min(m, size()); i < i0; ++i) polyWork1[i] -= (*this)[i]; for (int i = 0, i0 = min(m, size() - m); i < i0; ++i) polyWork1[i] -= (*this)[m + i]; memcpy(polyWork1 + m, polyWork1 + (m >> 1), (m >> 1) * sizeof(Mint)); memset(polyWork1 + (m >> 1), 0, (m >> 1) * sizeof(Mint)); memset(polyWork1 + m + (m >> 1), 0, (m >> 1) * sizeof(Mint)); fft(polyWork1, m); // (1/2) E(n) fft(polyWork1 + m, m); // (1/2) E(n) memcpy(polyWork3 + m, polyWork2 + (m >> 1), (m >> 1) * sizeof(Mint)); memset(polyWork3 + m + (m >> 1), 0, (m >> 1) * sizeof(Mint)); fft(polyWork3 + m, m); // (1/2) E(n) // for (int i = 0; i < m << 1; ++i) polyWork1[i] *= polyWork3[i]; for (int i = 0; i < m; ++i) polyWork1[m + i] = polyWork1[i] * polyWork3[m + i] + polyWork1[m + i] * polyWork3[i]; for (int i = 0; i < m; ++i) polyWork1[i] *= polyWork3[i]; invFft(polyWork1, m); // (1/2) E(n) invFft(polyWork1 + m, m); // (1/2) E(n) for (int i = 0; i < m >> 1; ++i) polyWork1[(m >> 1) + i] += polyWork1[m + i]; for (int i = 0; i < n - m; ++i) { polyWork1[i] = -polyWork1[i]; fs[m + i].x = ((polyWork1[i].x & 1) ? (polyWork1[i].x + MO) : polyWork1[i].x) >> 1; } return fs; } // (10 + 1/2) E(n) // modSqrt must return a quadratic residue if exists, or anything otherwise. // Return {} if *this does not have a square root. template Poly sqrt(int n, F modSqrt) const { assert(1 <= n); const int o = ord(); if (o == -1) return Poly(n); if (o & 1) return {}; const Mint c = modSqrt((*this)[o]); if (c * c != (*this)[o]) return {}; if (o >> 1 >= n) return Poly(n); const Mint b = (*this)[o].inv(); const int ntt = min(n - (o >> 1), size() - o); Poly tts(ntt); for (int i = 0; i < ntt; ++i) tts[i] = b * (*this)[o + i]; tts = tts.sqrt(n - (o >> 1)); // (10 + 1/2) E(n) Poly gs(n); for (int i = 0; i < n - (o >> 1); ++i) gs[(o >> 1) + i] = c * tts[i]; return gs; } // 6 E(|t|) // x -> x + a Poly shift(const Mint &a) const { if (empty()) return {}; const int n = size(); int m = 1; for (; m < n; m <<= 1) {} for (int i = 0; i < n; ++i) polyWork0[i] = fac[i] * (*this)[i]; memset(polyWork0 + n, 0, ((m << 1) - n) * sizeof(Mint)); fft(polyWork0, m << 1); // 2 E(|t|) { Mint aa = 1; for (int i = 0; i < n; ++i) { polyWork1[n - 1 - i] = invFac[i] * aa; aa *= a; } } memset(polyWork1 + n, 0, ((m << 1) - n) * sizeof(Mint)); fft(polyWork1, m << 1); // 2 E(|t|) for (int i = 0; i < m << 1; ++i) polyWork0[i] *= polyWork1[i]; invFft(polyWork0, m << 1); // 2 E(|t|) Poly fs(n); for (int i = 0; i < n; ++i) fs[i] = invFac[i] * polyWork0[n - 1 + i]; return fs; } }; Mint linearRecurrenceAt(const vector &as, const vector &cs, long long k) { assert(!cs.empty()); assert(cs[0]); const int d = cs.size() - 1; assert(as.size() >= static_cast(d)); return (Poly(vector(as.begin(), as.begin() + d)) * cs).mod(d).divAt(cs, k); } struct SubproductTree { int logN, n, nn; vector xs; // [DFT_4((X-xs[0])(X-xs[1])(X-xs[2])(X-xs[3]))] [(X-xs[0])(X-xs[1])(X-xs[2])(X-xs[3])mod X^4] // [ DFT_4((X-xs[0])(X-xs[1])) ] [ DFT_4((X-xs[2])(X-xs[3])) ] // [ DFT_2(X-xs[0]) ] [ DFT_2(X-xs[1]) ] [ DFT_2(X-xs[2]) ] [ DFT_2(X-xs[3]) ] vector buf; vector gss; // (1 - xs[0] X) ... (1 - xs[nn-1] X) Poly all; // (ceil(log_2 n) + O(1)) E(n) SubproductTree(const vector &xs_) { n = xs_.size(); for (logN = 0, nn = 1; nn < n; ++logN, nn <<= 1) {} xs.assign(nn, 0U); memcpy(xs.data(), xs_.data(), n * sizeof(Mint)); buf.assign((logN + 1) * (nn << 1), 0U); gss.assign(nn << 1, nullptr); for (int h = 0; h <= logN; ++h) for (int u = 1 << h; u < 1 << (h + 1); ++u) { gss[u] = buf.data() + (h * (nn << 1) + ((u - (1 << h)) << (logN - h + 1))); } for (int i = 0; i < nn; ++i) { gss[nn + i][0] = -xs[i] + 1; gss[nn + i][1] = -xs[i] - 1; } if (nn == 1) gss[1][1] += 2; for (int h = logN; --h >= 0; ) { const int m = 1 << (logN - h); for (int u = 1 << (h + 1); --u >= 1 << h; ) { for (int i = 0; i < m; ++i) gss[u][i] = gss[u << 1][i] * gss[u << 1 | 1][i]; memcpy(gss[u] + m, gss[u], m * sizeof(Mint)); invFft(gss[u] + m, m); // ((1/2) ceil(log_2 n) + O(1)) E(n) if (h > 0) { gss[u][m] -= 2; const Mint a = FFT_ROOTS[logN - h + 1]; Mint aa = 1; for (int i = m; i < m << 1; ++i) { gss[u][i] *= aa; aa *= a; }; fft(gss[u] + m, m); // ((1/2) ceil(log_2 n) + O(1)) E(n) } } } all.resize(nn + 1); all[0] = 1; for (int i = 1; i < nn; ++i) all[i] = gss[1][nn + nn - i]; all[nn] = gss[1][nn] - 1; } // ((3/2) ceil(log_2 n) + O(1)) E(n) + 10 E(|f|) + 3 E(|f| + 2^(ceil(log_2 n))) vector multiEval(const Poly &fs) const { vector work0(nn), work1(nn), work2(nn); { const int m = max(fs.size(), 1); auto invAll = all.inv(m); // 10 E(|f|) std::reverse(invAll.begin(), invAll.end()); int mm; for (mm = 1; mm < m - 1 + nn; mm <<= 1) {} invAll.resize(mm, 0U); fft(invAll); // E(|f| + 2^(ceil(log_2 n))) vector ffs(mm, 0U); memcpy(ffs.data(), fs.data(), fs.size() * sizeof(Mint)); fft(ffs); // E(|f| + 2^(ceil(log_2 n))) for (int i = 0; i < mm; ++i) ffs[i] *= invAll[i]; invFft(ffs); // E(|f| + 2^(ceil(log_2 n))) memcpy(((logN & 1) ? work1 : work0).data(), ffs.data() + m - 1, nn * sizeof(Mint)); } for (int h = 0; h < logN; ++h) { const int m = 1 << (logN - h); for (int u = 1 << h; u < 1 << (h + 1); ++u) { Mint *hs = (((logN - h) & 1) ? work1 : work0).data() + ((u - (1 << h)) << (logN - h)); Mint *hs0 = (((logN - h) & 1) ? work0 : work1).data() + ((u - (1 << h)) << (logN - h)); Mint *hs1 = hs0 + (m >> 1); fft(hs, m); // ((1/2) ceil(log_2 n) + O(1)) E(n) for (int i = 0; i < m; ++i) work2[i] = gss[u << 1 | 1][i] * hs[i]; invFft(work2.data(), m); // ((1/2) ceil(log_2 n) + O(1)) E(n) memcpy(hs0, work2.data() + (m >> 1), (m >> 1) * sizeof(Mint)); for (int i = 0; i < m; ++i) work2[i] = gss[u << 1][i] * hs[i]; invFft(work2.data(), m); // ((1/2) ceil(log_2 n) + O(1)) E(n) memcpy(hs1, work2.data() + (m >> 1), (m >> 1) * sizeof(Mint)); } } work0.resize(n); return work0; } // ((5/2) ceil(log_2 n) + O(1)) E(n) Poly interpolate(const vector &ys) const { assert(static_cast(ys.size()) == n); Poly gs(n); for (int i = 0; i < n; ++i) gs[i] = (i + 1) * all[n - (i + 1)]; const vector denoms = multiEval(gs); // ((3/2) ceil(log_2 n) + O(1)) E(n) vector work(nn << 1, 0U); for (int i = 0; i < n; ++i) { // xs[0], ..., xs[n - 1] are not distinct assert(denoms[i]); work[i << 1] = work[i << 1 | 1] = ys[i] / denoms[i]; } for (int h = logN; --h >= 0; ) { const int m = 1 << (logN - h); for (int u = 1 << (h + 1); --u >= 1 << h; ) { Mint *hs = work.data() + ((u - (1 << h)) << (logN - h + 1)); for (int i = 0; i < m; ++i) hs[i] = gss[u << 1 | 1][i] * hs[i] + gss[u << 1][i] * hs[m + i]; if (h > 0) { memcpy(hs + m, hs, m * sizeof(Mint)); invFft(hs + m, m); // ((1/2) ceil(log_2 n) + O(1)) E(n) const Mint a = FFT_ROOTS[logN - h + 1]; Mint aa = 1; for (int i = m; i < m << 1; ++i) { hs[i] *= aa; aa *= a; }; fft(hs + m, m); // ((1/2) ceil(log_2 n) + O(1)) E(n) } } } invFft(work.data(), nn); // E(n) return Poly(vector(work.data() + nn - n, work.data() + nn)); } }; //////////////////////////////////////////////////////////////////////////////// constexpr int FACTORIAL_STEP = 1'000'000; constexpr ModInt<998244353> FACTORIAL[] = {1,373341033,45596018,834980587,623627864,428937595,442819817,499710224,833655840,83857087,295201906,788488293,671639287,849315549,597398273,813259672,732727656,244038325,122642896,310517972,160030060,483239722,683879839,712910418,384710263,433880730,844360005,513089677,101492974,959253371,957629942,678615452,34035221,56734233,524027922,31729117,102311167,330331487,8332991,832392662,545208507,594075875,318497156,859275605,300738984,767818091,864118508,878131539,316588744,812496962,213689172,584871249,980836133,54096741,417876813,363266670,335481797,730839588,393495668,435793297,760025067,811438469,720976283,650770098,586537547,117371703,566486504,749562308,708205284,932912293,939830261,983699513,206579820,301188781,593164676,770845925,247687458,41047791,266419267,937835947,506268060,6177705,936268003,166873118,443834893,328979964,470135404,954410105,117565665,832761782,39806322,478922755,394880724,821825588,468705875,512554988,232240472,876497899,356048018,895187265,808258749,575505950,68190615,939065335,552199946,694814243,385460530,529769387,640377761,916128300,440133909,362216114,826373774,502324157,457648395,385510728,904737188,78988746,454565719,623828097,686156489,713476044,63602402,570334625,681055904,222059821,477211096,343363294,833792655,461853093,741797144,74731896,930484262,268372735,941222802,677432735,474842829,700451655,400176109,697644778,390377694,790010794,360642718,505712943,946647976,339045014,715797300,251680896,70091750,40517433,12629586,850635539,110877109,571935891,695965747,634938288,69072133,155093216,749696762,963086402,544711799,724471925,334646013,574791029,722417626,377929821,743946412,988034679,405207112,18063742,104121967,638607426,607304611,751377777,35834555,313632531,18058363,656121134,40763559,562910912,495867250,48767038,210864657,659137294,715390025,865854329,324322857,388911184,286059202,636456178,421290700,832276048,726437551,526417714,252522639,386147469,674313019,274769381,226519400,272047186,117153405,712896591,486826649,119444874,338909703,18536028,41814114,245606459,140617938,250512392,57084755,157807456,261113192,40258068,194807105,325341339,884328111,896332013,880836012,737358206,202713771,785454372,399586250,485457499,640827004,546969497,749602473,159788463,159111724,218592929,675932866,314795475,811539323,246883213,696818315,759880589,4302336,353070689,477909706,559289160,79781699,878094972,840903973,367416824,973366814,848259019,462421750,667227759,897917455,81800722,956276337,942686845,420541799,417005912,272641764,941778993,217214373,192220616,267901132,50530621,652678397,354880856,164289049,781023184,105376215,315094878,607856504,733905911,457743498,992735713,35212756,231822660,276036750,734558079,424180850,433186147,308380947,18333316,12935086,351491725,655645460,535812389,521902115,67016984,48682076,64748124,489360447,361275315,786336279,805161272,468129309,645091350,887284732,913004502,358814684,281295633,328970139,395955130,164840186,820902807,761699708,246274415,592331769,913846362,866682684,600130702,903837674,529462989,90612675,526540127,533047427,110008879,674279751,801920753,645226926,676886948,752481486,474034007,457790341,166813684,287671032,188118664,244731384,404032157,269766986,423996017,182948540,356801634,737863144,652014069,206068022,504569410,919894484,593398649,963768176,882517476,702523597,949028249,128957299,171997372,50865043,20937461,690959202,581356488,369182214,993580422,193500140,540665426,365786018,743731625,144980423,979536721,773259009,617053935,247670131,843705280,30419459,985463402,261585206,237885042,111276893,488166208,137660292,720784236,244467770,26368504,792857103,666885724,670313309,905683034,259415897,512017253,826265493,111960112,633652060,918048438,516432938,386972415,996212724,610073831,444094191,72480267,665038087,11584804,301029012,723617861,113763819,778259899,937766095,535448641,593907889,783573565,673298635,599533244,655712590,173350007,868198597,169013813,585161712,697502214,573994984,285943986,675831407,3134056,965907646,401920943,665949756,236277883,612745912,813282113,892454686,901222267,624900982,927122298,686321335,84924870,927606072,506664166,353631992,165913238,566073550,816674343,864877926,171259407,908752311,874007723,803597299,613676466,880336545,282280109,128761001,58852065,474075900,434816091,364856903,149123648,388854780,314693916,423183826,419733481,888483202,238933227,336564048,757103493,100189123,855479832,51370348,403061033,496971759,831753030,251718753,272779384,683379259,488844621,881783783,659478190,445719559,740782647,546525906,985524427,548033568,333772553,331916427,752533273,730387628,93829695,655989476,930661318,334885743,466041862,428105027,888238707,232218076,769865249,730641039,616996159,231721356,326973501,426068899,722403656,742756734,663270261,364187931,350431704,671823672,633125919,226166717,386814657,237594135,451479365,546182474,119366536,465211069,605313606,728508871,249619035,663053607,900453742,48293872,229958401,62402409,69570431,71921532,960467929,537087913,514588945,513856225,415497414,286592050,645469437,102052166,163298189,873938719,617583886,986843080,962390239,580971332,665147020,88900164,89866970,826426395,616059995,443012312,659160562,229855967,687413213,59809521,398599610,325666688,154765991,159186619,210830877,386454418,84493735,974220646,820097297,2191828,481459931,729073424,551556379,926316039,151357011,808637654,218058015,786112034,850407126,84202800,94214098,30019651,121701603,176055335,865461951,553631971,286620803,984061713,888573766,302767023,977070668,110954576,83922475,51568171,60949367,19533020,510592752,615419476,341370469,912573425,286207526,206707897,384156962,414163604,193301813,749570167,366933789,11470970,600191572,391667731,328736286,30645366,215162519,604947226,236199953,718439098,411423177,803407599,632441623,766760224,263006576,757681534,61082578,681666415,947466395,12206799,659767098,933746852,978860867,59215985,161179205,439197472,259779111,511621808,145770512,882749888,943124465,872053396,631078482,166861622,743415395,772287179,602427948,924112080,385643091,794973480,883782693,869723371,805963889,313106351,262132854,400034567,488248149,265769800,791715397,408753255,468381897,415812467,172922144,64404368,281500398,512318142,288791777,955559118,242484726,536413695,205340854,707803527,576699812,218525078,875554190,46283078,833841915,763148293,807722138,788080170,556901372,150896699,253151120,97856807,918256774,771557187,582547026,472709375,911615063,743371401,641382840,446540967,184639537,157247760,775930891,939702814,499082462,19536133,548753627,593243221,563850263,185475971,687419227,396799323,657976136,864535682,433009242,860830935,33107339,517661450,467651311,812398757,202133852,431839017,709549400,99643620,773282878,290471030,61134552,129206504,929147251,837008968,422332597,353775281,469563025,62265336,835064501,851685235,21197005,264793769,326416680,118842991,84257200,763248924,687559609,150907932,401832452,242726978,766752066,959173604,390269102,992293822,744816299,476631694,177284763,702429415,374065901,169855231,629007616,719169602,564737074,475119050,714502830,40993711,820235888,749063595,239329111,612759169,18591377,419142436,442202439,941600951,158013406,637073231,471564060,447222237,701248503,599797734,577221870,69656699,51052704,6544303,10958310,554955500,943192237,192526269,897983911,961628039,240232720,627280533,710239542,70255649,261743865,228474833,776408079,304180483,63607040,953297493,758058902,395529997,156010331,825833840,539880795,234683685,52626619,751843490,116909119,62806842,574857555,353417551,40061330,822203768,681051568,490913702,9322961,766631257,124794668,37844313,163524507,729108319,490867505,47035168,682765157,53842115,817965276,757179922,339238384,909741023,150530547,158444563,140949492,993302799,551621442,137578883,475122706,443869843,605400098,689361523,769596520,801661499,474900284,586624857,349960501,134084537,650564083,877097974,379857427,887890124,159436401,133274277,986182139,729720334,568925901,459461496,499309445,493171177,460958750,380694152,168836226,840160881,141116880,225064950,109618190,842341383,85305729,759273275,97369807,669317759,766247510,829017039,550323884,261274540,918239352,29606025,870793828,293683814,378510746,367270918,481292028,813097823,798448487,230791733,899305835,504040630,162510533,479367951,275282274,806951470,462774647,56473153,184659008,905122161,664034750,109726629,59372704,325795100,486860143,843736533,924723613,880348000,801252478,616515290,776142608,284803450,583439582,274826676,6018349,377403437,244041569,527081707,544763288,708818585,354033051,904309832,589922898,673933870,682858433,945260111,899893421,515264973,911685911,9527148,239480646,524126897,48259065,578214879,118677219,786127243,869205770,923276513,937928886,802186160,12198440,638784295,34200904,758925811,185027790,80918046,120604699,610456697,573601211,208296321,49743354,653691911,490750754,674335312,887877110,875880304,308360096,414636410,886100267,8525751,636257427,558338775,500159951,696213291,97268896,364983542,937928436,641582714,586211304,345265657,994704486,443549763,207259440,302122082,166055224,623250998,239642551,476337075,283167364,211328914,68064804,950202136,187552679,18938709,646784245,598764068,538505481,610424991,864445053,390248689,278395191,686098470,935957187,868529577,329970687,804930040,84992079,474569269,810762228,573258936,756464212,155080225,286966169,283614605,19283401,24257676,871831819,612689791,846988741,617120754,971716517,979541482,297910784,991087897,783825907,214821357,689498189,405026419,946731704,609346370,707669156,457703127,957341187,980735523,649367684,791011898,82098966,234729712,105002711,130614285,291032164,193188049,363211260,58108651,100756444,954947696,346032213,863300806,36876722,622610957,289232396,667938985,734886266,395881057,417188702,183092975,887586469,83334648,797819763,100176902,781587414,841864935,371674670,18247584,}; template ModInt factorial(long long n) { assert(n >= 0); if (n >= static_cast(M)) return 0; const long long pos = n / FACTORIAL_STEP; const long long m0 = pos * FACTORIAL_STEP; const long long m1 = m0 + FACTORIAL_STEP; if (m1 < static_cast(M) && n - m0 > m1 - n) { ModInt prod = 1; for (long long i = m1; i > n; ) prod *= i--; return FACTORIAL[pos + 1] / prod; } else { ModInt prod = FACTORIAL[pos]; for (long long i = m0; i < n; ) prod *= ++i; return prod; } } Mint brute(Int n, Int k) { Mint ret = 0; Mint pw = 1; for (int a2 = 0; a2 <= k / 2; ++a2) { if (a2 >= k - n) { Mint num = 1; num *= fac[n] * invFac[n - (k - a2)] * invFac[k - 2 * a2] * invFac[a2]; num *= pw; ret += num; } pw *= inv[8]; } ret *= fac[k]; ret *= Mint(2).pow(k); return ret; } int T; vector N, K; vector ans; Int maxK; namespace small { vector bs; pair solve(int l, int r) { if (r - l == 1) { return make_pair(Poly{-l, 1}, Poly{bs[l]}); } else { const int mid = (l + r) / 2; const auto resL = solve(l, mid); const auto resR = solve(mid, r); return make_pair(resL.first * resR.first, resL.second + resL.first * resR.second); } } void run() { vector> tss(maxK + 1); for (int t = 0; t < T; ++t) { tss[K[t]].push_back(t); } // const Int big=1; const Int big = sqrt(maxK) * log1p(maxK); for (int k = 0; k <= maxK; ++k) { const auto &ts = tss[k]; const int tsLen = ts.size(); if (tsLen >= big) { bs.assign(k + 1, 0); { Mint pw = fac[k] * Mint(2).pow(k); for (int a2 = 0; a2 <= k / 2; ++a2) { bs[k - a2] += invFac[k - 2 * a2] * invFac[a2] * pw; pw *= inv[8]; } } const auto res = solve(0, k + 1); vector ns(tsLen); for (int i = 0; i < tsLen; ++i) { ns[i] = N[ts[i]]; } const auto ys = SubproductTree(ns).multiEval(res.second); for (int i = 0; i < tsLen; ++i) { ans[ts[i]] = ys[i]; } } else { for (const int t : ts) { ans[t] = brute(N[t], k); } } } } } // small namespace large { Mint solve(Mint n, Int k) { if (k >= MO) { return 0; } Mint ret = 0; Mint f = 1; Mint ni = n; ni.x += MO; for (int i = 0; i < k - k / 2; ++i) { f *= ni; --ni.x; } f *= factorial(k / 2).inv(); f *= inv[8].pow(k / 2); /* for (int a2 = k / 2; a2 >= 0; --a2) { ret += f * invFac[k - 2 * a2] * invFac[a2]; f *= ni; --ni.x; f *= 8; } */ constexpr int SIZE = 10'000'000; vector gs(SIZE); for (int c = k / 2, d; c >= 0; c = d) { d = max(c - SIZE, -1); const int len = c - d; gs[len - 1] = factorial(k - 2 * (d + 1)).inv(); for (int i = len - 1; --i >= 0; ) { gs[i] = gs[i + 1] * (k - 2 * (c - i) + 2) * (k - 2 * (c - i) + 1); } for (int i = 0; i < len; ++i) { // assert(gs[i]==invFac[k-2*(c-i)]); ret += f * gs[i]; f *= ni; --ni.x; f *= (c - i); f *= 8; } } ret *= factorial(k); ret *= Mint(2).pow(k); return ret; } void run() { for (int t = 0; t < T; ++t) { ans[t] = solve(N[t], K[t]); } } } // large int main() { for (; ~scanf("%d", &T); ) { N.resize(T); K.resize(T); for (int t = 0; t < T; ++t) { scanf("%lld%lld", &N[t], &K[t]); } ans.assign(T, 0); maxK = *max_element(K.begin(), K.end()); if (maxK <= 100'000) { small::run(); } else { large::run(); } for (int t = 0; t < T; ++t) { printf("%u\n", ans[t].x); } } return 0; }