#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef KK2_TEMPLATE_PROCON_HPP #define KK2_TEMPLATE_PROCON_HPP 1 #ifndef KK2_TEMPLATE_CONSTANT_HPP #define KK2_TEMPLATE_CONSTANT_HPP 1 #ifndef KK2_TEMPLATE_TYPE_ALIAS_HPP #define KK2_TEMPLATE_TYPE_ALIAS_HPP 1 using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using pi = std::pair; using pl = std::pair; using pil = std::pair; using pli = std::pair; template using vc = std::vector; template using vvc = std::vector>; template using vvvc = std::vector>; template using vvvvc = std::vector>; template using pq = std::priority_queue; template using pqi = std::priority_queue, std::greater>; #endif // KK2_TEMPLATE_TYPE_ALIAS_HPP template constexpr T infty = 0; template <> constexpr int infty = (1 << 30) - 123; template <> constexpr i64 infty = (1ll << 62) - (1ll << 31); template <> constexpr i128 infty = (i128(1) << 126) - (i128(1) << 63); template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr u128 infty = infty; template <> constexpr double infty = infty; template <> constexpr long double infty = infty; constexpr int mod = 998244353; constexpr int modu = 1e9 + 7; constexpr long double PI = 3.14159265358979323846; #endif // KK2_TEMPLATE_CONSTANT_HPP #ifndef KK2_TEMPLATE_FUNCTION_UTIL_HPP #define KK2_TEMPLATE_FUNCTION_UTIL_HPP 1 #ifndef KK2_MATH_MONOID_MAX_HPP #define KK2_MATH_MONOID_MAX_HPP 1 #ifndef KK2_TYPE_TRAITS_IO_HPP #define KK2_TYPE_TRAITS_IO_HPP 1 namespace kk2 { namespace type_traits { struct istream_tag {}; struct ostream_tag {}; } // namespace type_traits template using is_standard_istream = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_standard_ostream = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_user_defined_istream = std::is_base_of; template using is_user_defined_ostream = std::is_base_of; template using is_istream = typename std::conditional::value || is_user_defined_istream::value, std::true_type, std::false_type>::type; template using is_ostream = typename std::conditional::value || is_user_defined_ostream::value, std::true_type, std::false_type>::type; template using is_istream_t = std::enable_if_t::value>; template using is_ostream_t = std::enable_if_t::value>; } // namespace kk2 #endif // KK2_TYPE_TRAITS_IO_HPP namespace kk2 { namespace monoid { template > struct Max { static constexpr bool commutative = true; using M = Max; S a; bool is_unit; Max() : a(S()), is_unit(true) {} Max(S a_) : a(a_), is_unit(false) {} operator S() const { return a; } inline static M op(M l, M r) { if (l.is_unit or r.is_unit) return l.is_unit ? r : l; return Compare{}(l.a, r.a) ? r : l; } inline static M unit() { return M(); } bool operator==(const M &rhs) const { return is_unit == rhs.is_unit and (is_unit or a == rhs.a); } bool operator!=(const M &rhs) const { return is_unit != rhs.is_unit or (!is_unit and a != rhs.a); } template * = nullptr> friend OStream &operator<<(OStream &os, const M &x) { if (x.is_unit) os << "-inf"; else os << x.a; return os; } template * = nullptr> friend IStream &operator>>(IStream &is, M &x) { is >> x.a; x.is_unit = false; return is; } }; } // namespace monoid } // namespace kk2 #endif // MATH_MONOID_MAX_HPP #ifndef KK2_MATH_MONOID_MIN_HPP #define KK2_MATH_MONOID_MIN_HPP 1 namespace kk2 { namespace monoid { template > struct Min { static constexpr bool commutative = true; using M = Min; S a; bool is_unit; Min() : a(S()), is_unit(true) {} Min(S a_) : a(a_), is_unit(false) {} operator S() const { return a; } inline static M op(M l, M r) { if (l.is_unit or r.is_unit) return l.is_unit ? r : l; return Compare{}(l.a, r.a) ? l : r; } inline static M unit() { return M(); } bool operator==(const M &rhs) const { return is_unit == rhs.is_unit and (is_unit or a == rhs.a); } bool operator!=(const M &rhs) const { return is_unit != rhs.is_unit or (!is_unit and a != rhs.a); } template * = nullptr> friend OStream &operator<<(OStream &os, const M &x) { if (x.is_unit) os << "inf"; else os << x.a; return os; } template * = nullptr> friend IStream &operator>>(IStream &is, M &x) { is >> x.a; x.is_unit = false; return is; } }; } // namespace monoid } // namespace kk2 #endif // KK2_MATH_MONOID_MIN_HPP #ifndef KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP #define KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP 1 namespace kk2 { template struct is_vector : std::false_type {}; template struct is_vector> : std::true_type {}; } // namespace kk2 #endif // KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP namespace kk2 { template auto make_vector(int first, Sizes... sizes) { if constexpr (sizeof...(sizes) == 0) { return std::vector(first); } else { return std::vector(sizes...))>(first, make_vector(sizes...)); } } template void fill_all(std::vector &v, const U &x) { if constexpr (is_vector::value) { for (auto &u : v) fill_all(u, x); } else { std::fill(v.begin(), v.end(), T(x)); } } template int iota_all(std::vector &v, U x, int offset = 0) { if constexpr (is_vector::value) { for (auto &u : v) offset += iota_all(u, x + offset); } else { for (auto &u : v) u = x++, ++offset; } return offset; } template int mysize(const C &c) { return size(c); } // T: commutative monoid, F: (U, T) -> U template U all_monoid_prod(const std::vector &v, U unit, const F &f) { U res = unit; if constexpr (is_vector::value) { for (const auto &x : v) res = f(res, all_monoid_prod(x, unit, f)); } else { for (const auto &x : v) res = f(res, x); } return res; } template U all_sum(const std::vector &v, U unit = U()) { return all_monoid_prod(v, unit, [](U a, U b) { return a + b; }); } template U all_prod(const std::vector &v, U unit = U(1)) { return all_monoid_prod(v, unit, [](U a, U b) { return a * b; }); } template U all_xor(const std::vector &v, U unit = U()) { return all_monoid_prod(v, unit, [](U a, U b) { return a ^ b; }); } template U all_and(const std::vector &v, U unit = U(-1)) { return all_monoid_prod(v, unit, [](U a, U b) { return a & b; }); } template U all_or(const std::vector &v, U unit = U()) { return all_monoid_prod(v, unit, [](U a, U b) { return a | b; }); } template U all_min(const std::vector &v) { return all_monoid_prod, T>(v, monoid::Min::unit(), monoid::Min::op); } template U all_max(const std::vector &v) { return all_monoid_prod, T>(v, monoid::Max::unit(), monoid::Max::op); } template U all_gcd(const std::vector &v, U unit = U()) { return all_monoid_prod(v, unit, [](U a, U b) { return std::gcd(a, b); }); } template U all_lcm(const std::vector &v, U unit = U(1)) { return all_monoid_prod(v, unit, [](U a, U b) { return std::lcm(a, b); }); } } // namespace kk2 #endif // KK2_TEMPLATE_FUNCTION_UTIL_HPP #ifndef KK2_TEMPLATE_IO_UTIL_HPP #define KK2_TEMPLATE_IO_UTIL_HPP 1 // なんかoj verifyはプロトタイプ宣言が落ちる namespace impl { struct read { template inline static void all_read(IStream &is, T &x) { is >> x; } template inline static void all_read(IStream &is, std::pair &p) { all_read(is, p.first); all_read(is, p.second); } template inline static void all_read(IStream &is, std::vector &v) { for (T &x : v) all_read(is, x); } template inline static void all_read(IStream &is, std::array &a) { for (T &x : a) all_read(is, x); } }; struct write { template inline static void all_write(OStream &os, const T &x) { os << x; } template inline static void all_write(OStream &os, const std::pair &p) { all_write(os, p.first); all_write(os, ' '); all_write(os, p.second); } template inline static void all_write(OStream &os, const std::vector &v) { for (int i = 0; i < (int)v.size(); ++i) { if (i) all_write(os, ' '); all_write(os, v[i]); } } template inline static void all_write(OStream &os, const std::array &a) { for (int i = 0; i < (int)F; ++i) { if (i) all_write(os, ' '); all_write(os, a[i]); } } }; } // namespace impl template * = nullptr> IStream &operator>>(IStream &is, std::pair &p) { impl::read::all_read(is, p); return is; } template * = nullptr> IStream &operator>>(IStream &is, std::vector &v) { impl::read::all_read(is, v); return is; } template * = nullptr> IStream &operator>>(IStream &is, std::array &a) { impl::read::all_read(is, a); return is; } template * = nullptr> OStream &operator<<(OStream &os, const std::pair &p) { impl::write::all_write(os, p); return os; } template * = nullptr> OStream &operator<<(OStream &os, const std::vector &v) { impl::write::all_write(os, v); return os; } template * = nullptr> OStream &operator<<(OStream &os, const std::array &a) { impl::write::all_write(os, a); return os; } #endif // KK2_TEMPLATE_IO_UTIL_HPP #ifndef KK2_TEMPLATE_MACROS_HPP #define KK2_TEMPLATE_MACROS_HPP 1 #define rep1(a) for (long long _ = 0; _ < (long long)(a); ++_) #define rep2(i, a) for (long long i = 0; i < (long long)(a); ++i) #define rep3(i, a, b) for (long long i = (a); i < (long long)(b); ++i) #define repi2(i, a) for (long long i = (a) - 1; i >= 0; --i) #define repi3(i, a, b) for (long long i = (a) - 1; i >= (long long)(b); --i) #define overload3(a, b, c, d, ...) d #define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi(...) overload3(__VA_ARGS__, repi3, repi2, rep1)(__VA_ARGS__) #define fi first #define se second #define all(p) begin(p), end(p) #endif // KK2_TEMPLATE_MACROS_HPP struct FastIOSetUp { FastIOSetUp() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } fast_io_set_up; auto &kin = std::cin; auto &kout = std::cout; auto (*kendl)(std::ostream &) = std::endl>; void Yes(bool b = 1) { kout << (b ? "Yes\n" : "No\n"); } void No(bool b = 1) { kout << (b ? "No\n" : "Yes\n"); } void YES(bool b = 1) { kout << (b ? "YES\n" : "NO\n"); } void NO(bool b = 1) { kout << (b ? "NO\n" : "YES\n"); } void yes(bool b = 1) { kout << (b ? "yes\n" : "no\n"); } void no(bool b = 1) { kout << (b ? "no\n" : "yes\n"); } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } std::istream &operator>>(std::istream &is, u128 &x) { std::string s; is >> s; x = 0; for (char c : s) { assert('0' <= c && c <= '9'); x = x * 10 + c - '0'; } return is; } std::istream &operator>>(std::istream &is, i128 &x) { std::string s; is >> s; bool neg = s[0] == '-'; x = 0; for (int i = neg; i < (int)s.size(); i++) { assert('0' <= s[i] && s[i] <= '9'); x = x * 10 + s[i] - '0'; } if (neg) x = -x; return is; } std::ostream &operator<<(std::ostream &os, u128 x) { if (x == 0) return os << '0'; std::string s; while (x) { s.push_back('0' + x % 10); x /= 10; } std::reverse(s.begin(), s.end()); return os << s; } std::ostream &operator<<(std::ostream &os, i128 x) { if (x == 0) return os << '0'; if (x < 0) { os << '-'; x = -x; } std::string s; while (x) { s.push_back('0' + x % 10); x /= 10; } std::reverse(s.begin(), s.end()); return os << s; } #endif // KK2_TEMPLATE_PROCON_HPP #ifndef KK2_TEMPLATE_DEBUG_HPP #define KK2_TEMPLATE_DEBUG_HPP 1 #ifndef KK2_TYPE_TRAITS_MEMBER_HPP #define KK2_TYPE_TRAITS_MEMBER_HPP 1 namespace kk2 { #define HAS_MEMBER_FUNC(member) \ template struct has_member_func_##member##_impl { \ template \ static std::true_type check(decltype(std::declval().member(std::declval()...)) *); \ template static std::false_type check(...); \ using type = decltype(check(nullptr)); \ }; \ template struct has_member_func_##member \ : has_member_func_##member##_impl::type {}; \ template using has_member_func_##member##_t = \ std::enable_if_t::value>; \ template using not_has_member_func_##member##_t = \ std::enable_if_t::value>; #define HAS_MEMBER_VAR(member) \ template struct has_member_var_##member##_impl { \ template static std::true_type check(decltype(std::declval().member) *); \ template static std::false_type check(...); \ using type = decltype(check(nullptr)); \ }; \ template struct has_member_var_##member \ : has_member_var_##member##_impl::type {}; \ template using has_member_var_##member##_t = \ std::enable_if_t::value>; \ template using not_has_member_var_##member##_t = \ std::enable_if_t::value>; HAS_MEMBER_FUNC(debug_output) HAS_MEMBER_FUNC(val) #undef HAS_MEMBER_FUNC #undef HAS_MEMBER_VAR } // namespace kk2 #endif // KK2_TYPE_TRAITS_MEMBER_HPP namespace kk2 { namespace debug { #ifdef KK2 template *> void output(OStream &os); template *> void output(OStream &os, const T &t); template *> void output(OStream &os, const std::vector &v); template *> void output(OStream &os, const std::vector> &d); template *> void output(OStream &os, const std::array &a); template *> void output(OStream &os, const std::pair &p); template *> void output(OStream &os, const std::queue &q); template *> void output(OStream &os, const std::priority_queue &q); template *> void output(OStream &os, const std::deque &d); template *> void output(OStream &os, const std::stack &s); template *> void output(OStream &os, const std::set &s); template *> void output(OStream &os, const std::multiset &s); template *> void output(OStream &os, const std::unordered_set &s); template *> void output(OStream &os, const std::unordered_multiset &s); template *> void output(OStream &os, const std::map &m); template *> void output(OStream &os, const std::unordered_map &m); template * = nullptr> void output(OStream &) {} template * = nullptr> void output(OStream &os, const T &t) { if constexpr (has_member_func_debug_output::value) { t.debug_output(os); } else { os << t; } } template * = nullptr> void output(OStream &os, const std::vector &v) { os << "["; for (int i = 0; i < (int)v.size(); i++) { output(os, v[i]); if (i + 1 != (int)v.size()) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::vector> &d) { os << "[\n"; for (int i = 0; i < (int)d.size(); i++) { output(os, d[i]); output(os, "\n"); } os << "]"; } template * = nullptr> void output(OStream &os, const std::array &a) { os << "["; for (int i = 0; i < (int)F; i++) { output(os, a[i]); if (i + 1 != (int)F) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::pair &p) { os << "("; output(os, p.first); os << ", "; output(os, p.second); os << ")"; } template * = nullptr> void output(OStream &os, const std::queue &q) { os << "["; std::queue tmp = q; while (!tmp.empty()) { output(os, tmp.front()); tmp.pop(); if (!tmp.empty()) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::priority_queue &q) { os << "["; std::priority_queue tmp = q; while (!tmp.empty()) { output(os, tmp.top()); tmp.pop(); if (!tmp.empty()) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::deque &d) { os << "["; std::deque tmp = d; while (!tmp.empty()) { output(os, tmp.front()); tmp.pop_front(); if (!tmp.empty()) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::stack &s) { os << "["; std::stack tmp = s; std::vector v; while (!tmp.empty()) { v.push_back(tmp.top()); tmp.pop(); } for (int i = (int)v.size() - 1; i >= 0; i--) { output(os, v[i]); if (i != 0) os << ", "; } os << "]"; } template * = nullptr> void output(OStream &os, const std::set &s) { os << "{"; std::set tmp = s; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, *it); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const std::multiset &s) { os << "{"; std::multiset tmp = s; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, *it); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const std::unordered_set &s) { os << "{"; std::unordered_set tmp = s; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, *it); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const std::unordered_multiset &s) { os << "{"; std::unordered_multiset tmp = s; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, *it); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const std::map &m) { os << "{"; std::map tmp = m; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, it->first); os << ": "; output(os, it->second); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const std::unordered_map &m) { os << "{"; std::unordered_map tmp = m; for (auto it = tmp.begin(); it != tmp.end(); ++it) { output(os, it->first); os << ": "; output(os, it->second); if (std::next(it) != tmp.end()) os << ", "; } os << "}"; } template * = nullptr> void output(OStream &os, const T &t, const Args &...args) { output(os, t); os << ' '; output(os, args...); } template * = nullptr> void outputln(OStream &os) { os << '\n'; os.flush(); } template * = nullptr> void outputln(OStream &os, const T &t, const Args &...args) { output(os, t, args...); os << '\n'; os.flush(); } std::vector sep(const char *s) { std::vector res; std::string now; int dep = 0; while (true) { if (*s == '\0') { res.emplace_back(now); break; } if (*s == '(' or *s == '[' or *s == '{') dep++; if (*s == ')' or *s == ']' or *s == '}') dep--; if (dep == 0 and *s == ',') { res.emplace_back(now); now.clear(); } else if (!isspace(*s)) { now += *s; } s++; } return res; } void show_vars(const std::vector &, int) {} template void show_vars(const std::vector &name, int pos, const T &t, const Args &...args) { assert(pos < (int)name.size()); output(std::cerr, name[pos++] + ":", t); if (sizeof...(args) > 0) output(std::cerr, ", "); show_vars(name, pos, args...); } #define kdebug(...) \ kk2::debug::output(std::cerr, "line:" + std::to_string(__LINE__)); \ kk2::debug::output(std::cerr, ' '); \ kk2::debug::show_vars(kk2::debug::sep(#__VA_ARGS__), 0, __VA_ARGS__); \ kk2::debug::outputln(std::cerr); #else template * = nullptr> void output(OStream &, const Args &...) {} template * = nullptr> void outputln(OStream &, const Args &...) {} template void fix_warn(const Args &...) {} #define kdebug(...) kk2::debug::fix_warn(__VA_ARGS__); #endif // KK2 } // namespace debug } // namespace kk2 #endif // KK2_TEMPLATE_DEBUG_HPP #ifndef KK2_MODINT_MODINT_HPP #define KK2_MODINT_MODINT_HPP 1 #ifndef KK2_TYPE_TRAITS_INTERGRAL_HPP #define KK2_TYPE_TRAITS_INTERGRAL_HPP 1 namespace kk2 { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value or std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value or std::is_same::value, std::true_type, std::false_type>::type; template using is_integral = typename std::conditional::value or is_signed_int128::value or is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed = typename std::conditional::value or is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned = typename std::conditional::value or is_unsigned_int128::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using to_unsigned = typename std::conditional::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = std::enable_if_t::value>; template using is_signed = std::enable_if_t::value>; template using is_unsigned = std::enable_if_t::value>; template using to_unsigned = std::make_unsigned; #endif // _MSC_VER template using is_integral_t = std::enable_if_t::value>; template using is_signed_t = std::enable_if_t::value>; template using is_unsigned_t = std::enable_if_t::value>; } // namespace kk2 #endif // KK2_TYPE_TRAITS_INTERGRAL_HPP namespace kk2 { template struct ModInt { using mint = ModInt; public: static int Mod; constexpr static unsigned int getmod() { if (p > 0) return p; else return Mod; } static void setmod(int Mod_) { assert(1 <= Mod_); Mod = Mod_; } static mint raw(int v) { mint x; x._v = v; return x; } constexpr ModInt() : _v(0) {} template * = nullptr> constexpr ModInt(T v) { if constexpr (is_signed::value) { v = v % (long long)(getmod()); if (v < 0) v += getmod(); _v = v; } else if constexpr (is_unsigned::value) { _v = v %= getmod(); } else { ModInt(); } } unsigned int val() const { return _v; } mint &operator++() { _v++; if (_v == getmod()) _v = 0; return *this; } mint &operator--() { if (_v == 0) _v = getmod(); _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 >= getmod()) _v -= getmod(); return *this; } mint &operator-=(const mint &rhs) { _v += getmod() - rhs._v; if (_v >= getmod()) _v -= getmod(); return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = _v; z *= rhs._v; z %= getmod(); _v = z; return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } 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; } 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 { long long s = getmod(), t = _v; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; std::swap(s, t); std::swap(m0, m1); } if (m0 < 0) m0 += getmod() / s; return m0; } template * = nullptr> friend OStream &operator<<(OStream &os, const mint &mint_) { os << mint_._v; return os; } template * = nullptr> friend IStream &operator>>(IStream &is, mint &mint_) { long long x; is >> x; mint_ = mint(x); return is; } private: unsigned int _v; }; template int ModInt

::Mod = 998244353; using mint998 = ModInt<998244353>; using mint107 = ModInt<1000000007>; } // namespace kk2 #endif // KK2_MODINT_MODINT_HPP #ifndef KK2_MATH_PRIME_FACTORIZE_HPP #define KK2_MATH_PRIME_FACTORIZE_HPP 1 #ifndef KK2_MATH_MOD_POW_MOD_HPP #define KK2_MATH_MOD_POW_MOD_HPP 1 namespace kk2 { template constexpr S pow_mod(T x, U n, T m) { assert(n >= 0); if (m == 1) return S(0); S _m = m, r = 1; S y = x % _m; if (y < 0) y += _m; while (n) { if (n & 1) r = (r * y) % _m; if (n >>= 1) y = (y * y) % _m; } return r; } } // namespace kk2 #endif // KK2_MATH_MOD_POW_MOD_HPP #ifndef KK2_MODINT_MONT_ARB_HPP #define KK2_MODINT_MONT_ARB_HPP 1 namespace kk2 { template struct ArbitraryLazyMontgomeryModIntBase { using mint = ArbitraryLazyMontgomeryModIntBase; inline static UInt mod; inline static UInt r; inline static UInt n2; static constexpr int bit_length = sizeof(UInt) * 8; static UInt get_r() { UInt ret = mod; while (mod * ret != 1) ret *= UInt(2) - mod * ret; return ret; } static void setmod(UInt m) { assert(m < (UInt(1u) << (bit_length - 2))); assert(m & 1); mod = m, n2 = -ULong(m) % m, r = get_r(); } UInt _v; ArbitraryLazyMontgomeryModIntBase() : _v(0) {} template * = nullptr> ArbitraryLazyMontgomeryModIntBase(const T &b) : _v(reduce(ULong(b % (Int)mod + mod) * n2)) {} static UInt reduce(const ULong &b) { return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length; } mint &operator+=(const mint &b) { if (Int(_v += b._v - 2 * mod) < 0) _v += 2 * mod; return *this; } mint &operator-=(const mint &b) { if (Int(_v -= b._v) < 0) _v += 2 * mod; return *this; } mint &operator*=(const mint &b) { _v = reduce(ULong(_v) * b._v); return *this; } mint &operator/=(const mint &b) { *this *= b.inv(); return *this; } mint operator-() const { return mint(0) - mint(*this); } mint operator+() const { return mint(*this); } friend mint operator+(const mint &a, const mint &b) { return mint(a) += b; } friend mint operator-(const mint &a, const mint &b) { return mint(a) -= b; } friend mint operator*(const mint &a, const mint &b) { return mint(a) *= b; } friend mint operator/(const mint &a, const mint &b) { return mint(a) /= b; } bool operator==(const mint &b) const { return (_v >= mod ? _v - mod : _v) == (b._v >= mod ? b._v - mod : b._v); } bool operator!=(const mint &b) const { return (_v >= mod ? _v - mod : _v) != (b._v >= mod ? b._v - mod : b._v); } template mint pow(T n) const { mint ret(1), mul(*this); n %= (Long)getmod() - 1; while (n > 0) { if (n & 1) ret *= mul; if (n >>= 1) mul *= mul; } return ret; } mint inv() const { Int s = getmod(), t = val(), m0 = 0, m1 = 1; while (t) { Int u = s / t; std::swap(s -= t * u, t); std::swap(m0 -= m1 * u, m1); } if (m0 < 0) m0 += getmod(); return mint(m0); } template * = nullptr> friend OStream &operator<<(OStream &os, const mint &x) { return os << x.val(); } template * = nullptr> friend IStream &operator>>(IStream &is, mint &x) { Long t; is >> t; x = mint(t); return (is); } UInt val() const { UInt ret = reduce(_v); return ret >= mod ? ret - mod : ret; } static UInt getmod() { return mod; } }; template using ArbitraryLazyMontgomeryModInt = ArbitraryLazyMontgomeryModIntBase; template using ArbitraryLazyMontgomeryModInt64bit = ArbitraryLazyMontgomeryModIntBase; } // namespace kk2 #endif // KK2_MODINT_MONT_ARB_HPP #ifndef KK2_RANDOM_GEN_HPP #define KK2_RANDOM_GEN_HPP 1 #ifndef KK2_RANDOM_SEED_HPP #define KK2_RANDOM_SEED_HPP 1 namespace kk2 { namespace random { using u64 = unsigned long long; inline u64 non_deterministic_seed() { u64 seed = std::chrono::duration_cast( std::chrono::high_resolution_clock::now().time_since_epoch()) .count(); seed ^= reinterpret_cast(&seed); seed ^= seed << 5; seed ^= seed >> 41; seed ^= seed << 20; return seed; } inline u64 deterministic_seed() { return 5801799128519729247ull; } inline u64 seed() { #if defined(KK2_RANDOM_DETERMINISTIC) return deterministic_seed(); #else return non_deterministic_seed(); #endif } } // namespace random } // namespace kk2 #endif // KK2_RANDOM_SEED_HPP namespace kk2 { namespace random { using i64 = long long; using u64 = unsigned long long; inline u64 rng() { static std::mt19937_64 mt(kk2::random::seed()); return mt(); } // [l, r) inline i64 rng(i64 l, i64 r) { assert(l < r); return l + rng() % (r - l); } // [l, r) template std::vector random_vector(int n, T l, T r) { std::vector res(n); for (int i = 0; i < n; i++) res[i] = rng(l, r); return res; } // [l, r) std::vector distinct_rng(i64 l, i64 r, i64 n) { assert(l < r and n <= r - l); std::unordered_set st; for (i64 i = n; i; --i) { i64 m = rng(l, r + 1 - i); if (st.find(m) != st.end()) m = r - i; st.insert(m); } std::vector res(st.begin(), st.end()); std::sort(res.begin(), res.end()); return res; } template void shuffle(Iter first, Iter last) { if (first == last) return; int len = 1; for (auto it = first + 1; it != last; ++it) { len++; int j = rng(0, len); if (j != len - 1) std::iter_swap(first + j, it); } } template std::vector perm(int n) { std::vector res(n); std::iota(res.begin(), res.end(), T(0)); shuffle(res.begin(), res.end()); return res; } template std::vector choices(int l, int r, int k) { assert(l < r and k <= r - l); std::vector res(r - l); std::iota(res.begin(), res.end(), T(l)); shuffle(res.begin(), res.end()); res.resize(k); return res; } } // namespace random } // namespace kk2 #endif // KK2_RANDOM_GEN_HPP #ifndef KK2_MATH_IS_PRIME_HPP #define KK2_MATH_IS_PRIME_HPP 1 namespace kk2 { namespace number_theory { template bool miller_rabin(const T &n, const std::vector &ws) { if (n <= 2) return n == 2; if (~n & 1) return false; T d = n - 1; while (~d & 1) d >>= 1; U e = 1, rev = n - 1; for (T w : ws) { if (w % n == 0) continue; T t = d; U y = pow_mod(w, t, n); while (t != n - 1 and y != e and y != rev) { y = y * y % n; t <<= 1; } if (y != rev and ~t & 1) return false; } return true; } bool miller_rabin_u64(unsigned long long n) { return miller_rabin( n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022}); } template bool miller_rabin_mont(unsigned long long n, const std::vector &ws) { if (n <= 2) return n == 2; if (~n & 1) return false; if (mint::getmod() != n) mint::setmod(n); unsigned long long d = n - 1; while (~d & 1) d >>= 1; mint e = 1, rev = n - 1; for (unsigned long long w : ws) { if (w % n == 0) continue; unsigned long long t = d; mint y = mint(w).pow(t); while (t != n - 1 and y != e and y != rev) { y *= y; t <<= 1; } if (y != rev and ~t & 1) return false; } return true; } bool is_prime(unsigned long long n) { using mint32 = ArbitraryLazyMontgomeryModInt<54305750>; using mint64 = ArbitraryLazyMontgomeryModInt64bit<54305750>; if (n <= 2) return n == 2; if (~n & 1) return false; if (n < (1ull << 30)) { return miller_rabin_mont(n, {2, 7, 61}); } else if (n < (1ull << 62)) { return miller_rabin_mont(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022}); } else { return miller_rabin_u64(n); } } }; // namespace number_theory using number_theory::is_prime; }; // namespace kk2 #endif // KK2_MATH_IS_PRIME_HPP namespace kk2 { namespace number_theory { using i64 = long long; using u64 = unsigned long long; template T pollard_rho(T n) { if (~n & 1) return 2; if (is_prime(n)) return n; if (mint::getmod() != n) mint::setmod(n); mint R, one = 1; auto f = [&](mint x) { return x * x + R; }; auto _rng = [&]() { return kk2::random::rng(2, n); }; while (true) { mint x, y, ys, q = one; R = _rng(), y = _rng(); T g = 1; constexpr int m = 128; for (int r = 1; g == 1; r <<= 1) { x = y; for (int i = 0; i < r; i++) y = f(y); for (int k = 0; k < r && g == 1; k += m) { ys = y; for (int i = 0; i < std::min(m, r - k); i++) { y = f(y); q *= x - y; } g = std::gcd(q.val(), n); } } if (g == n) do { ys = f(ys); g = std::gcd((x - ys).val(), n); } while (g == 1); if (g != n) return g; } exit(1); } std::vector inner_factorize(i64 n) { using mint32 = ArbitraryLazyMontgomeryModInt<54355165>; using mint64 = ArbitraryLazyMontgomeryModInt64bit<54355165>; assert(n); if (n < 0) n = -n; if (n == 1) return {}; u64 p; if (n <= (1ll << 30)) { p = pollard_rho(n); } else if (n <= (1ll << 62)) { p = pollard_rho(n); } else { exit(1); } if (i64(p) == n) return {i64(p)}; auto l = inner_factorize(p); auto r = inner_factorize(n / p); std::copy(r.begin(), r.end(), std::back_inserter(l)); return l; } std::vector> factorize(i64 n) { auto tmp = inner_factorize(n); std::sort(tmp.begin(), tmp.end()); std::vector> res; for (int i = 0; i < (int)tmp.size(); i++) { if (i == 0 or res.back().first != tmp[i]) { res.emplace_back(tmp[i], 1); } else { res.back().second++; } } return res; } std::map factorize_map(i64 n) { auto tmp = inner_factorize(n); std::map res; for (auto x : tmp) res[x]++; return res; } std::vector divisors(i64 n) { if (n == 0) return {}; auto f = factorize(n); std::vector res = {1}; for (auto [p, k] : f) { int sz = res.size(); i64 x = 1; for (int i = 0; i < k; i++) { x *= p; for (int j = 0; j < sz; j++) { res.emplace_back(res[j] * x); } } } std::sort(res.begin(), res.end()); return res; } } // namespace number_theory using number_theory::divisors; using number_theory::factorize; using number_theory::factorize_map; } // namespace kk2 #endif // KK2_MATH_PRIME_FACTORIZE_HPP using namespace std; void solve() { /* 3K = N 頂点K+1の根付き木T(子供に順序あり), Tのroot以外の(子供の個数+1)の総積の総和 dp[i] := 頂点個数iのsum dp[0] = 0, dp[1] = 1 dp[i] = sum_{j=1}^{i-1} (j + 1) * sum_{sum(k)=i-1} dp[k_1] * ... * dp[k_j] f(X) = sum_{i} dp[i] * X^i f(X)(1-f(x))^2=X answer: [X^K](f(X) + f(X)^2 + ...) = [X^K](1-f(X))^{-1} 定数項が単元なら,逆元を定義可能 g(X) = (1-f(X))^{-1} - 1 -> g(X)/(1+g(X))^3 = X [X^K]g(X) = 1/K * [X^{K-1}](1+x)^3K = binom(3K, K-1)/K */ int n, m; kin >> n >> m; if (n % 3) { kout << 0 << kendl; return; } using mint = kk2::ModInt<0>; mint::setmod(m); int k = n / 3; auto pf = kk2::factorize(m); mint res = 1; vc v_ps(pf.size()); auto multiply = [&](int i, bool inv) { rep (j, pf.size()) { int p = pf[j].first; int v = 0; while (i % p == 0) { i /= p; v++; } if (inv) v_ps[j] -= v; else v_ps[j] += v; } if (inv) res /= mint(i); else res *= mint(i); }; multiply(k, 1); rep (i, k - 1) multiply(3 * k - i, 0), multiply(k - i - 1, 1); rep (i, pf.size()) { int p = pf[i].first; int v = v_ps[i]; assert(v >= 0); res *= mint(p).pow(v); } kout << res << kendl; } int main() { int t = 1; // kin >> t; rep (t) solve(); return 0; } // Author: kk2 // converted by https://github.com/kk2a/cpp-bundle // 2025-05-17 19:21:07