#define PROBLEM "https://yukicoder.me/problems/no/2005" #include #include #include using mint = atcoder::modint998244353; std::istream& operator>>(std::istream& in, mint &a) { long long e; in >> e; a = e; return in; } std::ostream& operator<<(std::ostream& out, const mint &a) { out << a.val(); return out; } #include #include #include #include #include #include #include namespace suisen { // ! utility template using constraints_t = std::enable_if_t, std::nullptr_t>; template constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) { if constexpr (cond_v) { return std::forward(then); } else { return std::forward(or_else); } } // ! function template using is_same_as_invoke_result = std::is_same, ReturnType>; template using is_uni_op = is_same_as_invoke_result; template using is_bin_op = is_same_as_invoke_result; template using is_comparator = std::is_same, bool>; // ! integral template >> constexpr int bit_num = std::numeric_limits>::digits; template struct is_nbit { static constexpr bool value = bit_num == n; }; template static constexpr bool is_nbit_v = is_nbit::value; // ? template struct safely_multipliable {}; template <> struct safely_multipliable { using type = long long; }; template <> struct safely_multipliable { using type = __int128_t; }; template <> struct safely_multipliable { using type = unsigned long long; }; template <> struct safely_multipliable { using type = __uint128_t; }; template <> struct safely_multipliable { using type = __uint128_t; }; template <> struct safely_multipliable { using type = float; }; template <> struct safely_multipliable { using type = double; }; template <> struct safely_multipliable { using type = long double; }; template using safely_multipliable_t = typename safely_multipliable::type; template struct rec_value_type { using type = T; }; template struct rec_value_type> { using type = typename rec_value_type::type; }; template using rec_value_type_t = typename rec_value_type::type; } // namespace suisen #include /** * refernce: https://37zigen.com/tonelli-shanks-algorithm/ * calculates x s.t. x^2 = a mod p in O((log p)^2). */ template std::optional optional_sqrt(mint a) { static int p = mint::mod(); if (a == 0) return std::make_optional(0); if (p == 2) return std::make_optional(a); if (a.pow((p - 1) / 2) != 1) return std::nullopt; mint b = 1; while (b.pow((p - 1) / 2) == 1) ++b; static int tlz = __builtin_ctz(p - 1), q = (p - 1) >> tlz; mint x = a.pow((q + 1) / 2); b = b.pow(q); for (int shift = 2; x * x != a; ++shift) { mint e = a.inv() * x * x; if (e.pow(1 << (tlz - shift)) != 1) x *= b; b *= b; } return std::make_optional(x); } /** * calculates x s.t. x^2 = a mod p in O((log p)^2). * if not exists, raises runtime error. */ template auto sqrt(mint a) -> decltype(mint::mod(), mint()) { return *optional_sqrt(a); } template auto log(mint a) -> decltype(mint::mod(), mint()) { assert(a == 1); return 0; } template auto exp(mint a) -> decltype(mint::mod(), mint()) { assert(a == 0); return 1; } template auto pow(mint a, T b) -> decltype(mint::mod(), mint()) { return a.pow(b); } template auto inv(mint a) -> decltype(mint::mod(), mint()) { return a.inv(); } namespace suisen { template class inv_mods { public: inv_mods() {} inv_mods(int n) { ensure(n); } const mint& operator[](int i) const { ensure(i); return invs[i]; } static void ensure(int n) { int sz = invs.size(); if (sz < 2) invs = {0, 1}, sz = 2; if (sz < n + 1) { invs.resize(n + 1); for (int i = sz; i <= n; ++i) invs[i] = mint(mod - mod / i) * invs[mod % i]; } } private: static std::vector invs; static constexpr int mod = mint::mod(); }; template std::vector inv_mods::invs{}; } namespace suisen { template struct FPSNaive : std::vector { static inline int MAX_DEG = std::numeric_limits::max() / 2; using value_type = T; using element_type = rec_value_type_t; using std::vector::vector; FPSNaive(const std::initializer_list l) : std::vector::vector(l) {} static void set_max_deg(int max_deg) { FPSNaive::MAX_DEG = max_deg; } const value_type operator[](int n) const { return n <= deg() ? unsafe_get(n) : value_type{ 0 }; } value_type& operator[](int n) { return ensure_deg(n), unsafe_get(n); } int size() const { return std::vector::size(); } int deg() const { return size() - 1; } int normalize() { while (size() and this->back() == value_type{ 0 }) this->pop_back(); return deg(); } FPSNaive& cut_inplace(int max_deg) { if (deg() > max_deg) this->resize(std::max(0, max_deg + 1)); return *this; } FPSNaive cut(int max_deg) const { return FPSNaive(*this).cut_inplace(max_deg); } FPSNaive operator+() const { return FPSNaive(*this); } FPSNaive operator-() const { FPSNaive f(*this); for (auto& e : f) e = -e; return f; } FPSNaive& operator++() { return ++(*this)[0], * this; } FPSNaive& operator--() { return --(*this)[0], * this; } FPSNaive& operator+=(const value_type x) { return (*this)[0] += x, *this; } FPSNaive& operator-=(const value_type x) { return (*this)[0] -= x, *this; } FPSNaive& operator+=(const FPSNaive& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) += g.unsafe_get(i); return *this; } FPSNaive& operator-=(const FPSNaive& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) -= g.unsafe_get(i); return *this; } FPSNaive& operator*=(const FPSNaive& g) { return *this = *this * g; } FPSNaive& operator*=(const value_type x) { for (auto& e : *this) e *= x; return *this; } FPSNaive& operator/=(const FPSNaive& g) { return *this = *this / g; } FPSNaive& operator%=(const FPSNaive& g) { return *this = *this % g; } FPSNaive& operator<<=(const int shamt) { this->insert(this->begin(), shamt, value_type { 0 }); return *this; } FPSNaive& operator>>=(const int shamt) { if (shamt > size()) this->clear(); else this->erase(this->begin(), this->begin() + shamt); return *this; } friend FPSNaive operator+(FPSNaive f, const FPSNaive& g) { f += g; return f; } friend FPSNaive operator+(FPSNaive f, const value_type &x) { f += x; return f; } friend FPSNaive operator-(FPSNaive f, const FPSNaive& g) { f -= g; return f; } friend FPSNaive operator-(FPSNaive f, const value_type &x) { f -= x; return f; } friend FPSNaive operator*(const FPSNaive &f, const FPSNaive& g) { if (f.empty() or g.empty()) return FPSNaive{}; const int n = f.size(), m = g.size(); FPSNaive h(std::min(MAX_DEG + 1, n + m - 1)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (i + j > MAX_DEG) break; h.unsafe_get(i + j) += f.unsafe_get(i) * g.unsafe_get(j); } return h; } friend FPSNaive operator*(FPSNaive f, const value_type &x) { f *= x; return f; } friend FPSNaive operator/(FPSNaive f, FPSNaive g) { return std::move(div_mod(std::move(f), std::move(g)).first); } friend FPSNaive operator%(FPSNaive f, FPSNaive g) { return std::move(div_mod(std::move(f), std::move(g)).second); } friend FPSNaive operator*(const value_type x, FPSNaive f) { f *= x; return f; } friend FPSNaive operator<<(FPSNaive f, const int shamt) { f <<= shamt; return f; } friend FPSNaive operator>>(FPSNaive f, const int shamt) { f >>= shamt; return f; } friend std::pair div_mod(FPSNaive f, FPSNaive g) { const int fd = f.normalize(), gd = g.normalize(); assert(gd >= 0); if (fd < gd) return { FPSNaive{}, f }; if (gd == 0) return { f *= g.unsafe_get(0).inv(), FPSNaive{} }; const int k = f.deg() - gd; value_type head_inv = g.unsafe_get(gd).inv(); FPSNaive q(k + 1); for (int i = k; i >= 0; --i) { value_type div = f.unsafe_get(i + gd) * head_inv; q.unsafe_get(i) = div; for (int j = 0; j <= gd; ++j) f.unsafe_get(i + j) -= div * g.unsafe_get(j); } return { q, f.cut_inplace(gd - 1) }; } friend bool operator==(const FPSNaive& f, const FPSNaive& g) { const int n = f.size(), m = g.size(); if (n < m) return g == f; for (int i = 0; i < m; ++i) if (f.unsafe_get(i) != g.unsafe_get(i)) return false; for (int i = m; i < n; ++i) if (f.unsafe_get(i) != 0) return false; return true; } friend bool operator!=(const FPSNaive& f, const FPSNaive& g) { return not (f == g); } FPSNaive mul(const FPSNaive& g, int max_deg) const { if (this->empty() or g.empty()) return FPSNaive{}; const int n = size(), m = g.size(); FPSNaive h(std::min(max_deg + 1, n + m - 1)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (i + j > max_deg) break; h.unsafe_get(i + j) += unsafe_get(i) * g.unsafe_get(j); } return h; } FPSNaive diff() const { if (this->empty()) return {}; FPSNaive g(size() - 1); for (int i = 1; i <= deg(); ++i) g.unsafe_get(i - 1) = unsafe_get(i) * i; return g; } FPSNaive intg() const { const int n = size(); FPSNaive g(n + 1); for (int i = 0; i < n; ++i) g.unsafe_get(i + 1) = unsafe_get(i) * invs[i + 1]; if (g.deg() > MAX_DEG) g.cut_inplace(MAX_DEG); return g; } FPSNaive inv(int max_deg) const { FPSNaive g(max_deg + 1); const value_type inv_f0 = ::inv(unsafe_get(0)); g.unsafe_get(0) = inv_f0; for (int i = 1; i <= max_deg; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) -= g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= inv_f0; } return g; } FPSNaive exp(int max_deg) const { assert(unsafe_get(0) == value_type{ 0 }); FPSNaive g(max_deg + 1); g.unsafe_get(0) = value_type{ 1 }; for (int i = 1; i <= max_deg; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) += j * g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= invs[i]; } return g; } FPSNaive log(int max_deg) const { assert(unsafe_get(0) == value_type{ 1 }); FPSNaive g(max_deg + 1); g.unsafe_get(0) = value_type{ 0 }; for (int i = 1; i <= max_deg; ++i) { g.unsafe_get(i) = i * (*this)[i]; for (int j = 1; j < i; ++j) g.unsafe_get(i) -= (i - j) * g.unsafe_get(i - j) * (*this)[j]; g.unsafe_get(i) *= invs[i]; } return g; } FPSNaive pow(const long long k, int max_deg) const { if (k == 0) return { value_type{ 1 } }; int z = 0; while (z < size() and unsafe_get(z) == value_type{ 0 }) ++z; if (z == size() or z > max_deg / k) return FPSNaive{}; const int d = max_deg - z * k; FPSNaive g(d + 1); const value_type inv_f0 = ::inv(unsafe_get(z)); g.unsafe_get(0) = unsafe_get(z).pow(k); for (int i = 1; i <= d; ++i) { for (int j = 1; j <= i; ++j) g.unsafe_get(i) += (element_type{ k } * j - (i - j)) * g.unsafe_get(i - j) * (*this)[z + j]; g.unsafe_get(i) *= inv_f0 * invs[i]; } g <<= z * k; return g; } std::optional optional_sqrt(int max_deg) const { int dl = 0; while (dl < size() and unsafe_get(dl) == value_type{ 0 }) ++dl; if (dl == size()) return FPSNaive{}; if (dl & 1) return std::nullopt; const int d = max_deg - dl / 2; FPSNaive g(d + 1); auto opt_g0 = ::optional_sqrt((*this)[dl]); if (not opt_g0.has_value()) return std::nullopt; g.unsafe_get(0) = *opt_g0; value_type inv_2g0 = ::inv(2 * g.unsafe_get(0)); for (int i = 1; i <= d; ++i) { g.unsafe_get(i) = (*this)[dl + i]; for (int j = 1; j < i; ++j) g.unsafe_get(i) -= g.unsafe_get(j) * g.unsafe_get(i - j); g.unsafe_get(i) *= inv_2g0; } g <<= dl / 2; return g; } FPSNaive sqrt(int max_deg) const { return *optional_sqrt(max_deg); } value_type eval(value_type x) const { value_type y = 0; for (int i = size() - 1; i >= 0; --i) y = y * x + unsafe_get(i); return y; } private: static inline inv_mods invs; void ensure_deg(int d) { if (deg() < d) this->resize(d + 1, value_type{ 0 }); } const value_type& unsafe_get(int i) const { return std::vector::operator[](i); } value_type& unsafe_get(int i) { return std::vector::operator[](i); } }; } // namespace suisen template auto sqrt(suisen::FPSNaive a) -> decltype(mint::mod(), suisen::FPSNaive{}) { return a.sqrt(suisen::FPSNaive::MAX_DEG == std::numeric_limits::max() / 2 ? suisen::FPSNaive::MAX_DEG : a.deg()); } template auto log(suisen::FPSNaive a) -> decltype(mint::mod(), suisen::FPSNaive{}) { return a.log(suisen::FPSNaive::MAX_DEG == std::numeric_limits::max() / 2 ? suisen::FPSNaive::MAX_DEG : a.deg()); } template auto exp(suisen::FPSNaive a) -> decltype(mint::mod(), suisen::FPSNaive{}) { return a.exp(suisen::FPSNaive::MAX_DEG == std::numeric_limits::max() / 2 ? suisen::FPSNaive::MAX_DEG : a.deg()); } template auto pow(suisen::FPSNaive a, T b) -> decltype(mint::mod(), suisen::FPSNaive{}) { return a.pow(b, suisen::FPSNaive::MAX_DEG == std::numeric_limits::max() / 2 ? suisen::FPSNaive::MAX_DEG : a.deg()); } template auto inv(suisen::FPSNaive a) -> decltype(mint::mod(), suisen::FPSNaive{}) { return a.inv(suisen::FPSNaive::MAX_DEG == std::numeric_limits::max() / 2 ? suisen::FPSNaive::MAX_DEG : a.deg()); } namespace suisen { template using convolution_t = std::vector(*)(const std::vector&, const std::vector&); template struct FPS : public std::vector { using std::vector::vector; FPS(const std::initializer_list l) : std::vector::vector(l) {} FPS(const std::vector& v) : std::vector::vector(v) {} FPS(std::vector&& v) : std::vector::vector(std::move(v)) {} static void set_multiplication(convolution_t multiplication) { FPS::mult = multiplication; } const mint operator[](int n) const noexcept { return n <= deg() ? unsafe_get(n) : 0; } mint& operator[](int n) noexcept { ensure_deg(n); return unsafe_get(n); } int size() const noexcept { return std::vector::size(); } int deg() const noexcept { return size() - 1; } int normalize() { while (this->size() and this->back() == 0) this->pop_back(); return deg(); } FPS& pre_inplace(int max_deg) noexcept { if (deg() > max_deg) this->resize(std::max(0, max_deg + 1)); return *this; } FPS pre(int max_deg) const noexcept { return FPS(*this).pre_inplace(max_deg); } FPS operator+() const { return FPS(*this); } FPS operator-() const { FPS f(*this); for (auto& e : f) e = mint::mod() - e; return f; } FPS& operator++() { ++(*this)[0]; return *this; } FPS& operator--() { --(*this)[0]; return *this; } FPS& operator+=(const mint x) { (*this)[0] += x; return *this; } FPS& operator-=(const mint x) { (*this)[0] -= x; return *this; } FPS& operator+=(const FPS& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) += g.unsafe_get(i); return *this; } FPS& operator-=(const FPS& g) { ensure_deg(g.deg()); for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) -= g.unsafe_get(i); return *this; } FPS& operator*=(const FPS& g) { return *this = FPS::mult(*this, g); } FPS& operator*=(const mint x) { for (auto& e : *this) e *= x; return *this; } FPS& operator/=(FPS g) { const int fd = normalize(), gd = g.normalize(); assert(gd >= 0); if (fd < gd) { this->clear(); return *this; } if (gd == 0) return *this *= g.unsafe_get(0).inv(); static constexpr int THRESHOLD_NAIVE_POLY_QUOTIENT = 256; if (gd <= THRESHOLD_NAIVE_POLY_QUOTIENT) { *this = std::move(naive_div_inplace(std::move(g), gd).first); return *this; } std::reverse(this->begin(), this->end()), std::reverse(g.begin(), g.end()); const int k = fd - gd; *this *= g.inv_inplace(k), this->resize(k + 1); std::reverse(this->begin(), this->end()); return *this; } FPS& operator%=(FPS g) { int fd = normalize(), gd = g.normalize(); assert(gd >= 0); if (fd < gd) return *this; if (gd == 0) { this->clear(); return *this; } static constexpr int THRESHOLD_NAIVE_REMAINDER = 256; if (gd <= THRESHOLD_NAIVE_REMAINDER) return naive_div_inplace(std::move(g), gd).second; *this -= g * (*this / g); return pre_inplace(gd - 1); } FPS& operator<<=(const int shamt) { this->insert(this->begin(), shamt, 0); return *this; } FPS& operator>>=(const int shamt) { if (shamt > size()) this->clear(); else this->erase(this->begin(), this->begin() + shamt); return *this; } friend FPS operator+(FPS f, const FPS& g) { f += g; return f; } friend FPS operator+(FPS f, const mint x) { f += x; return f; } friend FPS operator-(FPS f, const FPS& g) { f -= g; return f; } friend FPS operator-(FPS f, const mint x) { f -= x; return f; } friend FPS operator*(FPS f, const FPS& g) { f *= g; return f; } friend FPS operator*(FPS f, const mint x) { f *= x; return f; } friend FPS operator/(FPS f, const FPS& g) { f /= g; return f; } friend FPS operator%(FPS f, const FPS& g) { f %= g; return f; } friend FPS operator*(const mint x, FPS f) { f *= x; return f; } friend FPS operator<<(FPS f, const int shamt) { f <<= shamt; return f; } friend FPS operator>>(FPS f, const int shamt) { f >>= shamt; return f; } friend bool operator==(const FPS& f, const FPS& g) { int n = f.size(), m = g.size(); if (n < m) return g == f; for (int i = 0; i < m; ++i) if (f.unsafe_get(i) != g.unsafe_get(i)) return false; for (int i = m; i < n; ++i) if (f.unsafe_get(i) != 0) return false; return true; } FPS& diff_inplace() { if (this->size() == 0) return *this; for (int i = 1; i <= deg(); ++i) unsafe_get(i - 1) = unsafe_get(i) * i; this->pop_back(); return *this; } FPS& intg_inplace() { int d = deg(); ensure_deg(d + 1); for (int i = d; i >= 0; --i) unsafe_get(i + 1) = unsafe_get(i) * invs[i + 1]; unsafe_get(0) = 0; return *this; } FPS& inv_inplace(const int max_deg) { if (max_deg <= 60) return *this = FPSNaive(this->begin(), this->end()).inv(max_deg); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return *this = inv_sparse(std::move(*sp_f), max_deg); FPS res{ unsafe_get(0).inv() }; for (int k = 1; k <= max_deg; k *= 2) { FPS tmp(this->pre(k * 2) * (res * res)); res *= 2, res -= tmp.pre_inplace(2 * k); } return *this = std::move(res), pre_inplace(max_deg); } FPS& log_inplace(const int max_deg) { if (max_deg <= 60) return *this = FPSNaive(this->begin(), this->end()).log(max_deg); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return *this = log_sparse(std::move(*sp_f), max_deg); FPS f_inv = inv(max_deg); diff_inplace(), *this *= f_inv, pre_inplace(max_deg - 1), intg_inplace(); return *this; } FPS& exp_inplace(const int max_deg) { if (max_deg <= 60) return *this = FPSNaive(this->begin(), this->end()).exp(max_deg); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return *this = exp_sparse(std::move(*sp_f), max_deg); FPS res{ 1 }; for (int k = 1; k <= max_deg; k *= 2) res *= ++(pre(k * 2) - res.log(k * 2)), res.pre_inplace(k * 2); return *this = std::move(res), pre_inplace(max_deg); } FPS& sqrt_inplace(const int max_deg) { return *this = sqrt(max_deg); } FPS& pow_inplace(const long long k, const int max_deg) { if (k == 0) return *this = { mint{ 1 } }; if (max_deg <= 60) return *this = FPSNaive(this->begin(), this->end()).pow(k, max_deg); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return *this = pow_sparse(std::move(*sp_f), k, max_deg); int tlz = 0; while (tlz <= deg() and unsafe_get(tlz) == 0) ++tlz; if (tlz > deg() or tlz > max_deg / k) return this->clear(), *this; const int d = max_deg - tlz * k; *this >>= tlz; mint base = (*this)[0]; *this *= base.inv(), log_inplace(d), *this *= k, exp_inplace(d), *this *= base.pow(k); return *this <<= tlz * k; } FPS diff() const { FPS f{ *this }; f.diff_inplace(); return f; } FPS intg() const { FPS f{ *this }; f.intg_inplace(); return f; } FPS inv(const int max_deg) const { FPS f{ *this }; f.inv_inplace(max_deg); return f; } FPS log(const int max_deg) const { FPS f{ *this }; f.log_inplace(max_deg); return f; } FPS exp(const int max_deg) const { FPS f{ *this }; f.exp_inplace(max_deg); return f; } std::optional optional_sqrt(const int max_deg) { if (max_deg <= 60) return FPSNaive(this->begin(), this->end()).optional_sqrt(max_deg); if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return optional_sqrt_sparse(std::move(*sp_f), max_deg); int tlz = 0; while (tlz <= deg() and unsafe_get(tlz) == 0) ++tlz; if (tlz > deg()) return FPS{}; if (tlz % 2 == 1) return std::nullopt; int max_deg2 = max_deg - tlz / 2; FPS f(this->begin() + tlz, this->end()); auto opt_res0 = ::optional_sqrt(f[0]); if (not opt_res0.has_value()) return std::nullopt; FPS res{ *opt_res0 }; mint inv_2 = mint(2).inv(); for (int k = 1; k <= max_deg2; k *= 2) { res = ((f.pre(k * 2) * res.inv(2 * k)).pre_inplace(2 * k) += res) *= inv_2; } return *this = std::move(res <<= tlz / 2), pre_inplace(max_deg); } FPS sqrt(const int max_deg) const { return *optional_sqrt(max_deg); } FPS pow(const long long k, const int max_deg) const { FPS f{ *this }; f.pow_inplace(k, max_deg); return f; } mint eval(mint x) const { mint y = 0; for (int i = size() - 1; i >= 0; --i) y = y * x + unsafe_get(i); return y; } private: static inline inv_mods invs; static convolution_t mult; void ensure_deg(int d) { if (deg() < d) this->resize(d + 1, 0); } const mint& unsafe_get(int i) const { return std::vector::operator[](i); } mint& unsafe_get(int i) { return std::vector::operator[](i); } std::optional>> sparse_fps_format(int max_size) const { std::vector> res; for (int i = 0; i <= deg() and int(res.size()) <= max_size; ++i) if (mint v = unsafe_get(i); v != 0) res.emplace_back(i, v); if (int(res.size()) > max_size) return std::nullopt; return res; } std::pair naive_div_inplace(FPS&& g, const int gd) { const int k = deg() - gd; mint head_inv = g.unsafe_get(gd).inv(); FPS q(k + 1); for (int i = k; i >= 0; --i) { mint div = this->unsafe_get(i + gd) * head_inv; q.unsafe_get(i) = div; for (int j = 0; j <= gd; ++j) this->unsafe_get(i + j) -= div * g.unsafe_get(j); } return { q, pre_inplace(gd - 1) }; } static FPS div_fps_sparse(const FPS &f, const std::vector> &g, const int max_deg) { const int siz = g.size(); assert(siz and g[0].first == 0); const mint inv_g0 = g[0].second.inv(); FPS h(max_deg + 1); for (int i = 0; i <= max_deg; ++i) { mint v = f[i]; for (int idx = 1; idx < siz; ++idx) { const auto &[j, gj] = g[idx]; if (j > i) break; v -= gj * h.unsafe_get(i - j); } h.unsafe_get(i) = v * inv_g0; } return h; } static FPS inv_sparse(const std::vector> &g, const int max_deg) { return div_fps_sparse(FPS { mint{1} }, g, max_deg); } static FPS exp_sparse(const std::vector> &f, const int max_deg) { const int siz = f.size(); assert(not siz or f[0].first != 0); FPS g(max_deg + 1); g[0] = 1; for (int i = 1; i <= max_deg; ++i) { mint v = 0; for (const auto &[j, fj] : f) { if (j > i) break; v += j * fj * g.unsafe_get(i - j); } v *= invs[i]; g.unsafe_get(i) = v; } return g; } static FPS log_sparse(const std::vector> &f, const int max_deg) { const int siz = f.size(); assert(siz and f[0].first == 0 and f[0].second == 1); FPS g(max_deg + 1); for (int idx = 1; idx < siz; ++idx) { const auto &[j, fj] = f[idx]; if (j > max_deg) break; g.unsafe_get(j) = j * fj; } for (int i = 1; i <= max_deg; ++i) { mint v = g.unsafe_get(i); for (int idx = 1; idx < siz; ++idx) { const auto &[j, fj] = f[idx]; if (j > i) break; v -= fj * g.unsafe_get(i - j) * (i - j); } v *= invs[i]; g.unsafe_get(i) = v; } return g; } static FPS pow_sparse(const std::vector> &f, const long long k, const int max_deg) { if (k == 0) return FPS { mint{1} }; const int siz = f.size(); if (not siz) return FPS{}; const int p = f[0].first; if (p >= max_deg / k + 1) return FPS{}; const mint inv_f0 = f[0].second.inv(); const int lz = p * k; FPS g(max_deg + 1); g[lz] = f[0].second.pow(k); for (int i = 1; lz + i <= max_deg; ++i) { mint v = 0; for (int idx = 1; idx < siz; ++idx) { auto [j, fj] = f[idx]; j -= p; if (j > i) break; v += fj * g.unsafe_get(lz + i - j) * (mint(k) * j - (i - j)); } v *= invs[i] * inv_f0; g.unsafe_get(lz + i) = v; } return g; } static std::optional optional_sqrt_sparse(const std::vector> &f, const int max_deg) { const int siz = f.size(); if (not siz) return FPS{}; const int p = f[0].first; if (p % 2 == 1) return std::nullopt; if (p / 2 > max_deg) return FPS{}; const mint inv_f0 = f[0].second.inv(); const int lz = p / 2; FPS g(max_deg + 1); auto opt_g0 = ::optional_sqrt(f[0].second); if (not opt_g0.has_value()) return std::nullopt; g[lz] = *opt_g0; mint k = mint(2).inv(); for (int i = 1; lz + i <= max_deg; ++i) { mint v = 0; for (int idx = 1; idx < siz; ++idx) { auto [j, fj] = f[idx]; j -= p; if (j > i) break; v += fj * g.unsafe_get(lz + i - j) * (k * j - (i - j)); } v *= invs[i] * inv_f0; g.unsafe_get(lz + i) = v; } return g; } static FPS sqrt_sparse(const std::vector> &f, const int max_deg) { return *optional_sqrt(f, max_deg); } }; template convolution_t FPS::mult = [](const auto&, const auto&) { std::cerr << "convolution function is not available." << std::endl; assert(false); return std::vector{}; }; } // namespace suisen template auto sqrt(suisen::FPS a) -> decltype(mint::mod(), suisen::FPS{}) { return a.sqrt(a.deg()); } template auto log(suisen::FPS a) -> decltype(mint::mod(), suisen::FPS{}) { return a.log(a.deg()); } template auto exp(suisen::FPS a) -> decltype(mint::mod(), suisen::FPS{}) { return a.exp(a.deg()); } template auto pow(suisen::FPS a, T b) -> decltype(mint::mod(), suisen::FPS{}) { return a.pow(b, a.deg()); } template auto inv(suisen::FPS a) -> decltype(mint::mod(), suisen::FPS{}) { return a.inv(a.deg()); } namespace suisen { template struct factorial { factorial() {} factorial(int n) { ensure(n); } static void ensure(const int n) { int sz = _fac.size(); if (n + 1 <= sz) return; int new_size = std::max(n + 1, sz * 2); _fac.resize(new_size), _fac_inv.resize(new_size); for (int i = sz; i < new_size; ++i) _fac[i] = _fac[i - 1] * i; _fac_inv[new_size - 1] = U(1) / _fac[new_size - 1]; for (int i = new_size - 1; i > sz; --i) _fac_inv[i - 1] = _fac_inv[i] * i; } T fac(const int i) { ensure(i); return _fac[i]; } T operator()(int i) { return fac(i); } U fac_inv(const int i) { ensure(i); return _fac_inv[i]; } U binom(const int n, const int r) { if (n < 0 or r < 0 or n < r) return 0; ensure(n); return _fac[n] * _fac_inv[r] * _fac_inv[n - r]; } U perm(const int n, const int r) { if (n < 0 or r < 0 or n < r) return 0; ensure(n); return _fac[n] * _fac_inv[n - r]; } private: static std::vector _fac; static std::vector _fac_inv; }; template std::vector factorial::_fac{ 1 }; template std::vector factorial::_fac_inv{ 1 }; } // namespace suisen #include #include namespace suisen { // referece: https://37zigen.com/linear-sieve/ class LinearSieve { public: LinearSieve(const int n) : _n(n), min_prime_factor(std::vector(n + 1)) { std::iota(min_prime_factor.begin(), min_prime_factor.end(), 0); prime_list.reserve(_n / 20); for (int d = 2; d <= _n; ++d) { if (min_prime_factor[d] == d) prime_list.push_back(d); const int prime_max = std::min(min_prime_factor[d], _n / d); for (int prime : prime_list) { if (prime > prime_max) break; min_prime_factor[prime * d] = prime; } } } int prime_num() const noexcept { return prime_list.size(); } /** * Returns a vector of primes in [0, n]. * It is guaranteed that the returned vector is sorted in ascending order. */ const std::vector& get_prime_list() const noexcept { return prime_list; } const std::vector& get_min_prime_factor() const noexcept { return min_prime_factor; } /** * Returns a vector of `{ prime, index }`. * It is guaranteed that the returned vector is sorted in ascending order. */ std::vector> factorize(int n) const noexcept { assert(0 < n and n <= _n); std::vector> prime_powers; while (n > 1) { int p = min_prime_factor[n], c = 0; do { n /= p, ++c; } while (n % p == 0); prime_powers.emplace_back(p, c); } return prime_powers; } private: const int _n; std::vector min_prime_factor; std::vector prime_list; }; } // namespace suisen namespace suisen { // returns { 0^k, 1^k, ..., n^k } template std::vector powers(uint32_t n, uint64_t k) { const auto mpf = LinearSieve(n).get_min_prime_factor(); std::vector res(n + 1); res[0] = k == 0; for (uint32_t i = 1; i <= n; ++i) res[i] = i == 1 ? 1 : uint32_t(mpf[i]) == i ? mint(i).pow(k) : res[mpf[i]] * res[i / mpf[i]]; return res; } } // namespace suisen // reference: https://en.wikipedia.org/wiki/Eulerian_number namespace suisen { template std::vector eulerian_number(uint32_t n) { if (n == 0) return {}; factorial fac(n + 1); const uint32_t h = (n + 1) >> 1; FPS f = powers(h, n); f.erase(f.begin()); FPS g(h); for (uint32_t i = 0; i < h; ++i) { mint v = fac.fac_inv(i) * fac.fac_inv(n + 1 - i); g[i] = i & 1 ? -v : v; } FPS res = f * g; for (uint32_t i = h; i < n; ++i) res[i] = res[n - 1 - i]; res.resize(n); return res; } template std::vector> eulerian_number_table(uint32_t n) { if (n == 0) return {}; std::vector dp(n + 1, std::vector{}); for (uint32_t i = 1; i <= n; ++i) { dp[i].resize(i); dp[i][0] = dp[i][i - 1] = 1; for (uint32_t j = 1; j < i - 1; ++j) dp[i][j] = (i - j) * dp[i - 1][j - 1] + (j + 1) * dp[i - 1][j]; } return dp; } } // namespace suisen /** * [Idea] reference : https://motsu-xe.hatenablog.com/entry/2021/05/13/224016 * * SWAG + simulate a deque with 2 stacks * * [Operations] reference : https://www.slideshare.net/catupper/amortize-analysis-of-deque-with-2-stack * * `l`, `r` is a stack of { value, sum } * * accumulate * <---------- ------> fold values from inside * ( l ][ r ) * * pop_front: * 1. `l` is not empty * ( l ][ r ) -> ( l ][ r ) # pop from `l`. O(1) * 2. `l` is empty * (][ r ) -> ( l ][ r ) # split `r` at its middle point. amortized O(1) * ( l ][ r ) -> ( l ][ r ) # pop from `l`. O(1) * * pop_back: * 1. `r` is not empty * ( l ][ r ) -> ( l ][ r ) # pop from `r`. O(1) * 2. `r` is empty * ( l ][) -> ( l ][ r ) # split `l` at its middle point. amortized O(1) * ( l ][ r ) -> ( l ][ r ) # pop from `r`. O(1) * * push_front: * ( l ][ r ) -> ( l ][ r ) # push to `l`. O(1) * * push_back: * ( l ][ r ) -> ( l ][ r ) # push to `r`. O(1) */ namespace suisen { template struct DequeAggregation { struct DequeAggregationIterator { using difference_type = int; using value_type = T; using pointer = value_type*; using reference = value_type&; using iterator_category = std::random_access_iterator_tag; using fi_iterator_type = typename std::vector>::const_reverse_iterator; using se_iterator_type = typename std::vector>::const_iterator; fi_iterator_type it_l; fi_iterator_type it_l_end; se_iterator_type it_r_begin; se_iterator_type it_r; DequeAggregationIterator& operator++() { if (it_l == it_l_end) ++it_r; else ++it_l; return *this; } DequeAggregationIterator operator++(int) { DequeAggregationIterator ret = *this; ++(*this); return ret; } DequeAggregationIterator& operator--() { if (it_r == it_r_begin) --it_l; else --it_r; return *this; } DequeAggregationIterator operator--(int) { DequeAggregationIterator ret = *this; --(*this); return ret; } DequeAggregationIterator& operator+=(difference_type dif) { if (dif < 0) return *this -= -dif; if (int d = it_l_end - it_l; d < dif) it_l = it_l_end, it_r += dif - d; else it_l += dif; return *this; } DequeAggregationIterator operator+(difference_type dif) const { DequeAggregationIterator it = *this; it += dif; return it; } DequeAggregationIterator& operator-=(difference_type dif) { if (dif < 0) return *this += -dif; if (int d = it_r - it_r_begin; d < dif) it_r = it_r_begin, it_l -= dif - d; else it_r -= dif; return *this; } DequeAggregationIterator operator-(difference_type dif) const { DequeAggregationIterator it = *this; it -= dif; return it; } difference_type operator-(const DequeAggregationIterator &rhs) const { difference_type d1 = it_l == it_l_end ? it_r - it_r_begin : it_l - it_l_end; difference_type d2 = rhs.it_l == rhs.it_l_end ? rhs.it_r - rhs.it_r_begin : rhs.it_l - rhs.it_l_end; return d1 - d2; } const value_type& operator[](difference_type i) const { return *((*this) + i); } const value_type& operator*() const { return it_l == it_l_end ? it_r->first : it_l->first; } bool operator!=(const DequeAggregationIterator &rhs) const { return it_l != rhs.it_l or it_r != rhs.it_r; } bool operator==(const DequeAggregationIterator &rhs) const { return not (*this != rhs); } bool operator<(const DequeAggregationIterator &rhs) const { return (*this) - rhs < 0; } bool operator<=(const DequeAggregationIterator &rhs) const { return (*this) - rhs <= 0; } bool operator>(const DequeAggregationIterator &rhs) const { return (*this) - rhs > 0; } bool operator>=(const DequeAggregationIterator &rhs) const { return (*this) - rhs >= 0; } }; using iterator = DequeAggregationIterator; using difference_type = typename iterator::difference_type; using value_type = typename iterator::value_type; using pointer = typename iterator::pointer; using reference = typename iterator::reference; DequeAggregation() = default; template , std::nullptr_t> = nullptr> DequeAggregation(InputIterator first, InputIterator last) { for (; first != last; ++first) push_back(*first); } template , std::nullptr_t> = nullptr> DequeAggregation(const Container &c) : DequeAggregation(std::begin(c), std::end(c)) {} value_type prod() const { return op(prod(_st_l), prod(_st_r)); } void push_back(const value_type &val) { _st_r.emplace_back(val, op(prod(_st_r), val)); } void push_front(const value_type &val) { _st_l.emplace_back(val, op(val, prod(_st_l))); } void pop_back() { if (_st_r.size()) return _st_r.pop_back(); const int siz = _st_l.size(); const int l = siz >> 1, r = siz - l; assert(r); // <=> siz > 0 for (int i = r - 1; i > 0; --i) push_back(std::move(_st_l[i].first)); _st_l.erase(_st_l.begin(), _st_l.begin() + r); if (l == 0) return; _st_l[0].second = _st_l[0].first; for (int i = 1; i < l; ++i) _st_l[i].second = op(_st_l[i].first, _st_l[i - 1].second); } void pop_front() { if (_st_l.size()) return _st_l.pop_back(); const int siz = _st_r.size(); const int r = siz >> 1, l = siz - r; assert(l); // <=> siz > 0 for (int i = l - 1; i > 0; --i) push_front(std::move(_st_r[i].first)); _st_r.erase(_st_r.begin(), _st_r.begin() + l); if (r == 0) return; _st_r[0].second = _st_r[0].first; for (int i = 1; i < r; ++i) _st_r[i].second = op(_st_r[i - 1].second, _st_r[i].first); } const value_type& front() const { return _st_l.size() ? _st_l.back().first : _st_r.front().first; } const value_type& back() const { return _st_r.size() ? _st_r.back().first : _st_l.front().first; } const value_type& operator[](int i) const { const int k = i - _st_l.size(); return k < 0 ? _st_l[~k].first : _st_r[k].first; } int size() const { return _st_l.size() + _st_r.size(); } void clear() { _st_l.clear(), _st_r.clear(); } void shrink_to_fit() { _st_l.shrink_to_fit(), _st_r.shrink_to_fit(); } iterator begin() const { return iterator { _st_l.rbegin(), _st_l.rend(), _st_r.begin(), _st_r.begin() }; } iterator end() const { return iterator { _st_l.rend(), _st_l.rend(), _st_r.begin(), _st_r.end() }; } iterator cbegin() const { return begin(); } iterator cend() const { return end(); } private: std::vector> _st_l, _st_r; value_type prod(const std::vector> &st) const { return st.empty() ? e() : st.back().second; } }; } // namespace suisen mint op(mint x, mint y) { return x * y; } mint e() { return 1; } constexpr uint32_t K_MAX = 5000; int main() { suisen::FPS::set_multiplication([](const auto &a, const auto &b) { return atcoder::convolution(a, b); }); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); uint32_t n; uint64_t m; std::cin >> n >> m; std::vector c(K_MAX + 1); for (uint32_t i = 0; i < n; ++i) { uint32_t k; std::cin >> k; ++c[k]; } suisen::factorial fac(n + K_MAX); mint ans = 0; suisen::DequeAggregation dq; for (uint32_t d = 0; d < n; ++d) dq.push_front(m + d); for (uint32_t k = 1; k <= K_MAX; ++k) { std::vector e = suisen::eulerian_number(k); dq.push_front(m + n + k - 1); mint sum = 0; const uint32_t p = std::min(uint64_t(k), m); for (uint32_t i = 0; i < p; ++i) { sum += e[i] * dq.prod(); dq.pop_front(); dq.push_back(m - i - 1); } ans += c[k] * sum * fac.fac_inv(n + k); for (uint32_t i = p; i --> 0;) { dq.push_front(m - i + n + k - 1); dq.pop_back(); } } std::cout << ans.val() << std::endl; return 0; }