#include #include #include #include #include #include #include template class modular { public: using value_type = uint32_t; using max_type = uint64_t; static constexpr value_type mod = Modulus; static constexpr value_type get_mod() { return mod; } static_assert(mod >= 2, "invalid mod :: smaller than 2"); static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31"); template static constexpr value_type normalize(T value_) { if (value_ < 0) { value_ = -value_; value_ %= mod; if (value_ == 0) return 0; return mod - value_; } return value_ % mod; } private: value_type value; public: constexpr modular(): value(0) { } template explicit constexpr modular(T value_): value(normalize(value_)) { } template explicit constexpr operator T() { return static_cast(value); } constexpr value_type get() const { return value; } constexpr modular operator - () const { return modular(mod - value); } constexpr modular operator ~ () const { return inverse(); } constexpr value_type &extract() { return value; } constexpr modular inverse() const { return power(mod - 2); } constexpr modular power(max_type exp) const { modular res(1), mult(*this); while (exp > 0) { if (exp & 1) res *= mult; mult *= mult; exp >>= 1; } return res; } constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; } constexpr modular& operator += (const modular &rhs) { if ((value += rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; } constexpr modular& operator -= (const modular &rhs) { if ((value += mod - rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; } constexpr modular& operator *= (const modular &rhs) { value = (max_type) value * rhs.value % mod; return *this; } constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; } constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); } constexpr bool zero() const { return value == 0; } constexpr bool operator == (const modular &rhs) const { return value == rhs.value; } constexpr bool operator != (const modular &rhs) const { return value != rhs.value; } friend std::ostream& operator << (std::ostream &stream, const modular &rhs) { return stream << rhs.value; } }; namespace ntt_detail { constexpr uint32_t calc_primitive_root(uint32_t mod) { uint32_t exp[32] = {}; uint32_t cur = mod - 1; size_t size = 0; for (uint32_t i = 2; i * i <= cur; ++i) { if (cur % i == 0) { exp[size++] = (mod - 1) / i; while (cur % i == 0) cur /= i; } } if (cur != 1) { exp[size++] = (mod - 1) / cur; } uint32_t res = 2; while (true) { bool ok = true; for (size_t i = 0; i < size; ++i) { uint64_t a = res, e = exp[i], x = 1; while (e > 0) { if (e & 1) (x *= a) %= mod; (a *= a) %= mod; e >>= 1; } if (x == 1) { ok = false; break; } } if (ok) break; ++res; } return res; }; template constexpr std::array calculate_roots(T omega) { std::array res; res[N - 1] = omega; for (size_t i = N - 1; i > 0; --i) { res[i - 1] = res[i] * res[i]; } return res; } template constexpr OtherModular convert_mod(Modular x) { return OtherModular(x.get()); } template std::vector convert_mod_vec(const std::vector &vec) { std::vector res(vec.size()); std::transform(vec.cbegin(), vec.cend(), res.begin(), convert_mod); return res; } namespace bit_operation { constexpr uint32_t b16 = 0b00000000000000001111111111111111; constexpr uint32_t b8 = 0b00000000111111110000000011111111; constexpr uint32_t b4 = 0b00001111000011110000111100001111; constexpr uint32_t b2 = 0b00110011001100110011001100110011; constexpr uint32_t b1 = 0b01010101010101010101010101010101; constexpr size_t reverse(size_t x) { x = ((x >> 16) & b16) | ((x & b16) << 16); x = ((x >> 8) & b8) | ((x & b8) << 8); x = ((x >> 4) & b4) | ((x & b4) << 4); x = ((x >> 2) & b2) | ((x & b2) << 2); x = ((x >> 1) & b1) | ((x & b1) << 1); return x; } }; namespace garner_mod { constexpr uint32_t m0 = 754974721; constexpr uint32_t m1 = 167772161; constexpr uint32_t m2 = 469762049; constexpr uint64_t m0m1 = (uint64_t) m0 * m1; constexpr auto im0_m1 = modular(m0).inverse(); constexpr auto im0m1_m2 = modular(m0m1).inverse(); }; /* prime numbers for ntt [ 1051721729 ] [ 2^20 ] [ 1045430273 ] [ 2^20 ] [ 1007681537 ] [ 2^20 ] [ 962592769 ] [ 2^21 ] [ 924844033 ] [ 2^21 ] [ 985661441 ] [ 2^22 ] [ 943718401 ] [ 2^22 ] [ 935329793 ] [ 2^22 ] [ 998244353 ] [ 2^23 ] [ 754974721 ] [ 2^24 ] [ 167772161 ] [ 2^25 ] [ 469762049 ] [ 2^26 ] */ } template > class number_theoretic_transform { public: using value_type = Modular; static constexpr uint32_t mod = Modulus; static constexpr uint32_t prim = ntt_detail::calc_primitive_root(mod); private: static constexpr size_t level = __builtin_ctz(mod - 1); static constexpr value_type unit = value_type(1); static constexpr value_type omega = value_type(prim).power((mod - 1) >> level); static constexpr auto roots = ntt_detail::calculate_roots(omega); static constexpr auto inv_roots = ntt_detail::calculate_roots(omega.inverse()); protected: void M_transform(std::vector &F) const { size_t size = F.size(); size_t logn = __builtin_ctz(size); for (size_t i = 0; i < size; ++i) { size_t j = ntt_detail::bit_operation::reverse(i) >> (32 - logn); if (i < j) { std::swap(F[i], F[j]); } } value_type coeff = unit; for (size_t s = 0; s < logn; ++s) { size_t mh = 1 << s; size_t m = mh << 1; for (size_t i = 0; i < size; i += m) { coeff = unit; for (size_t j = i; j < i + mh; ++j) { auto a = F[j]; auto b = F[j + mh] * coeff; F[j] = a + b; F[j + mh] = a - b; coeff *= roots[s]; } } } } void M_inv_transform(std::vector &F) const { size_t size = F.size(); size_t logn = __builtin_ctz(size); for (size_t i = 0; i < size; ++i) { size_t j = ntt_detail::bit_operation::reverse(i) >> (32 - logn); if (i < j) { std::swap(F[i], F[j]); } } value_type coeff = unit; for (size_t s = 0; s < logn; ++s) { size_t mh = 1 << s; size_t m = mh << 1; for (size_t i = 0; i < size; i += m) { coeff = unit; for (size_t j = i; j < i + mh; ++j) { auto a = F[j]; auto b = F[j + mh] * coeff; F[j] = a + b; F[j + mh] = a - b; coeff *= inv_roots[s]; } } } coeff = value_type(size).inverse(); for (auto &x: F) { x *= coeff; } } public: std::vector convolve( std::vector A, std::vector B, bool same = false ) const { if (A.empty() || B.empty()) return { }; size_t res_size = A.size() + B.size() - 1; size_t fix_size = 1 << (31 - __builtin_clz(2 * res_size - 1)); if (same) { A.resize(fix_size); M_transform(A); for (size_t i = 0; i < fix_size; ++i) { A[i] *= A[i]; } } else { A.resize(fix_size); B.resize(fix_size); M_transform(A); M_transform(B); for (size_t i = 0; i < fix_size; ++i) { A[i] *= B[i]; } } M_inv_transform(A); A.resize(res_size); return A; } template std::vector convolve_convert( const std::vector &A, const std::vector &B, bool same = false ) const { return convolve( ntt_detail::convert_mod_vec(A), ntt_detail::convert_mod_vec(B), same ); } }; template std::vector convolve_arbitrary_mod( const std::vector &A, const std::vector &B, bool same = false ) { using namespace ntt_detail::garner_mod; number_theoretic_transform ntt0; number_theoretic_transform ntt1; number_theoretic_transform ntt2; auto X = ntt0.convolve_convert(A, B, same); auto Y = ntt1.convolve_convert(A, B, same); auto Z = ntt2.convolve_convert(A, B, same); size_t size = X.size(); std::vector res(size); for (size_t i = 0; i < size; ++i) { uint32_t s = (uint32_t) X[i]; uint64_t t = (uint64_t) ((Y[i] - modular(s)) * im0_m1) * m0 + s; res[i] = Modular((__uint128_t) ((Z[i] - modular(t)) * im0m1_m2) * m0m1 + t); } return res; } template > class formal_power_series: public number_theoretic_transform { public: using value_type = Modular; using size_type = size_t; private: std::vector M_data; public: template formal_power_series(Args... args): M_data(args...) { } formal_power_series(std::initializer_list data_): M_data(data_.begin(), data_.end()) { } formal_power_series operator + (const formal_power_series &rhs) const { return formal_power_series(*this) += rhs; } formal_power_series& operator += (const formal_power_series &rhs) { if (M_data.size() < rhs.M_data.size()) M_data.resize(rhs.M_data.size()); for (size_type i = 0; i < rhs.M_data.size(); ++i) M_data[i] += rhs.M_data[i]; return *this; } formal_power_series operator - (const formal_power_series &rhs) const { return formal_power_series(*this) -= rhs; } formal_power_series& operator -= (const formal_power_series &rhs) { if (M_data.size() < rhs.M_data.size()) M_data.resize(rhs.M_data.size()); for (size_type i = 0; i < rhs.M_data.size(); ++i) M_data[i] -= rhs.M_data[i]; return *this; } formal_power_series operator * (const formal_power_series &rhs) const { return formal_power_series(*this) *= rhs; } formal_power_series& operator *= (const formal_power_series &rhs) { M_data = this -> convolve(M_data, rhs.M_data); return *this; } formal_power_series operator + (const value_type &rhs) const { return formal_power_series(*this) += rhs; } formal_power_series& operator += (const value_type &rhs) { M_data[0] += rhs; return *this; } formal_power_series operator - (const value_type &rhs) const { return formal_power_series(*this) -= rhs; } formal_power_series& operator -= (const value_type &rhs) { M_data[0] -= rhs; return *this; } formal_power_series operator * (const value_type &rhs) const { return formal_power_series(*this) *= rhs; } formal_power_series& operator *= (const value_type &rhs) { for (auto &x: M_data) x *= rhs; return *this; } formal_power_series operator / (const value_type &rhs) const { return formal_power_series(*this) /= rhs; } formal_power_series& operator /= (const value_type &rhs) { return (*this) *= rhs.inverse(); } formal_power_series lower(size_type size) const { return formal_power_series(M_data.begin(), M_data.begin() + std::min(M_data.size(), size)); } formal_power_series square() const { return formal_power_series(this -> convolve(M_data, M_data, true)); } formal_power_series diff() const { if (M_data.size() < 1) return formal_power_series(); formal_power_series res(M_data.size() - 1); for (size_type i = 0; i + 1 < M_data.size(); ++i) { res.M_data[i] = M_data[i + 1] * value_type(i + 1); } return res; } formal_power_series inte() const { formal_power_series res(M_data.size() + 1); value_type cur(1); for (size_type i = 0; i < M_data.size(); ++i) { res.M_data[i + 1] = M_data[i] * cur; cur *= value_type(i + 1); } cur = cur.inverse(); for (size_type i = M_data.size(); i > 0; --i) { res.M_data[i] *= cur; cur *= value_type(i); } return res; } formal_power_series inverse(size_type m) const { formal_power_series res(m); res.M_data[0] = M_data[0].inverse(); for (size_type d = 1; d < m; d <<= 1) { formal_power_series f = lower(d + d); if (f.M_data.size() < d + d) f.M_data.resize(d + d); this -> M_transform(f.M_data); formal_power_series g = res.lower(d + d); if (g.M_data.size() < d + d) g.M_data.resize(d + d); this -> M_transform(g.M_data); for (size_type i = 0; i < d + d; ++i) f.M_data[i] *= g.M_data[i]; this -> M_inv_transform(f.M_data); for (size_type i = 0; i < d; ++i) f.M_data[i] = value_type(); this -> M_transform(f.M_data); for (size_type i = 0; i < d + d; ++i) f.M_data[i] *= g.M_data[i]; this -> M_inv_transform(f.M_data); size_type right = std::min(d + d, m); for (size_type i = d; i < right; ++i) res.M_data[i] = -f.M_data[i]; } return res; } formal_power_series log(size_type m) const { return (lower(m).diff() * inverse(m - 1)).low(m - 1).inte(); } value_type get(size_type i) const { return i >= M_data.size() ? value_type() : M_data[i]; } value_type& extract(size_type i) { return M_data[i]; } size_type size() const { return M_data.size(); } bool empty() const { return M_data.empty(); } }; int main() { using m32 = modular<998244353>; using fps = formal_power_series; std::ios::sync_with_stdio(false); std::cin.tie(nullptr); size_t N; std::cin >> N; assert(2 <= N && N <= 100000); std::vector vec(N + 1); { m32 cur(1); for (size_t i = 0; i < N; ++i) { cur *= m32(i + 1); vec[i] = cur; } } auto ans = fps(vec).inverse(N + 1); m32 sum; for (size_t i = 1; i < N; ++i) { sum += ans.get(i); } std::cout << -sum << '\n'; return 0; }