#define SINGLE_TESTCASE #define FAST_IO #define INF 4'000'000'000'000'000'037LL #define EPS 1e-11 // https://github.com/miscalculation53/library/tree/wip/template/template_all.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_all_but_modint.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_types.hpp #include using namespace std; using ld = decltype(EPS); using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pll = pair; using tlll = tuple; using tllll = tuple; #define vc vector template using vvc = vc>; template using vvvc = vc>>; using vb = vc; using vl = vc; using vpll = vc; using vtlll = vc; using vtllll = vc; using vstr = vc; using vvb = vvc; using vvl = vvc; template using pql = priority_queue, greater>; template using pqg = priority_queue; using i128 = __int128_t; using u128 = __uint128_t; i128 stoi128(const string &s) { const bool neg = s.front() == '-'; u128 res = 0; for (int i = neg; i < (int)s.size(); i++) res = 10 * res + s[i] - '0'; if (neg) return -i128(res - 1) - 1; return i128(res); } string i128tos(i128 x) { if (x == 0) return "0"; string sign = "", res = ""; u128 ux; if (x < 0) ux = u128(-(x + 1)) + 1, sign = "-"; else ux = x; while (ux > 0) { res += '0' + ux % 10; ux /= 10; } reverse(res.begin(), res.end()); return sign + res; } istream &operator>>(istream &is, i128 &a) { string s; is >> s; a = stoi128(s); return is; } ostream &operator<<(ostream &os, const i128 &a) { os << i128tos(a); return os; } #define cauto const auto // https://github.com/miscalculation53/library/tree/wip/template/template_rep.hpp #define overload4(_1,_2,_3,_4,name,...) name #define rep1(i,n) for (ll i = 0, nnnnn = ll(n); i < nnnnn; i++) #define rep2(i,l,r) for (ll i = ll(l), rrrrr = ll(r); i < rrrrr; i++) #define rep3(i,l,r,d) for (ll i = ll(l), rrrrr = ll(r), ddddd = ll(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi1(i,n) for (int i = 0, nnnnn = int(n); i < nnnnn; i++) #define repi2(i,l,r) for (int i = int(l), rrrrr = int(r); i < rrrrr; i++) #define repi3(i,l,r,d) for (int i = int(l), rrrrr = int(r), ddddd = int(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d) #define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__) #define fe(...) for (auto __VA_ARGS__) #define fec(...) for (cauto &__VA_ARGS__) #define fem(...) for (auto &__VA_ARGS__) // https://github.com/miscalculation53/library/tree/wip/template/template_math.hpp // https://github.com/miscalculation53/library/tree/wip/utils/is_integral_ext.hpp template constexpr bool is_integral_ext = is_integral_v || is_same_v || is_same_v; template constexpr bool is_signed_ext = is_signed_v || is_same_v; template constexpr bool is_unsigned_ext = is_unsigned_v || is_same_v; // https://github.com/miscalculation53/library/tree/wip/utils/default_infty.hpp namespace default_infty_detail { template struct has_infty : false_type {}; template struct has_infty> : true_type {}; template inline constexpr bool unsupported = false; } template inline bool chmin(T &a, U b) { return a > b ? a = b, true : false; } template && is_integral_ext>> inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); } template && is_integral_ext>> inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor(a, b); } // https://github.com/miscalculation53/library/tree/wip/template/template_vector.hpp #define ALL(a) (a).begin(), (a).end() template inline T SZ(const V &x) { return x.size(); } #define eb emplace_back #define LMD(x,fx) ([&](const auto &x) { return fx; }) #define GEN_VEC(n,i,fi) (gen_vec(n, LMD(i, fi))) template auto dvec(const V (&sz)[d], const T &init) { if constexpr (i < d) return vc(sz[i], dvec(sz, init)); else return init; } // https://github.com/miscalculation53/library/tree/wip/template/template_algo.hpp // https://github.com/miscalculation53/library/tree/wip/utils/resolved_infty.hpp // https://github.com/miscalculation53/library/tree/wip/utils/resolved_value.hpp template auto SUM(const V &v) { typename V::value_type s{}; fec(vi : v) s += vi; return s; } template vvc top(const vvc &a) { if (a.empty()) return {}; const int n = a.size(), m = a[0].size(); vvc b(m, vc(n)); repi(i, n) { assert(SZ(a[i]) == m); repi(j, m) b[j][i] = a[i][j]; } return b; } vstr top(const vstr &a) { vvc a_(a.size()); repi(i, SZ(a)) a_[i] = {ALL(a[i])}; vvc b_ = top(a_); vstr b(b_.size()); repi(i, SZ(b)) b[i] = {ALL(b_[i])}; return b; } template struct has_e0 : false_type {}; template struct has_e0> : true_type {}; template inline constexpr bool has_e0_v = has_e0::value; template struct MonoidAdd { using S = T; }; template struct MonoidMin { using S = T; }; template struct MonoidMax { using S = T; }; namespace internal { template struct HasMonoidPow : false_type { }; template struct HasMonoidPow(), declval()))>> : true_type { }; } constexpr array DRULgrid = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}}; constexpr array DRULplane = {{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}}; // https://github.com/miscalculation53/library/tree/wip/template/template_binsearch.hpp template struct is_random_access_iterator { static constexpr bool value = is_same_v< typename iterator_traits::iterator_category, random_access_iterator_tag >; }; template constexpr bool is_random_access_iterator_v = is_random_access_iterator::value; #define DEFAULT_COMP ranges::less namespace internal { }; // https://github.com/miscalculation53/library/tree/wip/template/template_bit.hpp template inline constexpr ull MASK(T k) { return (1ULL << k) - 1ULL; } inline constexpr ll bit_width(ll x) { return std::bit_width((ull)x); } inline constexpr ll bit_floor(ll x) { return std::bit_floor((ull)x); } inline constexpr ll bit_ceil(ll x) { return std::bit_ceil((ull)x); } inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); } inline constexpr ll popcount(ll x) { return std::popcount((ull)x); } inline constexpr bool has_single_bit(ll x) { return std::has_single_bit((ull)x); } inline constexpr ull lsb_pos(ull x) { assert(x != 0); return countr_zero(x); } inline constexpr ull msb_pos(ull x) { assert(x != 0); return bit_width(x) - 1; } inline constexpr ull lsb_mask(ull x) { assert(x != 0); return x & -x; } inline constexpr ull msb_mask(ull x) { assert(x != 0); return bit_floor(x); } inline constexpr bool btest(ull x, uint k) { return (x >> k) & 1; } inline constexpr bool bsubset(ull x, ull y) { return (x & y) == x; } inline constexpr bool bsupset(ull x, ull y) { return (x & y) == y; } inline constexpr ull bsetminus(ull x, ull y) { return x & ~y; } // https://github.com/miscalculation53/library/tree/wip/template/template_inout.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_dump.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_dump_map.hpp #define CPP_DUMP_DEFINE_DATA(...) #define dump(...) #define local(...) #define oj(...) __VA_ARGS__ #define local_oj(a,b) (b) namespace fastio { template struct unsigned_integer { using type = make_unsigned_t; }; template <> struct unsigned_integer { using type = u128; }; template <> struct unsigned_integer { using type = u128; }; template using unsigned_integer_t = typename unsigned_integer::type; static constexpr uint32_t SIZ = 1 << 17; char ibuf[SIZ]; char obuf[SIZ]; char out[100]; uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memcpy(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SIZ - pir + pil, stdin); pil = 0; if (pir < SIZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd1(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (c <= ' '); } void rd1(string &x) { x.clear(); while (true) { if (pil == pir) load(); while (pil < pir && ibuf[pil] <= ' ') ++pil; if (pil < pir) break; } while (true) { uint32_t p = pil; while (pil < pir && ibuf[pil] > ' ') ++pil; x.append(ibuf + p, pil - p); if (pil < pir) { ++pil; return; } load(); } } template void rd1_real(T &x) { string s; rd1(s); if constexpr (!is_same_v) { auto [p, ec] = from_chars(s.data(), s.data() + s.size(), x); if (ec == errc{} && p == s.data() + s.size()) return; } if constexpr (is_same_v) x = stold(s); else x = stod(s); } template void rd1_integer(T &x) { using U = unsigned_integer_t; bool minus = false; U val = 0; if constexpr (check_buffer) if (pil + 100 > pir) load(); uint32_t p = pil; while (ibuf[p] < '-') ++p; if constexpr (is_signed::value || is_same_v) { if (ibuf[p] == '-') minus = true, ++p; } while ('0' <= ibuf[p]) val = val * 10 + (ibuf[p++] & 15); pil = p; if constexpr (is_signed::value || is_same_v) { if (minus) { const U min_abs = U(numeric_limits::max()) + 1; x = val == min_abs ? numeric_limits::lowest() : -T(val); } else x = T(val); } else x = T(val); } void rd1(int &x) { rd1_integer(x); } void rd1(ll &x) { rd1_integer(x); } void rd1(i128 &x) { rd1_integer(x); } void rd1(uint &x) { rd1_integer(x); } void rd1(ull &x) { rd1_integer(x); } void rd1(u128 &x) { rd1_integer(x); } void rd1(double &x) { rd1_real(x); } void rd1(long double &x) { rd1_real(x); } template void rd1(pair &p) { return rd1(p.first), rd1(p.second); } template void rd1(vc &x) { for (auto &d: x) rd1(d); } template void read(T &...x) { if constexpr (sizeof...(T) <= SIZ / 100 && ((!is_same_v && (is_integral_v || is_same_v || is_same_v)) && ...)) { if (pil + 100 * sizeof...(T) > pir) load(); (rd1_integer(x), ...); } else (rd1(x), ...); } void wt1(const char c) { if (por == SIZ) flush(); obuf[por++] = c; } void wt1(string_view s) { while (!s.empty()) { if (por == SIZ) flush(); size_t n = min(s.size(), SIZ - por); memcpy(obuf + por, s.data(), n); por += n; s.remove_prefix(n); } } template void wt1_integer(T x) { if (por > SIZ - 100) flush(); using U = unsigned_integer_t; U ux; if constexpr (is_signed::value || is_same_v) { if (x < 0) obuf[por++] = '-', ux = U(0) - U(x); else ux = U(x); } else ux = x; int outi; for (outi = 96; ux >= 10000; outi -= 4) { memcpy(out + outi, pre.num[ux % 10000], 4); ux /= 10000; } if (ux >= 1000) { memcpy(obuf + por, pre.num[ux], 4); por += 4; } else if (ux >= 100) { memcpy(obuf + por, pre.num[ux] + 1, 3); por += 3; } else if (ux >= 10) { int q = (ux * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (ux - q * 10) | '0'; por += 2; } else obuf[por++] = ux | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template void wt1_real(T x) { if constexpr (!is_same_v) { auto [p, ec] = to_chars(out, out + sizeof(out), x, chars_format::fixed, 15); if (ec == errc{}) { wt1(string_view(out, p)); return; } } ostringstream oss; oss << fixed << setprecision(15) << x; wt1(oss.str()); } void wt1(int x) { wt1_integer(x); } void wt1(i128 x) { wt1_integer(x); } void wt1(u128 x) { wt1_integer(x); } void wt1(double x) { wt1_real(x); } void wt1(long double x) { wt1_real(x); } template void write(T &&...x) { (wt1(std::forward(x)), ...); } template void print(T &&...x) { if constexpr (sizeof...(T)) { int i = 0; ((i++ ? wt1(' ') : void(), wt1(std::forward(x))), ...); } wt1('\n'); } } struct Dummy { Dummy() { atexit(fastio::flush); } } dummy; namespace internal { template void READnodump(Ts &...a) { fastio::read(a...); } template void READVECnodump(int n, vc &...v) { (v.resize(n), ...); READnodump(v...); } }; #define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__) #define IN(T,...) T __VA_ARGS__; READ(__VA_ARGS__) #define CHAR(...) IN(char, __VA_ARGS__) #define INT(...) IN(int, __VA_ARGS__) #define LL(...) IN(ll, __VA_ARGS__) #define STR(...) IN(string, __VA_ARGS__) #define ARR(T,n,...) array __VA_ARGS__; READ(__VA_ARGS__) #define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__) #define READVEC2(...) internal::READVEC2nodump(__VA_ARGS__); dump(__VA_ARGS__) #define VEC(T,n,...) vc __VA_ARGS__; READVEC(n, __VA_ARGS__) #define VEC2(T,n,m,...) vvc __VA_ARGS__; READVEC2(n, m, __VA_ARGS__) #define READJAG(...) internal::READJAGnodump(__VA_ARGS__); dump(__VA_ARGS__) #define JAG(T,n,...) vvc __VA_ARGS__; READJAG(n, __VA_ARGS__) #define ENDL '\n' #define WRITE fastio::write #define PRINT fastio::print #define PRINTEXIT(...) do { PRINT(__VA_ARGS__); exit(0); } while (false) #define PRINTRETURN(...) do { PRINT(__VA_ARGS__); return; } while (false) #define PRINTVEXIT(...) do { PRINTV(__VA_ARGS__); exit(0); } while (false) #define PRINTVRETURN(...) do { PRINTV(__VA_ARGS__); return; } while (false) template pair &operator+=(pair &a, const P &b) { a.first += b.first; a.second += b.second; return a; } namespace internal { }; template void offset(vc &v, const Add &add) { for (auto &vi : v) vi += add; } namespace internal { }; #define UNZIP(vt,...) auto [__VA_ARGS__] = unzip(vt) #define ZIP(vt,...) auto vt = zip(tuple{__VA_ARGS__}) // https://github.com/miscalculation53/library/tree/wip/template/template_random.hpp mt19937_64 mt; bool randbool(double p) { assert(0 <= p && p <= 1); return bernoulli_distribution(p)(mt); } namespace internal { }; // https://github.com/miscalculation53/library/tree/wip/math/modint/template_modint.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/modint.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_static.hpp // https://github.com/miscalculation53/library/tree/wip/utils/larger_int.hpp namespace larger_int_detail { } template struct larger_int { private: static constexpr bool check(); static_assert(check()); public: using type = T; }; #define LARGER_INT(T,U) template <> struct larger_int { using type = U; }; LARGER_INT(signed char, short) LARGER_INT(short, int) LARGER_INT(int, long long) LARGER_INT(long, __int128_t) LARGER_INT(long long, __int128_t) LARGER_INT(unsigned char, unsigned short) LARGER_INT(unsigned short, unsigned int) LARGER_INT(unsigned int, unsigned long long) LARGER_INT(unsigned long, __uint128_t) LARGER_INT(unsigned long long, __uint128_t) #undef LARGER_INT template struct Rational; template struct larger_int> { using type = Rational::type>; }; template using larger_int_t = typename larger_int::type; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_isprime.hpp namespace internal { template constexpr ll powmod_constexpr(ll x, ll n, T m) { if (m == 1) return 0; using U = make_unsigned_t; using L = larger_int_t; U r = 1, y = safemod(x, m); while (n) { if (n & 1) r = L(r) * y % m; y = L(y) * y % m; n >>= 1; } return r; } template constexpr bool isprime_constexpr(T n) { if constexpr (sizeof(T) > 4) { if (n <= INT_MAX) return isprime_constexpr(n); } if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; ll d = n - 1; while (d % 2 == 0) d /= 2; using U = make_unsigned_t; using L = larger_int_t; auto miller_rabin = [&](const auto &bases) constexpr { for (ll a : bases) { ll t = d, y = powmod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = L(y) * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) return false; } return true; }; if constexpr(sizeof(T) <= 4) { constexpr ll bases[3] = {2, 7, 61}; return miller_rabin(bases); } else { constexpr ll bases[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; return miller_rabin(bases); } } template constexpr bool isprime = isprime_constexpr(n); }; namespace internal { template struct policy_static { using mod_type = decltype(M); using value_type = make_unsigned_t; using calc_type = larger_int_t; static constexpr bool is_prime = isprime_constexpr(M); static constexpr mod_type mod() { return M; } static constexpr value_type umod() { return M; } static constexpr value_type init(value_type v) { return v; } static constexpr mod_type val(value_type v) { return v; } static constexpr value_type mul(value_type a, value_type b) { return (value_type)((calc_type(a) * b) % M); } }; }; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_barrett32.hpp namespace internal { struct barrett32 { uint m; ull im; explicit barrett32(uint m) : m(m), im((ull)(-1) / m + 1) {} uint umod() const { return m; } uint mul(uint a, uint b) const { ull z = a; z *= b; ull x = ull((u128(z) * im) >> 64); ull y = x * m; return uint(z - y + (z < y ? m : 0)); } }; template struct policy_barrett32 { using value_type = uint; using calc_type = ull; using mod_type = int; static constexpr bool is_prime = false; static inline barrett32 reducer{998244353}; static void set_mod(mod_type m) { reducer = barrett32(m); } static mod_type mod() { return reducer.umod(); } static value_type umod() { return reducer.umod(); } static value_type init(value_type v) { return v; } static mod_type val(value_type v) { return v; } static value_type mul(value_type a, value_type b) { return reducer.mul(a, b); } }; }; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_montgomery64.hpp namespace internal { inline constexpr ull inv64(ull a) { ull x = a; while (a * x != 1) x *= 2 - a * x; return x; } struct montgomery64odd { ull m, im, sq; explicit montgomery64odd(ull m) : m(m), im(inv64(m)), sq(-u128(m) % m) {} ull umod() const { return m; } ull reduce(u128 x) const { auto t = (x + u128(m) * (-im * ull(x))) >> 64; if (t >= m) t -= m; return (ull)t; } ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * sq); } }; struct montgomery64 { ull m, mx, imx, d, q; uint b; explicit montgomery64(ull m) : m(m) { b = countr_zero(m), mx = m >> b; imx = inv64(mx); d = powmod_constexpr((mx + 1) / 2, b, mx); u128 sq = -u128(mx) % mx; q = (1 + (((sq - 1) * d) << b)) % m; } ull umod() const { return m; } ull reduce(u128 x) const { if (b == 0) { auto t = (x + u128(mx) * (-imx * ull(x))) >> 64; if (t >= m) t -= m; return (ull)t; } ull p = x & MASK(b); x = (x >> b) + p * d; ull y = p << (64 - b); auto t = (x + u128(mx) * (imx * (y - ull(x)))) >> (64 - b); if (t >= m) { t -= m; if (t >= m) t -= m; } return (ull)t; } ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * q); } }; template struct policy_montgomery64_odd { using value_type = ull; using calc_type = u128; using mod_type = ll; static constexpr bool is_prime = false; static inline montgomery64odd reducer{(1LL << 61) - 1}; static void set_mod(mod_type m) { reducer = montgomery64odd(m); } static mod_type mod() { return reducer.umod(); } static value_type umod() { return reducer.umod(); } static value_type init(value_type v) { return reducer.inv_reduce(v); } static mod_type val(value_type v) { return reducer.reduce(v); } static value_type mul(value_type a, value_type b) { return reducer.reduce((calc_type)a * b); } }; template struct policy_montgomery64 { using value_type = ull; using calc_type = u128; using mod_type = ll; static constexpr bool is_prime = false; static inline montgomery64 reducer{(1LL << 61) - 1}; static mod_type mod() { return reducer.umod(); } static value_type umod() { return reducer.umod(); } static value_type init(value_type v) { return reducer.inv_reduce(v); } static mod_type val(value_type v) { return reducer.reduce(v); } }; }; // https://github.com/miscalculation53/library/tree/wip/math/extgcd.hpp template constexpr tuple extgcd(T a, T b) { if (a == 0 && b == 0) return {0, 0, 0}; T x1 = 1, y1 = 0, z1 = a; T x2 = 0, y2 = 1, z2 = b; while (z2 != 0) { T q = z1 / z2; tie(x1, x2) = make_pair(x2, x1 - q * x2); tie(y1, y2) = make_pair(y2, y1 - q * y2); tie(z1, z2) = make_pair(z2, z1 - q * z2); } if (z1 < 0) x1 = -x1, y1 = -y1, z1 = -z1; return {z1, x1, y1}; } namespace internal { template struct modint_impl { using V = typename Policy::value_type; using M = typename Policy::mod_type; using mint = modint_impl; private: V _v; public: static constexpr M mod() { return Policy::mod(); } template static auto set_mod(M m) -> decltype(T::set_mod(m)) { return T::set_mod(m); } static mint raw(V v) { mint x; x._v = v; return x; } modint_impl() : _v(0) {} template >> modint_impl(T v) { V rem; if constexpr (is_signed_ext) { using S = make_signed_t; S x = v % S(Policy::umod()); if (x < 0) x += Policy::umod(); rem = x; } else rem = V(v % Policy::umod()); _v = Policy::init(rem); }; M val() const { return Policy::val(_v); } mint &operator+=(const mint &rhs) { _v += rhs._v; if (_v >= Policy::umod()) _v -= Policy::umod(); return *this; } mint &operator-=(const mint &rhs) { _v -= rhs._v; if (_v >= Policy::umod()) _v += Policy::umod(); return *this; } mint &operator*=(const mint &rhs) { _v = Policy::mul(_v, rhs._v); return *this; } mint &operator/=(const mint &rhs) { return *this *= rhs.inv(); } mint operator-() const { return mint() - *this; } template mint pow(T n) const { assert(n >= 0); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if constexpr (Policy::is_prime) { return pow(mod() - 2); } else { auto [g, x, y] = extgcd(val(), mod()); assert(g == 1); return mint(x); } } 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; } friend M safe_hash_key(const mint &x) { return x.val(); } friend void rd1(mint &x) { long long a; fastio::rd1(a); x = a; } friend void wt1(const mint &x) { fastio::wt1(x.val()); } }; }; template using static_modint32 = internal::modint_impl>; template using dynamic_modint32 = internal::modint_impl>; template using static_modint64 = internal::modint_impl>; template using dynamic_modint64_odd = internal::modint_impl>; template using dynamic_modint64 = internal::modint_impl>; using modint998244353 = static_modint32<998244353>; using modint1000000007 = static_modint32<1000000007>; using modint = dynamic_modint32<-1>; using modint61 = static_modint64<(1LL << 61) - 1>; using modint64 = dynamic_modint64<-1>; template struct is_modint : std::false_type { }; template struct is_modint> : std::true_type { }; template inline constexpr bool is_modint_v = is_modint::value; template struct is_static_modint : false_type {}; template struct is_static_modint> : true_type {}; template struct is_static_modint> : true_type {}; template inline constexpr bool is_static_modint_v = is_static_modint::value; template struct is_dynamic_modint : false_type {}; template struct is_dynamic_modint> : true_type {}; template struct is_dynamic_modint> : true_type {}; template struct is_dynamic_modint> : true_type {}; template inline constexpr bool is_dynamic_modint_v = is_dynamic_modint::value; template struct has_mod : std::false_type { }; template struct has_mod> : std::true_type { }; template struct modint_less { }; template struct modint_hash { }; // https://github.com/miscalculation53/library/tree/wip/math/modint/power_table.hpp template struct PowerTable { private: decltype(mint::mod()) mod; mint base; vc pw; public: void reserve(int n) { if (mod != mint::mod()) { mod = mint::mod(); pw = {1}; } int i = pw.size(); if (n < i) return; pw.resize(n + 1); for (; i <= n; i++) pw[i] = pw[i - 1] * base; } }; // https://github.com/miscalculation53/library/tree/wip/math/modint/binomial.hpp template struct Binomial { private: inline static decltype(T::mod()) mod; public: inline static vc fac_, finv_, inv_; static void reserve(int n) { if constexpr (is_dynamic_modint_v) { if (mod != T::mod()) { mod = T::mod(); fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1}; } } else { if (fac_.empty()) fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1}; } if (n < SZ(fac_)) return; chmin(n, T::mod() - 1); int si = fac_.size(); fac_.resize(n + 1), finv_.resize(n + 1), inv_.resize(n + 1); repi(i, si, n + 1) { fac_[i] = fac_[i - 1] * T::raw(i); inv_[i] = -inv_[T::mod() % i] * T::raw(T::mod() / i); finv_[i] = finv_[i - 1] * inv_[i]; } } static T inv(int n) { n %= T::mod(); if (n < 0) n += T::mod(); assert(n != 0); reserve(n); return inv_[n]; } }; // https://github.com/miscalculation53/library/tree/wip/math/modint/stom.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/to_rational.hpp // https://github.com/miscalculation53/library/tree/wip/math/svp2d.hpp namespace cpp_dump { template struct rat_value { T p, q; friend ostream &operator<<(ostream &os, const rat_value &x) { os << x.p; if (x.q != 1) os << '/' << x.q; return os; } }; struct mint_to_rat_fn { }; template struct rat_object { T value; }; template inline constexpr bool rat_is_map = false; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; struct rat_closure : std::ranges::range_adaptor_closure { }; constexpr rat_closure rat() { return rat_closure{}; } } using mint = static_modint32<846862693>; using bi = Binomial; void init() { oj(mt.seed(random_device()())); } // https://github.com/miscalculation53/library/tree/wip/graph/graph.hpp // https://github.com/miscalculation53/library/tree/wip/ds/csr.hpp template struct CSR { protected: int n, m; vc start; vc elist; vc len; template struct RowBase { using iterator = Iter; using reference = typename iterator_traits::reference; private: iterator begi, endi; public: inline iterator begin() const { return begi; } template inline I size() const { return endi - begi; } inline bool empty() const { return size() == 0; } }; using Row = RowBase::iterator>; using ConstRow = RowBase::const_iterator>; public: template I size() const { return n; } vc &get_elist() { return elist; } const vc &get_elist() const { return elist; } }; template struct Edge { int from, to; Cost cost; int index; CPP_DUMP_DEFINE_DATA(from, to, cost, index); }; template <> struct Edge { int from, to, index; static constexpr ll cost = 1; CPP_DUMP_DEFINE_DATA(from, to, cost, index); Edge() : from(-1), to(-1), index(-1) {} Edge(int s, int t, int i = -1) : from(s), to(t), index(i) {} operator int() const { return to; } bool operator<(const Edge &) const { return false; } Edge rev() const { return Edge(to, from, index); } }; template struct Graph { using E = Edge; protected: template > struct InternalEdge { int to, index; C cost; }; template struct InternalEdge { int to, index; static constexpr ll cost = 1; }; using GE = conditional_t>; using Weight = decay_t; int n = 0, m = 0, era = 0; CSR g; vc eid_to_elist_id; static E make_edge(int from, int to, const Weight &cost, int index) { if constexpr (is_void_v) return E(from, to, index); else return E(from, to, cost, index); } struct OutEdgeRow; struct OutEdgeIter { using iterator_category = input_iterator_tag; using value_type = E; using difference_type = ptrdiff_t; using pointer = const E *; using reference = const E &; private: friend struct OutEdgeRow; int from = -1; typename vc::const_iterator it; mutable E e; public: OutEdgeIter() = default; reference operator*() const { if constexpr (is_erasable) return *it; else { e = make_edge(from, it->to, it->cost, it->index); return e; } } }; struct OutEdgeRow { private: const Graph *g; int from, l, r; OutEdgeIter iter(int pos) const { return {from, g->g.get_elist().begin() + pos}; } public: OutEdgeIter begin() const { return iter(l); } template I size() const { return r - l; } bool empty() const { return l == r; } }; public: template I size() const { return n; } private: }; template Graph(int, const vc> &) -> Graph; template Graph(int, const vc> &) -> Graph; template Graph(int, const vc> &, const Cost &) -> Graph; template Graph(int, const vc> &) -> Graph; template using GraphDirected = Graph; template using GraphUndirected = Graph; // https://github.com/miscalculation53/library/tree/wip/math/fps/bmbm.hpp // https://github.com/miscalculation53/library/tree/wip/math/fps/berlekamp_massey.hpp // https://github.com/miscalculation53/library/tree/wip/algebra/algebra_basic_ops.hpp // https://github.com/miscalculation53/library/tree/wip/algebra/algebra_base.hpp template struct Monoid { using S = S_; static constexpr auto op = op_; static constexpr auto e = e_; }; template struct Group { using S = S_; static constexpr auto op = op_; static constexpr auto e = e_; static constexpr auto inv = inv_; }; template struct SemiRing { using S = S_; static constexpr auto add = add_; static constexpr auto e0 = e0_; static constexpr auto mul = mul_; static constexpr auto e1 = e1_; }; template struct Ring { using S = S_; static constexpr auto add = add_; static constexpr auto e0 = e0_; static constexpr auto minus = minus_; static constexpr auto mul = mul_; static constexpr auto e1 = e1_; }; template struct Field { using S = S_; static constexpr auto add = add_; static constexpr auto e0 = e0_; static constexpr auto minus = minus_; static constexpr auto mul = mul_; static constexpr auto e1 = e1_; static constexpr auto inv = inv_; }; template struct OppositeMonoid { using S = typename M::S; static constexpr auto e = M::e; }; template struct OppositeGroup { using S = typename G::S; static constexpr auto e = G::e; static constexpr auto inv = G::inv; }; template struct NormalAndOppositeMonoid { struct S { typename M::S normal; typename M::S opposite; }; }; template struct NormalAndOppositeGroup { struct S { typename G::S normal; typename G::S opposite; }; static constexpr S inv(const S &a) { return {G::inv(a.normal), G::inv(a.opposite)}; } }; template using MonoidOfSemiRingAdd = Monoid; template using MonoidOfSemiRingMul = Monoid; template using GroupOfRingAdd = Group; template using GroupOfFieldMul = Group; template struct SemiRingFromMonoidMonoid { static_assert(is_same_v, "Madd::S and Mmul::S must be identical"); using S = typename Madd::S; static constexpr auto add = Madd::op; static constexpr auto e0 = Madd::e; static constexpr auto mul = Mmul::op; static constexpr auto e1 = Mmul::e; }; template struct RingFromGroupMonoid { static_assert(is_same_v, "Gadd::S and Mmul::S must be identical"); using S = typename Gadd::S; static constexpr auto add = Gadd::op; static constexpr auto e0 = Gadd::e; static constexpr auto minus = Gadd::inv; static constexpr auto mul = Mmul::op; static constexpr auto e1 = Mmul::e; }; template struct FieldFromGroupGroup { static_assert(is_same_v, "Gadd::S and Gmul::S must be identical"); using S = typename Gadd::S; static constexpr auto add = Gadd::op; static constexpr auto e0 = Gadd::e; static constexpr auto minus = Gadd::inv; static constexpr auto mul = Gmul::op; static constexpr auto e1 = Gmul::e; static constexpr auto inv = Gmul::inv; }; template struct has_e1 : false_type {}; template struct has_e1> : true_type {}; template inline constexpr bool has_e1_v = has_e1::value; template struct MonoidMul { using S = T; static constexpr S op(S a, S b) { return a * b; } static constexpr S e() { if constexpr (has_e1_v) return S::e1(); else return 1; } }; template struct GroupAddSub { using S = T; static constexpr S op(S a, S b) { return a + b; } static constexpr S e() { if constexpr (has_e0_v) return S::e0(); else return S{}; } static constexpr S inv(S a) { return -a; } }; template struct GroupMulDiv { using S = T; static constexpr S op(S a, S b) { return a * b; } static constexpr S e() { if constexpr (has_e1_v) return S::e1(); else return S(1); } static constexpr S inv(S a) { return e() / a; } }; template using SemiRingMinPlus = SemiRingFromMonoidMonoid, MonoidAdd>; template using SemiRingMaxPlus = SemiRingFromMonoidMonoid, MonoidAdd>; template using RingAddSubMul = RingFromGroupMonoid, MonoidMul>; template using FieldAddSubMulDiv = FieldFromGroupGroup, GroupMulDiv>; // https://github.com/miscalculation53/library/tree/wip/math/dot_product.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/internal_mod32.hpp namespace internal { template struct ordinary_mod32 : false_type {}; template struct ordinary_mod32> : true_type {}; template struct ordinary_mod32> : true_type {}; template struct ordinary_mod32_add_group : false_type {}; template struct ordinary_mod32_add_group> : ordinary_mod32 {}; template struct ordinary_mod32_add_group::op, GroupAddSub::e, GroupAddSub::inv>> : ordinary_mod32 {}; template struct ordinary_mod32_field : false_type {}; template struct ordinary_mod32_field> : ordinary_mod32 {}; } namespace internal { template using dot_product_mod32_value = ordinary_mod32; template struct dot_product_mod32 : false_type {}; template struct dot_product_mod32> : dot_product_mod32_value {}; template struct dot_product_mod32> : dot_product_mod32_value {}; template struct dot_product_mod32, MonoidMul>> : dot_product_mod32_value {}; template S dot_product_mod32_impl(int n, It1 a, It2 b) { const ull mod = S::mod(); ull sum = 0; for (; n >= block; n -= block) { repi(j, block) sum += ull((a++)->val()) * (b++)->val(); sum %= mod; } repi(j, n) sum += ull((a++)->val()) * (b++)->val(); return S::raw(sum % mod); } } template typename SR::S dot_product(int n, It1 a, It2 b) { using S = typename SR::S; assert(n >= 0); if constexpr (internal::dot_product_mod32::value) { if (S::mod() <= (1 << 30)) return internal::dot_product_mod32_impl<16, S>(n, a, b); return internal::dot_product_mod32_impl<4, S>(n, a, b); } else { S sum = SR::e0(); repi(i, n) sum = SR::add(sum, SR::mul(*a++, *b++)); return sum; } } template struct BerlekampMassey { using S = typename F::S; vc a, b, c, tmp; int pos = -1; S inv_x = F::e0(); void append(S value) { const int i = a.size(), d = c.size(); a.eb(value); S y = F::add(value, F::minus(dot_product(d, c.begin(), a.rbegin() + 1))); if (y == F::e0()) return; if (c.empty()) { c.assign(i + 1, F::e0()); pos = i; inv_x = F::inv(y); return; } S z = F::mul(y, inv_x); int d2 = i - pos + b.size(); if (d2 > d) { tmp = c; c.resize(d2, F::e0()); } c[i - 1 - pos] = F::add(c[i - 1 - pos], z); const S minus_z = F::minus(z); repi(j, b.size()) c[i - pos + j] = F::add(c[i - pos + j], F::mul(minus_z, b[j])); if (d2 > d) pos = i, inv_x = F::inv(y), swap(tmp, b); } vc coefficients() const { vc res; res.reserve(c.size() + 1); res.eb(F::minus(F::e1())); res.insert(res.end(), c.begin(), c.end()); return res; } }; template vc berlekamp_massey(const vc &a) { BerlekampMassey bm; bm.a.reserve(a.size()); for (const auto &x : a) bm.append(x); return bm.coefficients(); } // https://github.com/miscalculation53/library/tree/wip/math/fps/bostan_mori.hpp // https://github.com/miscalculation53/library/tree/wip/bit/bit_reverse.hpp uint32_t bit_reverse32(uint32_t x) { x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1); x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2); x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4); x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8); return (x << 16) | (x >> 16); } int bitrev(int pw2, int i) { assert(pw2 > 0 && has_single_bit((uint)pw2)); assert(0 <= i && i < pw2); if (pw2 == 1) return 0; return bit_reverse32(i) >> (32 - countr_zero((uint)pw2)); } // https://github.com/miscalculation53/library/tree/wip/math/fps/fps.hpp // https://github.com/miscalculation53/library/tree/wip/math/convolution/convolution.hpp // https://github.com/miscalculation53/library/tree/wip/math/crt.hpp template constexpr pair crt_mod_constexpr(const array &rs, const array &ms) { using T = larger_int_t; assert(rs.size() == ms.size()); mint r = 0, m = 1; array rr{}, mm; fill(ALL(mm), 1); repi(i, n) { assert(ms[i] >= U2(1)); assert(U1(0) <= rs[i] && U2(rs[i]) < ms[i]); auto [g, im, _] = extgcd(mm[i], ms[i]); assert(g == 1); T t = safemod((rs[i] - rr[i]) * im, ms[i]); r += t * m, m *= ms[i]; repi(j, i + 1, n) { rr[j] += t * mm[j] % ms[j]; if (rr[j] >= ms[j]) rr[j] -= ms[j]; mm[j] *= ms[i], mm[j] %= ms[j]; } } return {r, m}; } template T convolution_point_get(const vc &a, const vc &b, int p) { const int n = a.size(), m = b.size(); if constexpr (internal::dot_product_mod32_value::value) { if (p < 0 || ll(p) >= ll(n) + m - 1 || n == 0 || m == 0) return T(0); const int l = max(0, p - m + 1), r = min(n, p + 1); return dot_product>(r - l, a.begin() + l, b.rbegin() + (m - 1 - p + l)); } T res = 0; repi(i, max(0, p - m + 1), min(n, p + 1)) res += a[i] * b[p - i]; return res; } namespace internal { 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; if (m == 1107296257) return 10; if (m == 1711276033) return 29; if (m == 1811939329) return 13; if (m == 2013265921) return 31; if (m == 2113929217) return 5; 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 (powmod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template constexpr int primitive_root_for_convolution = primitive_root_constexpr(m); template > struct fft_info { static constexpr int rank2 = countr_zero(mint::mod() - 1); std::array root; std::array iroot; std::array rate2; std::array irate2; std::array rate3; std::array irate3; fft_info() { root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for (int i = rank2 - 1; i >= 0; i--) { root[i] = root[i + 1] * root[i + 1]; iroot[i] = iroot[i + 1] * iroot[i + 1]; } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 2; i++) { rate2[i] = root[i + 2] * prod; irate2[i] = iroot[i + 2] * iprod; prod *= iroot[i + 2]; iprod *= root[i + 2]; } } { 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 bool ntt_ok(int n) { if (n <= 0) return false; if constexpr (is_static_modint_v) { if constexpr (!internal::isprime) return false; static constexpr int rank2 = countr_zero(mint::mod() - 1); return n <= (1 << rank2); } else return false; } template void ntt(vc> &) { assert(false); } template void ntt(vc>> &a) { using mint = internal::modint_impl>; int n = int(a.size()); assert(n > 0); int h = countr_zero((unsigned int)n); assert(n == (1 << h)); assert(ntt_ok(n)); static const internal::fft_info info; int len = 0; while (len < h) { if (h - len == 1) { int p = 1 << (h - len - 1); mint rot = 1; for (int s = 0; s < (1 << 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 { 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 void intt(vc>> &a) { using mint = internal::modint_impl>; int n = int(a.size()); assert(n > 0); int h = countr_zero((unsigned int)n); assert(n == (1 << h)); assert(ntt_ok(n)); static const internal::fft_info info; int len = h; while (len) { if (len == 1) { int p = 1 << (h - len); mint irot = 1; for (int s = 0; s < (1 << (len - 1)); s++) { int offset = s << (h - len + 1); for (int i = 0; i < 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() - (uint)r.val()) * irot.val(); ; } if (s + 1 != (1 << (len - 1))) irot *= info.irate2[countr_zero(~(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[countr_zero(~(unsigned int)(s))]; } len -= 2; } } } namespace internal { template vc convolution_naive(const vc &a, const vc &b) { const int n = a.size(), m = b.size(); const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0); vc c(n + m - 1); if constexpr (dot_product_mod32_value::value) { if (min(n, m) >= 16 && ll(cnta) * 2 >= n && ll(cntb) * 2 >= m) { repi(p, n + m - 1) c[p] = convolution_point_get(a, b, p); return c; } } if ((ll)m * cnta > (ll)n * cntb) { repi(j, m) { if (b[j] == 0) continue; repi(i, n) c[i + j] += a[i] * b[j]; } } else { repi(i, n) { if (a[i] == 0) continue; repi(j, m) c[i + j] += a[i] * b[j]; } } return c; } template vc convolution_ntt(vc a, vc b) { const int n = a.size(), m = b.size(); const int z = bit_ceil(n + m - 1); if (a == b) { a.resize(z); ntt(a); repi(i, z) a[i] *= a[i]; } else { a.resize(z), b.resize(z); ntt(a), ntt(b); repi(i, z) a[i] *= b[i]; } intt(a); mint iz = mint(z).inv(); fem(ai : a) ai *= iz; a.resize(n + m - 1); return a; } template void convolution_crt_helper(const vc &a, const vc &b, vc> &cs) { using mint = static_modint32; const int n = a.size(), m = b.size(); auto c = convolution_ntt(vc(ALL(a)), vc(ALL(b))); repi(i, n + m - 1) cs[i][j] = c[i].val(); } template vc convolution_crt_mod(const vc &a, const vc &b) { const int n = a.size(), m = b.size(); constexpr size_t k = sizeof...(ms); vc> cs(n + m - 1); constexpr array ms_arr = {ms...}; [&](index_sequence) { (convolution_crt_helper(a, b, cs), ...); }(make_index_sequence{}); vc c(n + m - 1); repi(i, n + m - 1) c[i] = crt_mod_constexpr(cs[i], ms_arr).first; return c; } } template ::value>> vc convolution(const vc &a, const vc &b) { const int n = a.size(), m = b.size(); const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0); if (n == 0 || m == 0) return {}; if (ntt_ok(n + m - 1)) { if (min(cnta, cntb) <= 60) return internal::convolution_naive(a, b); return internal::convolution_ntt(a, b); } else { if (min(cnta, cntb) <= 300) return internal::convolution_naive(a, b); assert(ntt_ok>(n + m - 1) && "|a| + |b| - 1 <= 2^26"); vc a_(n), b_(m); repi(i, n) a_[i] = a[i].val(); repi(j, m) b_[j] = b[j].val(); return internal::convolution_crt_mod(a_, b_); } } template ::value>> vc convolution(const vc &a, const vc &b) { using mint = static_modint32; auto c = convolution(vc(ALL(a)), vc(ALL(b))); vc c_(c.size()); repi(i, c.size()) c_[i] = c[i].val(); return c_; } // https://github.com/miscalculation53/library/tree/wip/math/modint/sqrt_mod.hpp // https://github.com/miscalculation53/library/tree/wip/math/prime/large/primality_test.hpp namespace internal { template bool is_prime_impl(ll n, const Array &bases) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; ll d = (n - 1) >> countr_zero(n - 1); mint::set_mod(n); for (ll a : bases) { ll t = d; mint y = mint(a).pow(t); while (t != n - 1 && y != 1 && y != n - 1) { y *= y; t <<= 1; } if (y != n - 1 && t % 2 == 0) return false; } return true; } }; bool is_prime(ll n) { static constexpr array bases32 = {2, 7, 61}; static constexpr array bases64 = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; if (n <= INT_MAX) { using mint = dynamic_modint32; return internal::is_prime_impl(n, bases32); } else { using mint = dynamic_modint64_odd; return internal::is_prime_impl(n, bases64); } } template struct FormalPowerSeries : vc { using F = FormalPowerSeries; using vc::vc; using vc::operator=; using vc::size; using vc::empty; using vc::back; using vc::pop_back; using vc::begin; using vc::resize; using vc::front; FormalPowerSeries(const vc &f) : vc(f) {} int sz() const { return size(); } void shrink() { while (!empty() && back() == 0) pop_back(); } mint get(int i) const { return 0 <= i && i < sz() ? (*this)[i] : 0; } F pre(int len) const { assert(len >= 0); return F(begin(), begin() + min(sz(), len)); } F rev(int d = -1) const { F res(*this); if (d >= 0) res.resize(d); reverse(ALL(res)); return res; } int cnt_nz() const { return count_if(ALL(*this), LMD(x, x != 0)); } vc> nz() const { vc> res; repi(i, sz()) if ((*this)[i] != 0) res.eb(i, (*this)[i]); return res; } F operator-() const { F res(*this); fem(a : res) a = -a; return res; } F &operator*=(const mint &k) { fem(a : *this) a *= k; return *this; } F operator*(const mint &k) const { return F(*this) *= k; } friend F operator*(const mint &k, const F &f) { return f * k; } F &operator-=(const F &g) { const int n = size(), m = g.size(); resize(max(n, m)); repi(i, m)(*this)[i] -= g[i]; return *this; } F operator-(const F &g) const { return F(*this) -= g; } F operator*(const F &g) const { return convolution(*this, g); } F div_sparse_destructive(const F &g, int d = -1) { assert(g.get(0) != 0); if (d < 0) d = max(sz(), g.sz()); mint iv = g.front().inv(); auto gnz = g.nz(); resize(d); repi(i, d) { fec([j, b] : gnz) { if (j == 0) continue; if (j > i) break; (*this)[i] -= (*this)[i - j] * b; } (*this)[i] *= iv; } return pre(d); } F div_sparse(const F &g, int d = -1) const { return F(*this).div_sparse_destructive(g, d); } F inv(int d = -1) const { assert(get(0) != 0); if (d < 0) d = sz(); if (cnt_nz() <= 200) return F{1}.div_sparse(*this, d); F f, g2, g{front().inv()}; for (int m = 1; m < d; m *= 2) { if (ntt_ok(2 * m)) { f = pre(2 * m), g2 = F(g); f.resize(2 * m), ntt(f); g2.resize(2 * m), ntt(g2); repi(i, 2 * m) f[i] *= g2[i]; intt(f); f >>= m; f.resize(2 * m), ntt(f); repi(i, 2 * m) f[i] *= g2[i]; intt(f); mint iz = mint(2 * m).inv(); iz *= -iz; repi(i, m) f[i] *= iz; g.insert(g.end(), f.begin(), f.begin() + m); } else g = (g * mint(2) - g * g * pre(2 * m)).pre(2 * m); } return g.pre(d); } F div_poly(const F &g) const { const int k = sz() - g.sz() + 1; if (k <= 0) return {}; return (rev().pre(k) * g.rev().inv(k)).pre(k).rev(); } pair divmod(const F &g) const { F q = div_poly(g); const int l = sz() - q.sz(); F r = pre(l) - (q.pre(l) * g.pre(l)).pre(l); r.shrink(); return {q, r}; } F operator>>(int k) const { F res(max(0, sz() - k)); repi(i, sz() - k) res[i] = (*this)[i + k]; return res; } F &operator>>=(int k) { return *this = *this >> k; } }; template mint bostan_mori(const FormalPowerSeries &p, const FormalPowerSeries &q, ll k) { using F = FormalPowerSeries; assert(k >= 0); static const internal::fft_info info; auto [r, u] = p.divmod(q); mint res = k < r.sz() ? r[(int)k] : 0; const int d = SZ(q) - 1; if (d == 0) return res; if (ntt_ok(2 * d + 1)) { const int z = bit_ceil(2 * d + 1); const mint i2 = mint(2).inv(); const mint iz = mint(z / 2).inv(); const mint root = info.root[bit_width(z / 2)]; const mint iroot = info.iroot[bit_width(z / 2)]; vc ipw(z / 2); { mint itmp = 1; repi(i, z / 2) { ipw[bitrev(z / 2, i)] = itmp; itmp *= iroot; } } F v = q; u.resize(z / 2), v.resize(z / 2); F u2 = u, v2 = v; ntt(u), ntt(v); while (k > 0) { { mint tmp = 1; repi(i, z / 2) { u2[i] = u2[i] * tmp; v2[i] = v2[i] * tmp; tmp *= root; } } ntt(u2), ntt(v2); if (k & 1) { repi(i, z / 4) { const mint x = v[2 * i], y = v[2 * i + 1]; u[i] = (u[2 * i] * y - u[2 * i + 1] * x) * ipw[i] * i2; v[i] = x * y; } repi(i, z / 4) { const mint x = v2[2 * i], y = v2[2 * i + 1]; u[i + z / 4] = (u2[2 * i] * y - u2[2 * i + 1] * x) * ipw[i + z / 4] * i2; v[i + z / 4] = x * y; } } else { repi(i, z / 4) { const mint x = v[2 * i], y = v[2 * i + 1]; u[i] = (u[2 * i] * y + u[2 * i + 1] * x) * i2; v[i] = x * y; } repi(i, z / 4) { const mint x = v2[2 * i], y = v2[2 * i + 1]; u[i + z / 4] = (u2[2 * i] * y + u2[2 * i + 1] * x) * i2; v[i + z / 4] = x * y; } } u2 = u, v2 = v; intt(u2), intt(v2); repi(i, z / 2) u2[i] *= iz, v2[i] *= iz; k >>= 1; } return res + u2[0] / v2[0]; } else { F v = q; u.resize(d + 1), v.resize(d + 1); while (k > 0) { F w = v; repi(i, 1, d + 1, 2) w[i] = -w[i]; F u2 = u * w, v2 = v * w; repi(i, d + 1) { if (2 * i + (k & 1) < SZ(u2)) u[i] = u2[2 * i + (k & 1)]; if (2 * i < SZ(v2)) v[i] = v2[2 * i]; } k >>= 1; } return res + u[0] / v[0]; } } template pair, FormalPowerSeries> linear_recurrence_gf(const vc &a, const vc &c) { using F = FormalPowerSeries; const int d = SZ(c) - 1; assert(d >= 0); assert(SZ(a) >= d); F q = -F(c); q[0] = 1; F p = (F(a) * q).pre(d); return {p, q}; } template mint bmbm(const vc &a, ll k, bool show_coefs = true) { auto c = berlekamp_massey>(a); if (show_coefs) dump(c | cp::index() | cp::rat()); auto [p, q] = linear_recurrence_gf(a, c); if (show_coefs) dump(p | cp::index() | cp::rat(), q | cp::index() | cp::rat()); return bostan_mori(p, q, k); } void main2() { LL(N, M, K, S, T); S--, T--; VEC(pll, M, UV); offset(UV, pll{-1, -1}); const ll LIM = 5 * N + 1; auto dp = dvec({2LL, N}, mint(0)), ndp = dp; dp[0][S] = 1; vc vec(LIM); vec[0] = dp[0][T] + dp[1][T]; rep(t, 1, LIM) { rep(i, 2) rep(j, N) ndp[i][j] = 0; fec([ u, v ] : UV) { ndp[0][u] += dp[0][v]; ndp[0][v] += dp[0][u]; ndp[1][u] += dp[0][v]; ndp[1][v] += dp[0][u]; } mint sm = SUM(dp[0]); rep(v, N) ndp[1][v] += sm - dp[0][v]; swap(dp, ndp); vec[t] = dp[0][T] + dp[1][T]; } mint ans = bmbm(vec, K); PRINT(ans != 0 ? "Yes" : "No"); } void test() { } // https://github.com/miscalculation53/library/tree/wip/template/template_main.hpp template struct Main { Main() { cauto CERR = [](string val, string color) { string s = "\033[" + color + "m" + val + "\033[m"; }; CERR("\n[FAST_IO]\n\n", "32"); cout << fixed << setprecision(20); init(); CERR("\n[SINGLE_TESTCASE]\n\n", "36"); main2(); } }; Main main_dummy; int main() {}