#pragma region competitive_programming #ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #pragma GCC optimize("Ofast") #include //#include //#include "Rollback_dsu.hpp" //#include "Partial_Persistent_DSU.hpp" //#include "Number_Theory.hpp" #include #include #include #ifdef _MSC_VER #include #endif #include #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } struct barrett { unsigned int _m; unsigned long long im; explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} unsigned int umod() const { return _m; } unsigned int mul(unsigned int a, unsigned int b) const { 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 int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; 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; } 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); constexpr std::pair inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; 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 auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } 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); 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; n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder #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 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 tomo0608 { std::istream& operator>>(std::istream& is, atcoder::modint998244353& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint998244353& a) { return os << a.val(); } std::istream& operator>>(std::istream& is, atcoder::modint1000000007& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint1000000007& a) { return os << a.val(); } template std::istream& operator>>(std::istream& is, atcoder::static_modint& a) { long long v; is >> v; a = v; return is; } template std::ostream& operator<<(std::ostream& os, const atcoder::static_modint& a) { return os << a.val(); } template std::istream& operator>>(std::istream& is, atcoder::dynamic_modint& a) { long long v; is >> v; a = v; return is; } template std::ostream& operator<<(std::ostream& os, const atcoder::dynamic_modint& a) { return os << a.val(); } // Binomial Coefficient of modint template struct BiCoef { std::vector fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].mod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr mint com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr mint perm(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[n - k]; } constexpr mint homo(int n, int r) { // The number of cases where k indistinguishable balls are put into n distinct boxes if (n < 0 || r < 0)return 0; return r == 0 ? 1 : com(n + r - 1, r); } constexpr mint second_stirling_number(int n, int r) { // The number of cases where n distinct balls are put into k indistinguishable boxes, with at least one ball in each box mint ret = 0; for (int i = 0; i <= r; i++) { mint tmp = com(r, i) * mint(i).pow(n); ret += ((r - i) & 1) ? -tmp : tmp; } return ret * finv_[r]; } constexpr mint bell_number(int n, int r) { // The number of cases where n distinct balls are put into k indistinguishable boxes if (n == 0) return 1; r = std::min(r, n); std::vector pref(r + 1); pref[0] = 1; for (int i = 1; i <= r; i++) { if (i & 1) { pref[i] = pref[i - 1] - finv_[i]; } else { pref[i] = pref[i - 1] + finv_[i]; } } mint ret = 0; for (int i = 1; i <= r; i++) ret += mint(i).pow(n) * fact_[i] * pref[r - i]; return ret; } constexpr mint fact(int n) const noexcept { if (n < 0)return 0; return fact_[n]; } constexpr mint inv(int n) const noexcept { if (n < 0)return 0; return inv_[n]; } constexpr mint finv(int n) const noexcept { if (n < 0)return 0; return finv_[n]; } inline mint operator()(int n, int k) { return com(n, k); } constexpr mint com_naive(long long n, long long k) { if (n < k || n < 0 || k < 0)return 0; mint res = 1; k = std::min(k, n - k); for (int i = 1; i <= k; i++)res *= inv(i) * (n--); return res; } }; } // namespace tomo0608 //typedef atcoder::modint1000000007 mint; typedef atcoder::modint998244353 mint; //#include "Matrix.hpp" //#include #include #include #include #include #include #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { template , internal::is_static_modint_t* = nullptr> struct fft_info { static constexpr int rank2 = bsf_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::ceil_pow2(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[bsf(~(unsigned int)(s))]; } len++; } else { 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[bsf(~(unsigned int)(s))]; } len += 2; } } } template * = nullptr> void butterfly_inv(std::vector& a) { int n = int(a.size()); int h = internal::ceil_pow2(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[bsf(~(unsigned int)(s))]; } len--; } else { 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[bsf(~(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 = 1 << internal::ceil_pow2(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 {}; 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 {}; 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; 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(move(a2), 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; 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; 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 namespace tomo0608 { template struct Formal_Power_Series : public std::vector { using FPS = Formal_Power_Series; using std::vector::vector; using std::vector::operator=; void shrink() { while (this->size() && this->back() == mint(0))this->pop_back(); } FPS& operator+=(const FPS& rhs) { if (rhs.size() > this->size())this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size();i++)(*this)[i] += rhs[i]; return *this; } FPS& operator-=(const FPS& rhs) { if (rhs.size() > this->size())this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size();i++)(*this)[i] -= rhs[i]; return *this; } FPS& operator+=(const mint& rhs) { if (this->empty())this->resize(1); (*this)[0] += rhs; return *this; } FPS& operator-=(const mint& rhs) { if (this->empty())this->resize(1); (*this)[0] -= rhs; return *this; } FPS& operator*=(const mint& rhs) { for (auto& e : *this)e *= rhs; return *this; } FPS& operator*=(const FPS& rhs) { *this = atcoder::convolution((*this), rhs); return *this; } FPS& operator/=(const mint& c) { assert(c != mint(0)); *this *= c.inv(); return *this; } FPS& operator/=(const FPS& rhs) { *this *= rhs.inv(); this->resize(rhs.size()); return *this; } friend FPS operator+(const FPS& lhs, const FPS& rhs) { return FPS(lhs) += rhs; } friend FPS operator+(const FPS& lhs, const mint& rhs) { return FPS(lhs) += rhs; } friend FPS operator-(const FPS& lhs, const FPS& rhs) { return FPS(lhs) -= rhs; } friend FPS operator-(const FPS& lhs, const mint& rhs) { return FPS(lhs) -= rhs; } friend FPS operator*(const FPS& lhs, const FPS& rhs) { return FPS(lhs) *= rhs; } friend FPS operator*(const FPS& lhs, const mint& rhs) { return FPS(lhs) *= rhs; } friend FPS operator/(const FPS& lhs, const mint& rhs) { return FPS(lhs) /= rhs; } // friend std::ostream& operator<<(std::ostream& os, FPS& f) { // for (int i = 0; i < f.size(); i++) { // os << f[i] << (i + 1 == f.size() ? "\n" : " "); // } // return (os); // } FPS inv(int deg = -1) const { assert(this->front() != mint(0)); if (deg == -1)deg = this->size(); FPS res = { this->front().inv() }; for (int d = 1; d < deg; d <<= 1) { FPS f(2 * d), g(2 * d); for (int j = 0; j < std::min((int)this->size(), 2 * d); j++)f[j] = (*this)[j]; for (int j = 0; j < d; j++)g[j] = res[j]; atcoder::internal::butterfly(f); atcoder::internal::butterfly(g); for (int j = 0; j < 2 * d; j++)f[j] *= g[j]; atcoder::internal::butterfly_inv(f); f /= 2 * d; for (int j = 0; j < d; j++) { f[j] = f[j + d]; f[j + d] = 0; } atcoder::internal::butterfly(f); for (int j = 0; j < 2 * d; j++)f[j] *= -g[j]; atcoder::internal::butterfly_inv(f); f /= 2 * d; for (int j = 0; j < d; j++)res.emplace_back(f[j]); } res.resize(deg); return res; } FPS operator-() const { FPS res(*this); for (auto& e : res) e = -e; return res; } FPS derivative() const { FPS res(this->begin() + 1, this->end()); res.emplace_back(0); for (int j = 0; j < res.size(); j++)res[j] *= (j + 1); return res; } FPS integral(bool truncate = true) const { FPS res(this->size() + 1 - truncate, 0); for (int j = 0; j < this->size() - truncate; j++)res[j + 1] = (*this)[j] / (j + 1); return res; } FPS log() const { FPS f = this->derivative(); f /= (*this); return f.integral(); } // https://arxiv.org/pdf/1301.5804.pdf FPS exp(int deg = -1) const { assert(this->front() == mint(0)); if (deg == -1)deg = this->size(); FPS f = { 1 }, g = { 1 }; for (int d = 1; d < deg; d <<= 1) { FPS f_next(f); f_next.resize(2 * d, 0); atcoder::internal::butterfly(f_next); g.resize(2 * d, 0); atcoder::internal::butterfly(g); for (int j = 0; j < 2 * d; j++)g[j] = 2 * g[j] - f_next[j] * g[j] * g[j]; atcoder::internal::butterfly_inv(g); g /= 2 * d; g.resize(d); FPS q(2 * d); for (int j = 0; j < d && j < this->size() - 1; j++)q[j] = (*this)[j + 1] * (j + 1); FPS w(2 * d); FPS G(g); G.resize(2 * d, 0); atcoder::internal::butterfly(G); for (int j = 0; j < 2 * d; j++)w[j] = f_next[j] * G[j]; atcoder::internal::butterfly_inv(w); w /= 2 * d; for (int j = 0; j < d; j++) { w[j] = w[j + d]; w[j + d] = 0; } atcoder::internal::butterfly(w); atcoder::internal::butterfly(q); for (int j = 0; j < 2 * d; j++)w[j] *= q[j]; atcoder::internal::butterfly_inv(w); w /= 2 * d; FPS df(f.derivative()); df.resize(2 * d, 0); atcoder::internal::butterfly(df); for (int j = 0;j < 2 * d;j++)df[j] *= G[j]; atcoder::internal::butterfly_inv(df); df /= 2 * d; for (int j = 0; j < d;j++) { w[j + d] = w[j]; w[j] = 0; } w -= df; w = w.integral(); for (int j = 0; j < 2 * d && j < this->size(); j++)w[j] += (*this)[j]; for (int j = 0; j < d;j++) { w[j] = w[j + d]; w[j + d] = 0; } atcoder::internal::butterfly(w); for (int j = 0; j < 2 * d; j++)f_next[j] *= w[j]; atcoder::internal::butterfly_inv(f_next); f_next /= 2 * d; f.resize(2 * d, 0); for (int j = 0; j < d; j++)f[j + d] = f_next[j]; } f.resize(deg); return f; } FPS pow(long long m) const { int n = this->size(); if (m == 0) { auto res = FPS(n, 0); res[0] = 1; return res; } int l = std::find_if(this->begin(), this->end(), [](mint x) {return x != mint(0);}) - this->begin(); if (l == this->size() || (l && m >= (n + l - 1) / l))return FPS(n, 0); FPS res(this->begin() + l, this->end()); mint c = (*this)[l]; res /= c; res.resize(n, 0); res = (res.log() * mint(m)).exp(); res.erase(res.begin() + (n - m * l), res.end()); res *= c.pow(m); std::reverse(res.begin(), res.end()); res.resize(n, 0); std::reverse(res.begin(), res.end()); return res; } }; // for sparse fps template Formal_Power_Series positive_unit_fractions(int n) { // res[i] = 1 / i, res[0] = 0 length: n+1 static const int mod = mint::mod(); static Formal_Power_Series res = { 0, 1 }; assert(0 < n); if (n >= mod) n -= mod; while (int(res.size()) <= n) { int num = res.size(); int q = (mod + num - 1) / num; res.emplace_back(res[num * q - mod] * mint(q)); } return res; } template std::vector> compress_fps(const Formal_Power_Series& f) { int n = f.size(); std::vector> cf; for (int i = 0; i < n; i++) { if (f[i] != 0)cf.emplace_back(i, f[i]); } return cf; } template Formal_Power_Series mul_sparse(const Formal_Power_Series& f, const Formal_Power_Series& g) { int n = f.size(), m = g.size(); auto cf = compress_fps(f), cg = compress_fps(g); Formal_Power_Series h(n + m - 1); for (auto [i, p] : cf)for (auto [j, q] : cg)h[i + j] += p * q; return h; } template Formal_Power_Series inv_sparse(const Formal_Power_Series& f, int deg = -1) { assert(f[0] != 0); if (deg == -1)deg = f.size(); auto cf = compress_fps(f); Formal_Power_Series f_inv(deg); f_inv[0] = f[0].inv(); for (int i = 1; i < deg; i++) { for (auto [k, p] : cf) { if (i - k < 0)break; f_inv[i] -= f_inv[i - k] * p; } f_inv[i] *= f_inv[0]; } return f_inv; } template Formal_Power_Series exp_sparse(const Formal_Power_Series& f, int deg = -1) { assert(f[0] == 0); if (deg == -1)deg = f.size(); auto cf = compress_fps(f); Formal_Power_Series f_exp(deg); Formal_Power_Series inv_num = positive_unit_fractions(deg); f_exp[0] = 1; for (int i = 1; i < deg; i++) { for (auto [k, p] : cf) { if (i - k < 0)break; f_exp[i] += f_exp[i - k] * p * k; } f_exp[i] *= inv_num[i]; } return f_exp; } template Formal_Power_Series log_sparse(const Formal_Power_Series& f, int deg = -1) { assert(f[0] == 1); if (deg == -1)deg = f.size(); Formal_Power_Series df = f.derivative(); Formal_Power_Series df_log = mul_sparse(df, inv_sparse(f)); Formal_Power_Series f_log = df_log.integral(); f_log.resize(deg); return f_log; } template Formal_Power_Series __pow_sparse_const_1(const Formal_Power_Series& f, long long k, int deg) { int n = f.size(); assert(n > 0 && f[0] == 1); auto cf = compress_fps(f); Formal_Power_Series f_pow_k(deg); Formal_Power_Series inv_num = positive_unit_fractions(deg); f_pow_k[0] = 1; for (int i = 1; i < deg; i++) { for (const auto& [j, coef] : cf) { if (i - j < 0) break; f_pow_k[i] += (mint(k) * mint(j) - mint(i - j)) * coef * f_pow_k[i - j]; } f_pow_k[i] *= inv_num[i]; } return f_pow_k; } template Formal_Power_Series pow_sparse(const Formal_Power_Series& f, long long k, int deg = -1) { int n = f.size(); if (deg < 0)deg = n; assert(k >= 0); if (k == 0) { Formal_Power_Series res(deg, 0); if (deg)res[0] = 1; return res; } int l = std::find_if(f.begin(), f.end(), [](mint x) {return x != mint(0);}) - f.begin(); if (l == f.size() || (l && k >= (deg + l - 1) / l))return Formal_Power_Series(deg, 0); Formal_Power_Series res(f.begin() + l, f.end()); mint c = f[l]; res /= c; res.resize(deg, 0); res = __pow_sparse_const_1(res, k, deg); res.erase(res.begin() + (deg - k * l), res.end()); res *= c.pow(k); std::reverse(res.begin(), res.end()); res.resize(deg, 0); std::reverse(res.begin(), res.end()); return res; } } // namespace tomo0608 //#include "Bit_Convolution.hpp" //#include //#include //#include "Primal_Dual.hpp" //#include "maxflow_mincap.hpp" //#include //#include //#include //#include "2D_Segment_Tree.hpp" //#include "DisjointSparseTable.hpp" //#include "SWAG.hpp" //#include "Mo_algorithm.hpp" //#include "Heavy_Light_Decomposition.hpp" //#include "Binary_Trie.hpp" //#include "LCT.hpp" //#include "Slope_Trick.hpp" //#include //#include //#include "TwoEdgeCC.hpp" namespace tomo0608 { typedef long long ll; typedef long double ld; template using V = std::vector; template using VV = V>; template using VVV = V>; typedef std::pair pii; typedef std::pair pll; templatevoid input(T&... a) { (std::cin >> ... >> a); }; #define INT(...) int __VA_ARGS__; IN(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; IN(__VA_ARGS__) #define STR(...) string __VA_ARGS__; IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__) #define VEC(type, name, size) std::vector name(size);IN(name) #define VECVEC(type, name, h, w) std::vector> name(h, std::vector(w));IN(name) template std::istream& operator>>(std::istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template std::ostream& operator<<(std::ostream& os, const std::pair& p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& e : v) is >> e; return is; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (auto& e : v) os << e << ' '; return os; } template std::ostream& operator << (std::ostream& os, std::set& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr;++itr;if (itr != set_var.end()) os << ", ";itr--; }os << "}";return os; } template std::ostream& operator<<(std::ostream& os, std::map& map_var) { os << "{";for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--; }os << "}";return os; } void IN() {} template void IN(Head& head, Tail &...tail) { std::cin >> head; IN(tail...); } void print() { std::cout << '\n'; } templatevoid print(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; } void drop() { std::cout << '\n';exit(0); } templatevoid drop(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n';exit(0); } #ifdef __LOCAL void debug_out() { std::cerr << std::endl; } template < class Head, class... Tail> void debug_out(Head H, Tail... T) { std::cerr << ' ' << H; debug_out(T...); } #define debug(...) std::cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) std::cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << std::endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #define rep1(a) for(long long i = 0; i < a; i++) #define rep2(i, a) for(long long i = 0; i < a; i++) #define rep3(i, a, b) for(long long i = a; i < b; i++) #define rep4(i, a, b, c) for(long long i = a; i < b; i += c) #define drep1(a) for(long long i = a-1; i >= 0; i--) #define drep2(i, a) for(long long i = a-1; i >= 0; i--) #define drep3(i, a, b) for(long long i = a-1; i >= b; i--) #define drep4(i, a, b, c) for(long long i = a-1; i >= b; i -= c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define drep(...) overload4(__VA_ARGS__, drep4, drep3, drep2, drep1)(__VA_ARGS__) #define endl '\n' } // namespace tomo0608 namespace tomo0608 { #define ALL(x) x.begin(),x.end() template T SUM(const S& v) { return accumulate(ALL(v), T(0)); } #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define RSORT(v) sort(ALL(v)); reverse(ALL(v)) #define UNIQUE(x) SORT(x), x.erase(unique(ALL(x)), x.end()) #define lb(c, x) distance((c).begin(), lower_bound(ALL(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(ALL(c), (x))) template void zip(std::vector& x) { std::vector y = x;UNIQUE(y);for (int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template using priority_queue_rev = std::priority_queue, std::greater>; template inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return 1; } return 0; } template inline int count_between(std::vector& a, T l, T r) { return lower_bound(ALL(a), r) - lower_bound(ALL(a), l); } // [l, r) #define bittest(n, k) (((n) >> (k)) & 1) int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } #define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(ALL(v)));) template T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } } using namespace atcoder; using namespace std; using namespace tomo0608; int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }; int dy[8] = { 0, 1, 0, -1, 1, -1, -1, 1 }; // インタラクティブ問題のときは出力するたびにcout.flush();を忘れない!!!!! void solve(); int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::setprecision(20); int codeforces = 1; //cin >> codeforces; while (codeforces--) { solve(); } return 0; } #pragma endregion const int m2 = 999630629; typedef Formal_Power_Series fps; void solve() { INT(n); VEC(ll, a, n); ll sum_a = SUM(a); // sum_a < 2 * m2 mint ans = sum_a * mint(2).pow(n-1); if(sum_a < m2)drop(ans); // \sum_{i \in S}a_i >= m2となるようなSの個数を求める // \sum_{i \in T}a_i <= sum_a - m2となるようなTの個数を求めればよい ll sz = sum_a - m2 + 1; fps sum_log(sz); BiCoef bc(sz); rep(i, n){ // log(1 + x^a_i)を求める for(int j = a[i], num = 1, c = 1; j < sz; j += a[i], num++, c *= -1){ sum_log[j] += bc.inv(num) * c; } } fps prd = sum_log.exp(); mint cnt = mint(2).pow(n); rep(i, sz)cnt -= prd[i]; print(ans - cnt * m2); }