#line 1 "sol.cpp" #line 1 "library/Template/template.hpp" #include using namespace std; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define SZ(v) (int)v.size() #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin()) #define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin()) using ll = long long int; using ull = unsigned long long; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template T ceil(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? x / y : (x - y + 1) / y); } template int popcnt(T x) { return __builtin_popcountll(x); } template int topbit(T x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } template int lowbit(T x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } #line 2 "library/Utility/fastio.hpp" #include class FastIO { static constexpr int L = 1 << 16; char rdbuf[L]; int rdLeft = 0, rdRight = 0; inline void reload() { int len = rdRight - rdLeft; memmove(rdbuf, rdbuf + rdLeft, len); rdLeft = 0, rdRight = len; rdRight += fread(rdbuf + len, 1, L - len, stdin); } inline bool skip() { for (;;) { while (rdLeft != rdRight and rdbuf[rdLeft] <= ' ') rdLeft++; if (rdLeft == rdRight) { reload(); if (rdLeft == rdRight) return false; } else break; } return true; } template ::value, int> = 0> inline bool _read(T &x) { if (!skip()) return false; if (rdLeft + 20 >= rdRight) reload(); bool neg = false; if (rdbuf[rdLeft] == '-') { neg = true; rdLeft++; } x = 0; while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) { x = x * 10 + (neg ? -(rdbuf[rdLeft++] ^ 48) : (rdbuf[rdLeft++] ^ 48)); } return true; } inline bool _read(__int128_t &x) { if (!skip()) return false; if (rdLeft + 40 >= rdRight) reload(); bool neg = false; if (rdbuf[rdLeft] == '-') { neg = true; rdLeft++; } x = 0; while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) { x = x * 10 + (neg ? -(rdbuf[rdLeft++] ^ 48) : (rdbuf[rdLeft++] ^ 48)); } return true; } inline bool _read(__uint128_t &x) { if (!skip()) return false; if (rdLeft + 40 >= rdRight) reload(); x = 0; while (rdbuf[rdLeft] >= '0' and rdLeft < rdRight) { x = x * 10 + (rdbuf[rdLeft++] ^ 48); } return true; } template ::value, int> = 0> inline bool _read(T &x) { if (!skip()) return false; if (rdLeft + 20 >= rdRight) reload(); bool neg = false; if (rdbuf[rdLeft] == '-') { neg = true; rdLeft++; } x = 0; while (rdbuf[rdLeft] >= '0' and rdbuf[rdLeft] <= '9' and rdLeft < rdRight) { x = x * 10 + (rdbuf[rdLeft++] ^ 48); } if (rdbuf[rdLeft] != '.') return true; rdLeft++; T base = .1; while (rdbuf[rdLeft] >= '0' and rdbuf[rdLeft] <= '9' and rdLeft < rdRight) { x += base * (rdbuf[rdLeft++] ^ 48); base *= .1; } if (neg) x = -x; return true; } inline bool _read(char &x) { if (!skip()) return false; if (rdLeft + 1 >= rdRight) reload(); x = rdbuf[rdLeft++]; return true; } inline bool _read(string &x) { if (!skip()) return false; for (;;) { int pos = rdLeft; while (pos < rdRight and rdbuf[pos] > ' ') pos++; x.append(rdbuf + rdLeft, pos - rdLeft); if (rdLeft == pos) break; rdLeft = pos; if (rdLeft == rdRight) reload(); else break; } return true; } template inline bool _read(vector &v) { for (auto &x : v) { if (!_read(x)) return false; } return true; } char wtbuf[L], tmp[50]; int wtRight = 0; inline void _write(const char &x) { if (wtRight > L - 32) flush(); wtbuf[wtRight++] = x; } inline void _write(const string &x) { for (auto &c : x) _write(c); } template ::value, int> = 0> inline void _write(T x) { if (wtRight > L - 32) flush(); if (x == 0) { _write('0'); return; } else if (x < 0) { _write('-'); if (__builtin_expect(x == std::numeric_limits::min(), 0)) { switch (sizeof(x)) { case 2: _write("32768"); return; case 4: _write("2147483648"); return; case 8: _write("9223372036854775808"); return; } } x = -x; } int pos = 0; while (x != 0) { tmp[pos++] = char((x % 10) | 48); x /= 10; } rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i]; wtRight += pos; } inline void _write(__int128_t x) { if (wtRight > L - 40) flush(); if (x == 0) { _write('0'); return; } else if (x < 0) { _write('-'); x = -x; } int pos = 0; while (x != 0) { tmp[pos++] = char((x % 10) | 48); x /= 10; } rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i]; wtRight += pos; } inline void _write(__uint128_t x) { if (wtRight > L - 40) flush(); if (x == 0) { _write('0'); return; } int pos = 0; while (x != 0) { tmp[pos++] = char((x % 10) | 48); x /= 10; } rep(i, 0, pos) wtbuf[wtRight + i] = tmp[pos - 1 - i]; wtRight += pos; } inline void _write(double x) { ostringstream oss; oss << fixed << setprecision(15) << double(x); string s = oss.str(); _write(s); } template inline void _write(const vector &v) { rep(i, 0, v.size()) { if (i) _write(' '); _write(v[i]); } } public: FastIO() {} ~FastIO() { flush(); } inline void read() {} template inline void read(Head &head, Tail &...tail) { assert(_read(head)); read(tail...); } template inline void write() { if (ln) _write('\n'); } template inline void write(const Head &head, const Tail &...tail) { _write(head); write(tail...); if (space) _write(' '); } inline void flush() { fwrite(wtbuf, 1, wtRight, stdout); wtRight = 0; } }; /** * @brief Fast IO */ #line 310 "sol.cpp" #ifndef ATCODER_CONVOLUTION_HPP #define ATCODER_CONVOLUTION_HPP 1 #include #include #include #include #include #ifndef ATCODER_INTERNAL_BITOP_HPP #define ATCODER_INTERNAL_BITOP_HPP 1 #ifdef _MSC_VER #include #endif #if __cplusplus >= 202002L #include #endif namespace atcoder { namespace internal { #if __cplusplus >= 202002L using std::bit_ceil; #else // @return same with std::bit::bit_ceil unsigned int bit_ceil(unsigned int n) { unsigned int x = 1; while (x < (unsigned int)(n)) x *= 2; return x; } #endif // @param n `1 <= n` // @return same with std::bit::countr_zero int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } // @param n `1 <= n` // @return same with std::bit::countr_zero constexpr int countr_zero_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_BITOP_HPP#ifndef ATCODER_MODINT_HPP #define ATCODER_MODINT_HPP 1 #include #include #include #ifdef _MSC_VER #include #endif #ifndef ATCODER_INTERNAL_MATH_HPP #define ATCODER_INTERNAL_MATH_HPP 1 #include #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast modular multiplication by barrett reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m` explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) // < 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned long long y = x * _m; return (unsigned int)(z - y + (z < y ? _m : 0)); } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template constexpr int primitive_root = primitive_root_constexpr(m); // @param n `n < 2^32` // @param m `1 <= m < 2^32` // @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64) unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; // y_max < m * (n + 1) // floor(y_max / m) <= n n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_MATH_HPP#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP #define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1 #include #include #include namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_TYPE_TRAITS_HPP namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; } // namespace internal template * = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template * = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template * = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime; }; template struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template * = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template * = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator-=(const mint &rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint &operator*=(const mint &rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template internal::barrett dynamic_modint::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template using is_static_modint = std::is_base_of; template using is_static_modint_t = std::enable_if_t::value>; template struct is_dynamic_modint : public std::false_type {}; template struct is_dynamic_modint> : public std::true_type {}; template using is_dynamic_modint_t = std::enable_if_t::value>; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { template , internal::is_static_modint_t * = nullptr> struct fft_info { static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1); std::array root; // root[i]^(2^i) == 1 std::array iroot; // root[i] * iroot[i] == 1 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]; } } { mint 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 * = nullptr> void butterfly(std::vector &a) { int n = int(a.size()); int h = internal::countr_zero((unsigned int)n); static const fft_info info; int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len < h) { if (h - len == 1) { int p = 1 << (h - len - 1); mint rot = 1; for (int s = 0; s < (1 << len); s++) { int offset = s << (h - len); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * rot; a[i + offset] = l + r; a[i + offset + p] = l - r; } if (s + 1 != (1 << len)) rot *= info.rate2[countr_zero(~(unsigned int)(s))]; } len++; } else { // 4-base int p = 1 << (h - len - 2); mint rot = 1, imag = info.root[2]; for (int s = 0; s < (1 << len); s++) { mint rot2 = rot * rot; mint rot3 = rot2 * rot; int offset = s << (h - len); for (int i = 0; i < p; i++) { auto mod2 = 1ULL * mint::mod() * mint::mod(); auto a0 = 1ULL * a[i + offset].val(); auto a1 = 1ULL * a[i + offset + p].val() * rot.val(); auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val(); auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val(); auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val(); auto na2 = mod2 - a2; a[i + offset] = a0 + a2 + a1 + a3; a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3)); a[i + offset + 2 * p] = a0 + na2 + a1na3imag; a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag); } if (s + 1 != (1 << len)) rot *= info.rate3[countr_zero(~(unsigned int)(s))]; } len += 2; } } } template * = nullptr> void butterfly_inv(std::vector &a) { int n = int(a.size()); int h = internal::countr_zero((unsigned int)n); static const fft_info info; int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed 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 < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * irot.val(); ; } if (s + 1 != (1 << (len - 1))) irot *= info.irate2[countr_zero(~(unsigned int)(s))]; } len--; } else { // 4-base int p = 1 << (h - len); mint irot = 1, iimag = info.iroot[2]; for (int s = 0; s < (1 << (len - 2)); s++) { mint irot2 = irot * irot; mint irot3 = irot2 * irot; int offset = s << (h - len + 2); for (int i = 0; i < p; i++) { auto a0 = 1ULL * a[i + offset + 0 * p].val(); auto a1 = 1ULL * a[i + offset + 1 * p].val(); auto a2 = 1ULL * a[i + offset + 2 * p].val(); auto a3 = 1ULL * a[i + offset + 3 * p].val(); auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val(); a[i + offset] = a0 + a1 + a2 + a3; a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val(); a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val(); a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val(); } if (s + 1 != (1 << (len - 2))) irot *= info.irate3[countr_zero(~(unsigned int)(s))]; } len -= 2; } } } template * = nullptr> std::vector convolution_naive(const std::vector &a, const std::vector &b) { int n = int(a.size()), m = int(b.size()); std::vector ans(n + m - 1); if (n < m) { for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { ans[i + j] += a[i] * b[j]; } } } else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } } return ans; } template * = nullptr> std::vector convolution_fft(std::vector a, std::vector b) { int n = int(a.size()), m = int(b.size()); int z = (int)internal::bit_ceil((unsigned int)(n + m - 1)); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } } // namespace internal template * = nullptr> std::vector convolution(std::vector &&a, std::vector &&b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; int z = (int)internal::bit_ceil((unsigned int)(n + m - 1)); assert((mint::mod() - 1) % z == 0); if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template * = nullptr> std::vector convolution(const std::vector &a, const std::vector &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; int z = (int)internal::bit_ceil((unsigned int)(n + m - 1)); assert((mint::mod() - 1) % z == 0); if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template ::value> * = nullptr> std::vector convolution(const std::vector &a, const std::vector &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint; int z = (int)internal::bit_ceil((unsigned int)(n + m - 1)); assert((mint::mod() - 1) % z == 0); std::vector a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(std::move(a2), std::move(b2)); std::vector c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector convolution_ll(const std::vector &a, const std::vector &b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; static constexpr int MAX_AB_BIT = 24; static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24."); static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24."); static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24."); assert(n + m - 1 <= (1 << MAX_AB_BIT)); auto c1 = convolution(a, b); auto c2 = convolution(a, b); auto c3 = convolution(a, b); std::vector c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; // B = 2^63, -B <= x, r(real value) < B // (x, x - M, x - 2M, or x - 3M) = r (mod 2B) // r = c1[i] (mod MOD1) // focus on MOD1 // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B) // r = x, // x - M' + (0 or 2B), // x - 2M' + (0, 2B or 4B), // x - 3M' + (0, 2B, 4B or 6B) (without mod!) // (r - x) = 0, (0) // - M' + (0 or 2B), (1) // -2M' + (0 or 2B or 4B), (2) // -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1) // we checked that // ((1) mod MOD1) mod 5 = 2 // ((2) mod MOD1) mod 5 = 3 // ((3) mod MOD1) mod 5 = 4 long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder const int DIGIT = 6; const int BASE = 1000000; struct positive_bigint { std::vector d; positive_bigint() {} positive_bigint(long long X) { while (X > 0) { d.push_back(X % BASE); X /= BASE; } } positive_bigint(std::string S) { if (S == "0") { S = ""; } int L = S.size(); d.resize((L + DIGIT - 1) / DIGIT, 0); for (int i = L - 1; i >= 0; i -= 6) { for (int j = std::max(i - 5, 0); j <= i; j++) { d[i / DIGIT] *= 10; d[i / DIGIT] += S[j] - '0'; } } std::reverse(d.begin(), d.end()); } bool empty() const { return d.empty(); } int size() const { return d.size(); } int &operator[](int i) { return d[i]; } int operator[](int i) const { return d[i]; } }; std::string to_string(const positive_bigint &A) { int N = A.size(); std::string ans; for (int i = N - 1; i >= 0; i--) { std::string tmp = std::to_string(A[i]); if (i < N - 1) { ans += std::string(DIGIT - tmp.size(), '0'); } ans += tmp; } if (ans.empty()) { ans = "0"; } return ans; } std::istream &operator>>(std::istream &is, positive_bigint &A) { std::string S; is >> S; A = positive_bigint(S); return is; } std::ostream &operator<<(std::ostream &os, positive_bigint &A) { os << to_string(A); return os; } int cmp(const positive_bigint &A, const positive_bigint &B) { int N = A.size(); int M = B.size(); if (N < M) { return -1; } else if (N > M) { return 1; } else { for (int i = N - 1; i >= 0; i--) { if (A[i] < B[i]) { return -1; } if (A[i] > B[i]) { return 1; } } return 0; } } bool operator==(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) == 0; } bool operator!=(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) != 0; } bool operator<(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) < 0; } bool operator>(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) > 0; } bool operator<=(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) <= 0; } bool operator>=(const positive_bigint &A, const positive_bigint &B) { return cmp(A, B) >= 0; } positive_bigint &operator+=(positive_bigint &A, const positive_bigint &B) { int N = A.size(); int M = B.size(); while (N < M) { A.d.push_back(0); N++; } for (int i = 0; i < M; i++) { A[i] += B[i]; } for (int i = 0; i < N - 1; i++) { if (A[i] >= BASE) { A[i] -= BASE; A[i + 1]++; } } if (N > 0) { if (A[N - 1] >= BASE) { A.d.push_back(1); A[N - 1] -= BASE; } } return A; } positive_bigint operator+(const positive_bigint &A, const positive_bigint &B) { positive_bigint A2 = A; A2 += B; return A2; } positive_bigint &operator-=(positive_bigint &A, const positive_bigint &B) { int N = A.size(); int M = B.size(); for (int i = 0; i < M; i++) { A[i] -= B[i]; } for (int i = 0; i < N - 1; i++) { if (A[i] < 0) { A[i] += BASE; A[i + 1]--; } } while (!A.empty()) { if (A.d.back() == 0) { A.d.pop_back(); } else { break; } } return A; } positive_bigint operator-(const positive_bigint &A, const positive_bigint &B) { positive_bigint A2 = A; A2 -= B; return A2; } positive_bigint operator*(const positive_bigint &A, const positive_bigint &B) { if (A.empty() || B.empty()) { return 0; } int N = A.size(); int M = B.size(); std::vector a(N); for (int i = 0; i < N; i++) { a[i] = A[i]; } std::vector b(M); for (int i = 0; i < M; i++) { b[i] = B[i]; } std::vector C = atcoder::convolution_ll(a, b); for (int i = 0; i < N + M - 2; i++) { C[i + 1] += C[i] / BASE; C[i] %= BASE; } if (C[N + M - 2] >= BASE) { C.resize(N + M); C[N + M - 1] += C[N + M - 2] / BASE; C[N + M - 2] %= BASE; } positive_bigint ans; ans.d.resize(C.size()); for (int i = 0; i < C.size(); i++) { ans[i] = C[i]; } return ans; } positive_bigint operator*=(positive_bigint &A, const positive_bigint &B) { A = A * B; return A; } struct bigint { bool neg = false; positive_bigint a; bigint() {} bigint(long long X) : neg(X < 0), a(abs(X)) {} bigint(const positive_bigint &X, bool neg = false) : neg(neg), a(X) {} bigint(const std::string &s) { if (!s.empty()) { if (s[0] == '-') { neg = true; a = positive_bigint(s.substr(1, s.size() - 1)); } else { a = positive_bigint(s); } } } bool empty() const { return a.empty(); } int size() const { return a.size(); } int &operator[](int i) { return a[i]; } }; std::string to_string(const bigint &A) { std::string ans; if (A.neg) { ans += '-'; } ans += to_string(A.a); return ans; } std::istream &operator>>(std::istream &is, bigint &A) { std::string S; is >> S; if (S != "0") { A = bigint(S); } return is; } std::ostream &operator<<(std::ostream &os, bigint A) { os << to_string(A); return os; } positive_bigint abs(const bigint &A) { return A.a; } int cmp(const bigint &A, const bigint &B) { if (!A.neg) { if (!B.neg) { return cmp(A.a, B.a); } else { return 1; } } else { if (!B.neg) { return -1; } else { return cmp(B.a, A.a); } } } bool operator==(const bigint &A, const bigint &B) { return cmp(A, B) == 0; } bool operator!=(const bigint &A, const bigint &B) { return cmp(A, B) != 0; } bool operator<(const bigint &A, const bigint &B) { return cmp(A, B) < 0; } bool operator>(const bigint &A, const bigint &B) { return cmp(A, B) > 0; } bool operator<=(const bigint &A, const bigint &B) { return cmp(A, B) <= 0; } bool operator>=(const bigint &A, const bigint &B) { return cmp(A, B) >= 0; } bigint operator+(const bigint &A) { return A; } bigint operator-(const bigint &A) { bigint A2 = A; if (!A2.empty()) { A2.neg = !A2.neg; } return A2; } bigint &operator+=(bigint &A, const bigint &B) { if (A.neg == B.neg) { A.a += B.a; } else { int c = cmp(A.a, B.a); if (c > 0) { A.a -= B.a; } else if (c < 0) { A.a = B.a - A.a; A.neg = !A.neg; } else { A = 0; } } return A; } bigint operator+(const bigint &A, const bigint &B) { bigint A2 = A; A2 += B; return A2; } bigint &operator-=(bigint &A, const bigint &B) { if (A.neg != B.neg) { A.a += B.a; } else { int c = cmp(A.a, B.a); if (c > 0) { A.a -= B.a; } else if (c < 0) { A.a = B.a - A.a; A.neg = !A.neg; } else { A = 0; } } return A; } bigint operator-(const bigint &A, const bigint &B) { bigint A2 = A; A2 -= B; return A2; } bigint operator*=(bigint &A, const bigint &B) { if (A.empty() || B.empty()) { A = 0; } else { if (B.neg) { A.neg = !A.neg; } A.a *= B.a; } return A; } bigint operator*(const bigint &A, const bigint &B) { bigint A2 = A; A2 *= B; return A2; } typedef string::const_iterator State; bigint expr(State &beg); bigint term(State &beg); bigint factor(State &beg); bigint num(State &beg); bigint expr(State &beg) { auto ret = term(beg); for (;;) { if ((*beg) == '+') { beg++; ret += term(beg); } else if ((*beg) == '-') { beg++; ret -= term(beg); } else break; } return ret; } bigint term(State &beg) { deque deq; deq.push_back(factor(beg)); while ((*beg) == '*') { beg++; deq.push_back(factor(beg)); } while (deq.size() > 1) { auto x = deq[0]; deq.pop_front(); auto y = deq[0]; deq.pop_front(); deq.push_back(x * y); } return deq[0]; } bigint factor(State &beg) { if ((*beg) == '(') { beg++; auto ret = expr(beg); beg++; return ret; } else return num(beg); } bigint num(State &beg) { bool neg = 0; while ((*beg) == '-') { neg ^= 1; beg++; } string buf; while (isdigit(*beg)) { buf += (*beg); beg++; } bigint ret(buf); if (neg) ret = -ret; return ret; } FastIO io; int main() { int n = 1000000; string s; io.read(n, s); State it = s.begin(); auto ret = expr(it); cout << ret << '\n'; return 0; }