結果
問題 | No.2005 Sum of Power Sums |
ユーザー |
|
提出日時 | 2022-07-10 21:33:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 46,733 bytes |
コンパイル時間 | 1,294 ms |
コンパイル使用メモリ | 112,768 KB |
最終ジャッジ日時 | 2025-01-30 06:17:32 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 13 TLE * 5 |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/no/2005"#include <iostream>#include <atcoder/modint>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 <algorithm>#include <cassert>#include <iostream>#include <cmath>#include <limits>#include <type_traits>#include <vector>namespace suisen {// ! utilitytemplate <typename ...Types>using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;template <bool cond_v, typename Then, typename OrElse>constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {if constexpr (cond_v) {return std::forward<Then>(then);} else {return std::forward<OrElse>(or_else);}}// ! functiontemplate <typename ReturnType, typename Callable, typename ...Args>using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;template <typename F, typename T>using is_uni_op = is_same_as_invoke_result<T, F, T>;template <typename F, typename T>using is_bin_op = is_same_as_invoke_result<T, F, T, T>;template <typename Comparator, typename T>using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;// ! integraltemplate <typename T, typename = constraints_t<std::is_integral<T>>>constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;template <typename T, unsigned int n>struct is_nbit { static constexpr bool value = bit_num<T> == n; };template <typename T, unsigned int n>static constexpr bool is_nbit_v = is_nbit<T, n>::value;// ?template <typename T>struct safely_multipliable {};template <>struct safely_multipliable<int> { using type = long long; };template <>struct safely_multipliable<long long> { using type = __int128_t; };template <>struct safely_multipliable<unsigned int> { using type = unsigned long long; };template <>struct safely_multipliable<unsigned long int> { using type = __uint128_t; };template <>struct safely_multipliable<unsigned long long> { using type = __uint128_t; };template <>struct safely_multipliable<float> { using type = float; };template <>struct safely_multipliable<double> { using type = double; };template <>struct safely_multipliable<long double> { using type = long double; };template <typename T>using safely_multipliable_t = typename safely_multipliable<T>::type;template <typename T, typename = void>struct rec_value_type {using type = T;};template <typename T>struct rec_value_type<T, std::void_t<typename T::value_type>> {using type = typename rec_value_type<typename T::value_type>::type;};template <typename T>using rec_value_type_t = typename rec_value_type<T>::type;} // namespace suisen#include <optional>/*** refernce: https://37zigen.com/tonelli-shanks-algorithm/* calculates x s.t. x^2 = a mod p in O((log p)^2).*/template <typename mint>std::optional<mint> 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 <typename mint>auto sqrt(mint a) -> decltype(mint::mod(), mint()) {return *optional_sqrt(a);}template <typename mint>auto log(mint a) -> decltype(mint::mod(), mint()) {assert(a == 1);return 0;}template <typename mint>auto exp(mint a) -> decltype(mint::mod(), mint()) {assert(a == 0);return 1;}template <typename mint, typename T>auto pow(mint a, T b) -> decltype(mint::mod(), mint()) {return a.pow(b);}template <typename mint>auto inv(mint a) -> decltype(mint::mod(), mint()) {return a.inv();}namespace suisen {template <typename mint>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<mint> invs;static constexpr int mod = mint::mod();};template <typename mint>std::vector<mint> inv_mods<mint>::invs{};}namespace suisen {template <typename T>struct FPSNaive : std::vector<T> {static inline int MAX_DEG = std::numeric_limits<int>::max() / 2;using value_type = T;using element_type = rec_value_type_t<T>;using std::vector<value_type>::vector;FPSNaive(const std::initializer_list<value_type> l) : std::vector<value_type>::vector(l) {}static void set_max_deg(int max_deg) {FPSNaive<T>::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<value_type>::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<FPSNaive, FPSNaive> 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<FPSNaive> 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<element_type> 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<value_type>::operator[](i);}value_type& unsafe_get(int i) {return std::vector<value_type>::operator[](i);}};} // namespace suisentemplate <typename mint>auto sqrt(suisen::FPSNaive<mint> a) -> decltype(mint::mod(), suisen::FPSNaive<mint>{}) {return a.sqrt(suisen::FPSNaive<mint>::MAX_DEG == std::numeric_limits<int>::max() / 2 ? suisen::FPSNaive<mint>::MAX_DEG : a.deg());}template <typename mint>auto log(suisen::FPSNaive<mint> a) -> decltype(mint::mod(), suisen::FPSNaive<mint>{}) {return a.log(suisen::FPSNaive<mint>::MAX_DEG == std::numeric_limits<int>::max() / 2 ? suisen::FPSNaive<mint>::MAX_DEG : a.deg());}template <typename mint>auto exp(suisen::FPSNaive<mint> a) -> decltype(mint::mod(), suisen::FPSNaive<mint>{}) {return a.exp(suisen::FPSNaive<mint>::MAX_DEG == std::numeric_limits<int>::max() / 2 ? suisen::FPSNaive<mint>::MAX_DEG : a.deg());}template <typename mint, typename T>auto pow(suisen::FPSNaive<mint> a, T b) -> decltype(mint::mod(), suisen::FPSNaive<mint>{}) {return a.pow(b, suisen::FPSNaive<mint>::MAX_DEG == std::numeric_limits<int>::max() / 2 ? suisen::FPSNaive<mint>::MAX_DEG : a.deg());}template <typename mint>auto inv(suisen::FPSNaive<mint> a) -> decltype(mint::mod(), suisen::FPSNaive<mint>{}) {return a.inv(suisen::FPSNaive<mint>::MAX_DEG == std::numeric_limits<int>::max() / 2 ? suisen::FPSNaive<mint>::MAX_DEG : a.deg());}namespace suisen {template <typename mint>using convolution_t = std::vector<mint>(*)(const std::vector<mint>&, const std::vector<mint>&);template <typename mint>struct FPS : public std::vector<mint> {using std::vector<mint>::vector;FPS(const std::initializer_list<mint> l) : std::vector<mint>::vector(l) {}FPS(const std::vector<mint>& v) : std::vector<mint>::vector(v) {}FPS(std::vector<mint>&& v) : std::vector<mint>::vector(std::move(v)) {}static void set_multiplication(convolution_t<mint> multiplication) {FPS<mint>::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<mint>::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<mint>::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<mint>(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<mint>(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<mint>(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<mint>(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<FPS> optional_sqrt(const int max_deg) {if (max_deg <= 60) return FPSNaive<mint>(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<mint> invs;static convolution_t<mint> mult;void ensure_deg(int d) { if (deg() < d) this->resize(d + 1, 0); }const mint& unsafe_get(int i) const { return std::vector<mint>::operator[](i); }mint& unsafe_get(int i) { return std::vector<mint>::operator[](i); }std::optional<std::vector<std::pair<int, mint>>> sparse_fps_format(int max_size) const {std::vector<std::pair<int, mint>> 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<FPS, FPS&> 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<std::pair<int, mint>> &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<std::pair<int, mint>> &g, const int max_deg) {return div_fps_sparse(FPS { mint{1} }, g, max_deg);}static FPS exp_sparse(const std::vector<std::pair<int, mint>> &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<std::pair<int, mint>> &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<std::pair<int, mint>> &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<FPS> optional_sqrt_sparse(const std::vector<std::pair<int, mint>> &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<std::pair<int, mint>> &f, const int max_deg) {return *optional_sqrt(f, max_deg);}};template <typename mint>convolution_t<mint> FPS<mint>::mult = [](const auto&, const auto&) {std::cerr << "convolution function is not available." << std::endl;assert(false);return std::vector<mint>{};};} // namespace suisentemplate <typename mint>auto sqrt(suisen::FPS<mint> a) -> decltype(mint::mod(), suisen::FPS<mint>{}) {return a.sqrt(a.deg());}template <typename mint>auto log(suisen::FPS<mint> a) -> decltype(mint::mod(), suisen::FPS<mint>{}) {return a.log(a.deg());}template <typename mint>auto exp(suisen::FPS<mint> a) -> decltype(mint::mod(), suisen::FPS<mint>{}) {return a.exp(a.deg());}template <typename mint, typename T>auto pow(suisen::FPS<mint> a, T b) -> decltype(mint::mod(), suisen::FPS<mint>{}) {return a.pow(b, a.deg());}template <typename mint>auto inv(suisen::FPS<mint> a) -> decltype(mint::mod(), suisen::FPS<mint>{}) {return a.inv(a.deg());}namespace suisen {template <typename T, typename U = T>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<T> _fac;static std::vector<U> _fac_inv;};template <typename T, typename U>std::vector<T> factorial<T, U>::_fac{ 1 };template <typename T, typename U>std::vector<U> factorial<T, U>::_fac_inv{ 1 };} // namespace suisen#include <cstdint>#include <numeric>namespace suisen {// referece: https://37zigen.com/linear-sieve/class LinearSieve {public:LinearSieve(const int n) : _n(n), min_prime_factor(std::vector<int>(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<int>& get_prime_list() const noexcept {return prime_list;}const std::vector<int>& 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<std::pair<int, int>> factorize(int n) const noexcept {assert(0 < n and n <= _n);std::vector<std::pair<int, int>> 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<int> min_prime_factor;std::vector<int> prime_list;};} // namespace suisennamespace suisen {// returns { 0^k, 1^k, ..., n^k }template <typename mint>std::vector<mint> powers(uint32_t n, uint64_t k) {const auto mpf = LinearSieve(n).get_min_prime_factor();std::vector<mint> res(n + 1);res[0] = k == 0;for (int i = 1; i <= n; ++i) res[i] = i == 1 ? 1 : 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_numbernamespace suisen {template <typename mint>std::vector<mint> eulerian_number(uint32_t n) {if (n == 0) return {};factorial<mint> fac(n + 1);const uint32_t h = (n + 1) >> 1;FPS<mint> f = powers<mint>(h, n);f.erase(f.begin());FPS<mint> 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<mint> res = f * g;for (uint32_t i = h; i < n; ++i) res[i] = res[n - 1 - i];res.resize(n);return res;}template <typename mint>std::vector<std::vector<mint>> eulerian_number_table(uint32_t n) {if (n == 0) return {};std::vector dp(n + 1, std::vector<mint>{});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 <typename T, T(*op)(T, T), T(*e)()>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<std::pair<value_type, value_type>>::const_reverse_iterator;using se_iterator_type = typename std::vector<std::pair<value_type, value_type>>::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 <typename InputIterator, std::enable_if_t<std::is_constructible_v<value_type, typename InputIterator::value_type>, std::nullptr_t>= nullptr>DequeAggregation(InputIterator first, InputIterator last) {for (; first != last; ++first) push_back(*first);}template <typename Container, std::enable_if_t<std::is_constructible_v<value_type, typename Container::value_type>, 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 > 0for (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 > 0for (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<std::pair<value_type, value_type>> _st_l, _st_r;value_type prod(const std::vector<std::pair<value_type, value_type>> &st) const {return st.empty() ? e() : st.back().second;}};} // namespace suisenmint op(mint x, mint y) {return x * y;}mint e() {return 1;}constexpr uint32_t K_MAX = 5000;int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);uint32_t n;uint64_t m;std::cin >> n >> m;std::vector<mint> c(K_MAX + 1);for (uint32_t i = 0; i < n; ++i) {uint32_t k;std::cin >> k;++c[k];}suisen::factorial<mint> fac(n + K_MAX);mint ans = 0;auto en = suisen::eulerian_number_table<mint>(K_MAX);for (uint32_t k = 1; k <= K_MAX; ++k) {suisen::DequeAggregation<mint, op, e> dq;for (uint32_t d = n + k; d --> 0;) dq.push_back(m + d);mint sum = 0;for (uint32_t i = 0; i < std::min(uint64_t(k), m); ++i) {sum += en[k][i] * dq.prod();dq.pop_front();dq.push_back(m - i - 1);}ans += c[k] * sum * fac.fac_inv(n + k);}std::cout << ans.val() << std::endl;return 0;}