This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: STANDALONE
#include <iostream>
#include "atcoder/modint.hpp"
#include "tools/assert_that.hpp"
#include "tools/stirling_2nd.hpp"
using mint = atcoder::modint998244353;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
for (int N = 0; N < 50; ++N) {
for (int K = 0; K < 50; ++K) {
const auto all = tools::stirling_2nd::all<mint>(N, K);
for (int n = 0; n <= N; ++n) {
const auto fixed_n = tools::stirling_2nd::fixed_n<mint>(n, K);
for (int k = 0; k <= K; ++k) {
assert_that(all[n][k] == fixed_n[k]);
}
}
for (int k = 0; k <= K; ++k) {
const auto fixed_k = tools::stirling_2nd::fixed_k<mint>(N, k);
for (int n = 0; n <= N; ++n) {
assert_that(all[n][k] == fixed_k[n]);
}
}
if (N == K) {
const auto diagonal = tools::stirling_2nd::diagonal<mint>(N);
for (int n = 0; n <= N; ++n) {
assert_that(all[n][n] == diagonal[n]);
}
}
}
}
return 0;
}
#line 1 "tests/stirling_2nd/consistent.test.cpp"
// competitive-verifier: STANDALONE
#include <iostream>
#line 1 "lib/ac-library/atcoder/modint.hpp"
#include <cassert>
#include <numeric>
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#line 1 "lib/ac-library/atcoder/internal_math.hpp"
#include <utility>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
unsigned int _m;
unsigned long long im;
// @param m `1 <= m`
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
// @return m
unsigned int umod() const { return _m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay
// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}
// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (long long)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}
} // namespace internal
} // namespace atcoder
#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"
#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#line 14 "lib/ac-library/atcoder/modint.hpp"
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_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 >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
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 {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
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;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_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 >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
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 {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
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;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#line 1 "tools/assert_that.hpp"
#line 5 "tools/assert_that.hpp"
#include <cstdlib>
#define assert_that_impl(cond, file, line, func) do {\
if (!cond) {\
::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
::std::exit(EXIT_FAILURE);\
}\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)
#line 1 "tools/stirling_2nd.hpp"
#line 5 "tools/stirling_2nd.hpp"
#include <algorithm>
#include <vector>
#line 1 "tools/is_prime.hpp"
#include <array>
#line 1 "tools/prod_mod.hpp"
#line 1 "tools/uint128_t.hpp"
#line 1 "tools/detail/int128_t.hpp"
#line 6 "tools/detail/int128_t.hpp"
#include <cstddef>
#include <cstdint>
#include <functional>
#line 10 "tools/detail/int128_t.hpp"
#include <limits>
#include <string>
#include <string_view>
#line 1 "tools/abs.hpp"
namespace tools {
constexpr float abs(const float x) {
return x < 0 ? -x : x;
}
constexpr double abs(const double x) {
return x < 0 ? -x : x;
}
constexpr long double abs(const long double x) {
return x < 0 ? -x : x;
}
constexpr int abs(const int x) {
return x < 0 ? -x : x;
}
constexpr long abs(const long x) {
return x < 0 ? -x : x;
}
constexpr long long abs(const long long x) {
return x < 0 ? -x : x;
}
constexpr unsigned int abs(const unsigned int x) {
return x;
}
constexpr unsigned long abs(const unsigned long x) {
return x;
}
constexpr unsigned long long abs(const unsigned long long x) {
return x;
}
}
#line 1 "tools/bit_ceil.hpp"
#include <bit>
#line 1 "tools/is_integral.hpp"
#line 5 "tools/is_integral.hpp"
namespace tools {
template <typename T>
struct is_integral : ::std::is_integral<T> {};
template <typename T>
inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}
#line 1 "tools/is_signed.hpp"
#line 5 "tools/is_signed.hpp"
namespace tools {
template <typename T>
struct is_signed : ::std::is_signed<T> {};
template <typename T>
inline constexpr bool is_signed_v = ::tools::is_signed<T>::value;
}
#line 1 "tools/make_unsigned.hpp"
#line 5 "tools/make_unsigned.hpp"
namespace tools {
template <typename T>
struct make_unsigned : ::std::make_unsigned<T> {};
template <typename T>
using make_unsigned_t = typename ::tools::make_unsigned<T>::type;
}
#line 10 "tools/bit_ceil.hpp"
namespace tools {
template <typename T>
constexpr T bit_ceil(T) noexcept;
template <typename T>
constexpr T bit_ceil(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_ceil<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_ceil(x);
}
}
}
#line 1 "tools/bit_floor.hpp"
#line 10 "tools/bit_floor.hpp"
namespace tools {
template <typename T>
constexpr T bit_floor(T) noexcept;
template <typename T>
constexpr T bit_floor(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_floor<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_floor(x);
}
}
}
#line 1 "tools/bit_width.hpp"
#line 10 "tools/bit_width.hpp"
namespace tools {
template <typename T>
constexpr int bit_width(T) noexcept;
template <typename T>
constexpr int bit_width(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_width<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_width(x);
}
}
}
#line 1 "tools/countr_zero.hpp"
#line 12 "tools/countr_zero.hpp"
namespace tools {
template <typename T>
constexpr int countr_zero(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::std::min(::tools::countr_zero<::tools::make_unsigned_t<T>>(x), ::std::numeric_limits<T>::digits);
} else {
return ::std::countr_zero(x);
}
}
}
#line 1 "tools/is_unsigned.hpp"
#line 5 "tools/is_unsigned.hpp"
namespace tools {
template <typename T>
struct is_unsigned : ::std::is_unsigned<T> {};
template <typename T>
inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}
#line 1 "tools/hash_combine.hpp"
#line 6 "tools/hash_combine.hpp"
// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.
// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace tools {
template <typename T>
void hash_combine(::std::size_t& seed, const T& v) {
static const ::std::hash<T> hasher;
static constexpr ::std::size_t k_mul = 0x9ddfea08eb382d69ULL;
::std::size_t a = (hasher(v) ^ seed) * k_mul;
a ^= (a >> 47);
::std::size_t b = (seed ^ a) * k_mul;
b ^= (b >> 47);
seed = b * k_mul;
}
}
#line 1 "tools/make_signed.hpp"
#line 5 "tools/make_signed.hpp"
namespace tools {
template <typename T>
struct make_signed : ::std::make_signed<T> {};
template <typename T>
using make_signed_t = typename ::tools::make_signed<T>::type;
}
#line 1 "tools/now.hpp"
#include <chrono>
namespace tools {
inline long long now() {
return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(::std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
}
#line 25 "tools/detail/int128_t.hpp"
namespace tools {
using uint128_t = unsigned __int128;
using int128_t = __int128;
namespace detail {
namespace int128_t {
constexpr ::tools::uint128_t parse_unsigned(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::uint128_t x = 0;
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
return x;
}
constexpr ::tools::int128_t parse_signed(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::int128_t x = 0;
if (s[0] == '-') {
::std::size_t i = 1;
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x *= 16;
if ('0' <= s[i] && s[i] <= '9') {
x -= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x -= s[i] - 'a' + 10;
} else {
x -= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x -= s[i] - '0';
}
}
} else {
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
}
return x;
}
}
}
constexpr ::tools::uint128_t abs(const ::tools::uint128_t& x) noexcept {
return x;
}
constexpr ::tools::int128_t abs(const ::tools::int128_t& x) {
return x >= 0 ? x : -x;
}
}
#define UINT128_C(c) ::tools::detail::int128_t::parse_unsigned(#c)
#define INT128_C(c) ::tools::detail::int128_t::parse_signed(#c)
inline ::std::istream& operator>>(::std::istream& is, ::tools::uint128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_unsigned(s);
return is;
}
inline ::std::istream& operator>>(::std::istream& is, ::tools::int128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_signed(s);
return is;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::uint128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::int128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else if (x < 0) {
while (x < 0) {
s.push_back('0' + (-(x % 10)));
x /= 10;
}
s.push_back('-');
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
namespace std {
template <>
struct hash<::tools::uint128_t> {
::std::size_t operator()(const ::tools::uint128_t& x) const {
static const ::std::size_t seed = ::tools::now();
::std::size_t hash = seed;
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x >> 64));
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x & ((UINT128_C(1) << 64) - 1)));
return hash;
}
};
template <>
struct hash<::tools::int128_t> {
::std::size_t operator()(const ::tools::int128_t& x) const {
static ::std::hash<::tools::uint128_t> hasher;
return hasher(static_cast<::tools::uint128_t>(x));
}
};
}
#endif
namespace tools {
template <>
struct is_integral<::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct make_signed<::tools::int128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<::tools::uint128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::int128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::uint128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::int128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::uint128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::int128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_unsigned<::tools::int128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<::tools::uint128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::int128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::uint128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::int128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::uint128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::int128_t> {
using type = const volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::uint128_t;
};
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
template <>
constexpr ::tools::uint128_t bit_ceil<::tools::uint128_t>(::tools::uint128_t x) noexcept {
if (x <= 1) return 1;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return ++x;
}
template <>
constexpr ::tools::uint128_t bit_floor<::tools::uint128_t>(::tools::uint128_t x) noexcept {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return x & ~(x >> 1);
}
template <>
constexpr int bit_width<::tools::uint128_t>(::tools::uint128_t x) noexcept {
int w = 0;
if (x & UINT128_C(0xffffffffffffffff0000000000000000)) {
x >>= 64;
w += 64;
}
if (x & UINT128_C(0xffffffff00000000)) {
x >>= 32;
w += 32;
}
if (x & UINT128_C(0xffff0000)) {
x >>= 16;
w += 16;
}
if (x & UINT128_C(0xff00)) {
x >>= 8;
w += 8;
}
if (x & UINT128_C(0xf0)) {
x >>= 4;
w += 4;
}
if (x & UINT128_C(0xc)) {
x >>= 2;
w += 2;
}
if (x & UINT128_C(0x2)) {
x >>= 1;
w += 1;
}
w += x;
return w;
}
namespace detail {
namespace countr_zero {
template <::std::size_t N>
struct ntz_traits;
template <>
struct ntz_traits<128> {
using type = ::tools::uint128_t;
static constexpr int shift = 120;
static constexpr type magic = UINT128_C(0x01061438916347932a5cd9d3ead7b77f);
static constexpr int ntz_table[255] = {
128, 0, 1, -1, 2, -1, 8, -1, 3, -1, 15, -1, 9, -1, 22, -1,
4, -1, 29, -1, 16, -1, 36, -1, 10, -1, 43, -1, 23, -1, 50, -1,
5, -1, 33, -1, 30, -1, 57, -1, 17, -1, 64, -1, 37, -1, 71, -1,
11, -1, 60, -1, 44, -1, 78, -1, 24, -1, 85, -1, 51, -1, 92, -1,
-1, 6, -1, 20, -1, 34, -1, 48, 31, -1, -1, 69, 58, -1, -1, 90,
18, -1, 67, -1, 65, -1, 99, -1, 38, -1, 101, -1, 72, -1, 106, -1,
-1, 12, -1, 40, -1, 61, -1, 82, 45, -1, -1, 103, 79, -1, 113, -1,
-1, 25, -1, 74, 86, -1, -1, 116, -1, 52, -1, 108, -1, 93, -1, 120,
127, -1, -1, 7, -1, 14, -1, 21, -1, 28, -1, 35, -1, 42, -1, 49,
-1, 32, -1, 56, -1, 63, -1, 70, -1, 59, -1, 77, -1, 84, -1, 91,
-1, 19, -1, 47, -1, 68, -1, 89, -1, 66, -1, 98, -1, 100, -1, 105,
-1, 39, -1, 81, -1, 102, -1, 112, -1, 73, -1, 115, -1, 107, -1, 119,
126, -1, 13, -1, 27, -1, 41, -1, -1, 55, 62, -1, -1, 76, 83, -1,
-1, 46, -1, 88, -1, 97, -1, 104, -1, 80, -1, 111, -1, 114, -1, 118,
125, -1, 26, -1, 54, -1, 75, -1, -1, 87, 96, -1, -1, 110, -1, 117,
124, -1, 53, -1, -1, 95, 109, -1, 123, -1, 94, -1, 122, -1, 121
};
};
template <typename T>
constexpr int impl(const T x) noexcept {
using tr = ::tools::detail::countr_zero::ntz_traits<::std::numeric_limits<T>::digits>;
using type = typename tr::type;
return tr::ntz_table[static_cast<type>(tr::magic * static_cast<type>(x & -x)) >> tr::shift];
}
}
}
template <>
constexpr int countr_zero<::tools::uint128_t>(const ::tools::uint128_t x) noexcept {
return ::tools::detail::countr_zero::impl(x);
}
#endif
}
#line 5 "tools/uint128_t.hpp"
#line 5 "tools/prod_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 prod_mod(const T1 x, const T2 y, const T3 m) {
using u128 = ::tools::uint128_t;
u128 prod_mod = u128(x >= 0 ? x : -x) * u128(y >= 0 ? y : -y) % u128(m);
if ((x >= 0) ^ (y >= 0)) prod_mod = u128(m) - prod_mod;
return prod_mod;
}
}
#line 1 "tools/pow_mod.hpp"
#line 1 "tools/mod.hpp"
#line 7 "tools/mod.hpp"
namespace tools {
template <typename M, typename N> requires (
::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
constexpr ::std::common_type_t<M, N> mod(const M a, const N b) noexcept {
assert(b != 0);
using UM = ::std::make_unsigned_t<M>;
using UN = ::std::make_unsigned_t<N>;
const UM ua = a >= 0 ? a : static_cast<UM>(-(a + 1)) + 1;
const UN ub = b >= 0 ? b : static_cast<UN>(-(b + 1)) + 1;
auto r = ua % ub;
if (a < 0 && r > 0) {
r = ub - r;
}
return r;
}
}
#line 6 "tools/pow_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 pow_mod(const T1 x, T2 n, const T3 m) {
if (m == 1) return 0;
T3 r = 1;
T3 y = ::tools::mod(x, m);
while (n > 0) {
if ((n & 1) > 0) {
r = ::tools::prod_mod(r, y, m);
}
y = ::tools::prod_mod(y, y, m);
n /= 2;
}
return r;
}
}
#line 7 "tools/is_prime.hpp"
namespace tools {
constexpr bool is_prime(const unsigned long long n) {
constexpr ::std::array<unsigned long long, 7> bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
auto d = n - 1;
for (; d % 2 == 0; d /= 2);
for (const auto a : bases) {
if (a % n == 0) return true;
auto power = d;
auto target = ::tools::pow_mod(a, power, n);
bool is_composite = true;
if (target == 1) is_composite = false;
for (; is_composite && power != n - 1; power *= 2, target = ::tools::prod_mod(target, target, n)) {
if (target == n - 1) is_composite = false;
}
if (is_composite) {
return false;
}
}
return true;
}
}
#line 1 "tools/fact_mod_cache.hpp"
#line 6 "tools/fact_mod_cache.hpp"
#include <iterator>
#line 8 "tools/fact_mod_cache.hpp"
#include <cmath>
#line 10 "tools/fact_mod_cache.hpp"
namespace tools {
template <class M>
class fact_mod_cache {
::std::vector<M> m_inv;
::std::vector<M> m_fact;
::std::vector<M> m_fact_inv;
public:
fact_mod_cache() : m_inv({M::raw(0), M::raw(1)}), m_fact({M::raw(1), M::raw(1)}), m_fact_inv({M::raw(1), M::raw(1)}) {
assert(::tools::is_prime(M::mod()));
}
explicit fact_mod_cache(const long long max) : fact_mod_cache() {
this->fact(::std::min<long long>(max, M::mod() - 1));
this->fact_inv(::std::min<long long>(max, M::mod() - 1));
}
M inv(const long long n) {
assert(n % M::mod() != 0);
const long long size = ::std::ssize(this->m_inv);
this->m_inv.resize(::std::clamp<long long>(::std::abs(n) + 1, size, M::mod()));
for (long long i = size; i < ::std::ssize(this->m_inv); ++i) {
this->m_inv[i] = -this->m_inv[M::mod() % i] * M::raw(M::mod() / i);
}
M result = this->m_inv[::std::abs(n) % M::mod()];
if (n < 0) result = -result;
return result;
}
M fact(const long long n) {
assert(n >= 0);
const long long size = ::std::ssize(this->m_fact);
this->m_fact.resize(::std::clamp<long long>(n + 1, size, M::mod()));
for (long long i = size; i < ::std::ssize(this->m_fact); ++i) {
this->m_fact[i] = this->m_fact[i - 1] * M::raw(i);
}
return n < M::mod() ? this->m_fact[n] : M::raw(0);
}
M fact_inv(const long long n) {
assert(0 <= n && n < M::mod());
const long long size = ::std::ssize(this->m_fact_inv);
this->m_fact_inv.resize(::std::max<long long>(size, n + 1));
this->inv(this->m_fact_inv.size() - 1);
for (long long i = size; i < ::std::ssize(this->m_fact_inv); ++i) {
this->m_fact_inv[i] = this->m_fact_inv[i - 1] * this->m_inv[i];
}
return this->m_fact_inv[n];
}
M binomial(long long n, long long r) {
if (r < 0) return M::raw(0);
if (0 <= n && n < r) return M::raw(0);
if (n < 0) return M(1 - ((r & 1) << 1)) * this->binomial(-n + r - 1, r);
this->fact(::std::min<long long>(n, M::mod() - 1));
this->fact_inv(::std::min<long long>(n, M::mod() - 1));
const auto c = [&](const long long nn, const long long rr) {
return 0 <= rr && rr <= nn ? this->m_fact[nn] * this->m_fact_inv[nn - rr] * this->m_fact_inv[rr] : M::raw(0);
};
M result(1);
while (n > 0 || r > 0) {
result *= c(n % M::mod(), r % M::mod());
n /= M::mod();
r /= M::mod();
}
return result;
}
M combination(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r);
}
M permutation(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r) * this->fact(r);
}
M combination_with_repetition(const long long n, const long long r) {
if (n < 0 || r < 0) return M::raw(0);
return this->binomial(n + r - 1, r);
}
};
}
#line 1 "tools/pow_mod_cache.hpp"
#line 5 "tools/pow_mod_cache.hpp"
#include <optional>
#line 1 "tools/find_cycle.hpp"
#line 5 "tools/find_cycle.hpp"
namespace tools {
template <typename T, typename F>
::std::pair<long long, long long> find_cycle(const T& seed, const F& f) {
auto i = 1LL;
auto j = 2LL;
T x = f(seed);
T y = f(f(seed));
for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));
i = 0;
x = seed;
for (; x != y; ++i, ++j, x = f(x), y = f(y));
const auto head = i;
++i;
j = i + 1;
x = f(x);
y = f(f(y));
for (; x != y; ++i, j += 2, x = f(x), y = f(f(y)));
const auto cycle = j - i;
return ::std::make_pair(head, cycle);
}
}
#line 1 "tools/floor.hpp"
#line 7 "tools/floor.hpp"
namespace tools {
template <typename M, typename N> requires (
::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
constexpr ::std::common_type_t<M, N> floor(const M x, const N y) noexcept {
assert(y != 0);
if (y >= 0) {
if (x >= 0) {
return x / y;
} else {
return (x + 1) / y - 1;
}
} else {
if (x > 0) {
return (x - 1) / y - 1;
} else {
return x / y;
}
}
}
}
#line 1 "tools/ceil.hpp"
#line 8 "tools/ceil.hpp"
namespace tools {
template <typename M, typename N> requires (
::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
constexpr ::std::common_type_t<M, N> ceil(const M x, const N y) noexcept {
assert(y != 0);
if (y >= 0) {
if (x > 0) {
return (x - 1) / y + 1;
} else {
if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
}
} else {
if (x >= 0) {
if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
} else {
return (x + 1) / y + 1;
}
}
}
}
#line 16 "tools/pow_mod_cache.hpp"
namespace tools {
template <class M>
class pow_mod_cache {
::std::vector<M> m_pow;
::std::vector<M> m_cumsum;
::std::vector<M> m_inv_pow;
::std::vector<M> m_inv_cumsum;
::std::optional<::std::pair<long long, long long>> m_period;
public:
pow_mod_cache() = default;
explicit pow_mod_cache(const M base) : m_pow({M(1), base}), m_cumsum({M::raw(0)}), m_inv_pow({M(1)}), m_inv_cumsum({M::raw(0)}) {
if (base == M(-1)) {
if (M::mod() > 2) {
this->m_period = ::std::make_pair(0LL, 2LL);
} else {
this->m_period = ::std::make_pair(0LL, 1LL);
this->m_pow.resize(1);
}
this->m_inv_pow.clear();
this->m_inv_cumsum.clear();
}
}
template <typename Z, ::std::enable_if_t<::std::is_integral_v<Z>, ::std::nullptr_t> = nullptr>
explicit pow_mod_cache(const Z base) : pow_mod_cache(M(base)) {
}
M operator[](const long long n) {
if (!this->m_period) {
if (::std::max<long long>(::std::ssize(this->m_pow) - 1, n) - ::std::min<long long>(n, -(::std::ssize(this->m_inv_pow) - 1)) + 1 < M::mod() - 1) {
if (n >= 0) {
const long long size = ::std::ssize(this->m_pow);
this->m_pow.resize(::std::max(size, n + 1));
for (long long i = size; i < ::std::ssize(this->m_pow); ++i) {
this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
}
return this->m_pow[n];
} else {
if (this->m_inv_pow.size() == 1) {
this->m_inv_pow.push_back(this->m_pow[1].inv());
}
const long long size = ::std::ssize(this->m_inv_pow);
this->m_inv_pow.resize(::std::max(size, -n + 1));
for (long long i = size; i < ::std::ssize(this->m_inv_pow); ++i) {
this->m_inv_pow[i] = this->m_inv_pow[i - 1] * this->m_inv_pow[1];
}
return this->m_inv_pow[-n];
}
}
this->m_period = ::tools::find_cycle(this->m_pow[0], [&](const M& prev) { return prev * this->m_pow[1]; });
const long long size = ::std::ssize(this->m_pow);
this->m_pow.resize(this->m_period->first + this->m_period->second);
for (long long i = size; i < ::std::ssize(this->m_pow); ++i) {
this->m_pow[i] = this->m_pow[i - 1] * this->m_pow[1];
}
this->m_inv_pow.clear();
this->m_inv_cumsum.clear();
}
if (this->m_period->first == 0) {
return this->m_pow[::tools::mod(n, this->m_period->second)];
} else {
assert(n >= 0);
if (n < this->m_period->first + this->m_period->second) {
return this->m_pow[n];
} else {
return this->m_pow[(n - this->m_period->first) % this->m_period->second + this->m_period->first];
}
}
}
M sum(const long long l, const long long r) {
if (l >= r) return M::raw(0);
(*this)[r - 1];
(*this)[l];
{
const long long size = ::std::ssize(this->m_cumsum);
this->m_cumsum.resize(this->m_pow.size() + 1);
for (long long i = size; i < ::std::ssize(this->m_cumsum); ++i) {
this->m_cumsum[i] = this->m_cumsum[i - 1] + this->m_pow[i - 1];
}
}
if (!this->m_period) {
const long long size = ::std::ssize(this->m_inv_cumsum);
this->m_inv_cumsum.resize(this->m_inv_pow.size() + 1);
for (long long i = size; i < ::std::ssize(this->m_inv_cumsum); ++i) {
this->m_inv_cumsum[i] = this->m_inv_cumsum[i - 1] + this->m_pow[i - 1];
}
if (l >= 0) {
return this->m_cumsum[r] - this->m_cumsum[l];
} else if (r <= 0) {
return this->m_inv_cumsum[-l] - this->m_inv_cumsum[-r];
} else {
return (this->m_inv_cumsum[-l] - this->m_inv_cumsum[1]) + (this->m_cumsum[r] - this->m_cumsum[0]);
}
}
static const auto cumsum = [&](const long long ll, const long long rr) {
return this->m_cumsum[rr] - this->m_cumsum[ll];
};
if (l >= 0) {
static const auto f = [&](const long long x) {
if (x <= this->m_period->first + this->m_period->second) {
return cumsum(0, x);
} else {
return cumsum(0, this->m_period->first) +
cumsum(this->m_period->first, this->m_period->first + this->m_period->second) * ((x - this->m_period->first) / this->m_period->second) +
cumsum(this->m_period->first, (x - this->m_period->first) % this->m_period->second + this->m_period->first);
}
};
return f(r) - f(l);
} else {
const auto& n = this->m_period->second;
return cumsum(::tools::mod(l, n), n) + cumsum(0, ::tools::mod(r, n)) + cumsum(0, n) * M(::tools::floor(r, n) - ::tools::ceil(l, n));
}
}
};
}
#line 1 "tools/fps.hpp"
#line 6 "tools/fps.hpp"
#include <initializer_list>
#line 1 "lib/ac-library/atcoder/convolution.hpp"
#line 9 "lib/ac-library/atcoder/convolution.hpp"
#line 1 "lib/ac-library/atcoder/internal_bit.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
#if __cplusplus >= 202002L
#line 10 "lib/ac-library/atcoder/internal_bit.hpp"
#endif
namespace atcoder {
namespace internal {
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
#endif
// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
} // namespace internal
} // namespace atcoder
#line 12 "lib/ac-library/atcoder/convolution.hpp"
namespace atcoder {
namespace internal {
template <class mint,
int g = internal::primitive_root<mint::mod()>,
internal::is_static_modint_t<mint>* = nullptr>
struct fft_info {
static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1);
std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1
std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1
std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;
std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;
fft_info() {
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for (int i = rank2 - 1; i >= 0; i--) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 2; i++) {
rate2[i] = root[i + 2] * prod;
irate2[i] = iroot[i + 2] * iprod;
prod *= iroot[i + 2];
iprod *= root[i + 2];
}
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 3; i++) {
rate3[i] = root[i + 3] * prod;
irate3[i] = iroot[i + 3] * iprod;
prod *= iroot[i + 3];
iprod *= root[i + 3];
}
}
}
};
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len < h) {
if (h - len == 1) {
int p = 1 << (h - len - 1);
mint rot = 1;
for (int s = 0; s < (1 << len); s++) {
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
if (s + 1 != (1 << len))
rot *= info.rate2[countr_zero(~(unsigned int)(s))];
}
len++;
} else {
// 4-base
int p = 1 << (h - len - 2);
mint rot = 1, imag = info.root[2];
for (int s = 0; s < (1 << len); s++) {
mint rot2 = rot * rot;
mint rot3 = rot2 * rot;
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto mod2 = 1ULL * mint::mod() * mint::mod();
auto a0 = 1ULL * a[i + offset].val();
auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
auto a1na3imag =
1ULL * mint(a1 + mod2 - a3).val() * imag.val();
auto na2 = mod2 - a2;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
}
if (s + 1 != (1 << len))
rot *= info.rate3[countr_zero(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len) {
if (len == 1) {
int p = 1 << (h - len);
mint irot = 1;
for (int s = 0; s < (1 << (len - 1)); s++) {
int offset = s << (h - len + 1);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] =
(unsigned long long)(mint::mod() + l.val() - r.val()) *
irot.val();
;
}
if (s + 1 != (1 << (len - 1)))
irot *= info.irate2[countr_zero(~(unsigned int)(s))];
}
len--;
} else {
// 4-base
int p = 1 << (h - len);
mint irot = 1, iimag = info.iroot[2];
for (int s = 0; s < (1 << (len - 2)); s++) {
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
int offset = s << (h - len + 2);
for (int i = 0; i < p; i++) {
auto a0 = 1ULL * a[i + offset + 0 * p].val();
auto a1 = 1ULL * a[i + offset + 1 * p].val();
auto a2 = 1ULL * a[i + offset + 2 * p].val();
auto a3 = 1ULL * a[i + offset + 3 * p].val();
auto a2na3iimag =
1ULL *
mint((mint::mod() + a2 - a3) * iimag.val()).val();
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] =
(a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
a[i + offset + 2 * p] =
(a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) *
irot2.val();
a[i + offset + 3 * p] =
(a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) *
irot3.val();
}
if (s + 1 != (1 << (len - 2)))
irot *= info.irate3[countr_zero(~(unsigned int)(s))];
}
len -= 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
std::vector<mint> ans(n + m - 1);
if (n < m) {
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
ans[i + j] += a[i] * b[j];
}
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i + j] += a[i] * b[j];
}
}
}
return ans;
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
int n = int(a.size()), m = int(b.size());
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
a.resize(z);
internal::butterfly(a);
b.resize(z);
internal::butterfly(b);
for (int i = 0; i < z; i++) {
a[i] *= b[i];
}
internal::butterfly_inv(a);
a.resize(n + m - 1);
mint iz = mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
} // namespace internal
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <unsigned int mod = 998244353,
class T,
std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
using mint = static_modint<mod>;
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
std::vector<mint> a2(n), b2(m);
for (int i = 0; i < n; i++) {
a2[i] = mint(a[i]);
}
for (int i = 0; i < m; i++) {
b2[i] = mint(b[i]);
}
auto c2 = convolution(std::move(a2), std::move(b2));
std::vector<T> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
c[i] = c2[i].val();
}
return c;
}
std::vector<long long> convolution_ll(const std::vector<long long>& a,
const std::vector<long long>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
static constexpr unsigned long long MOD1 = 754974721; // 2^24
static constexpr unsigned long long MOD2 = 167772161; // 2^25
static constexpr unsigned long long MOD3 = 469762049; // 2^26
static constexpr unsigned long long M2M3 = MOD2 * MOD3;
static constexpr unsigned long long M1M3 = MOD1 * MOD3;
static constexpr unsigned long long M1M2 = MOD1 * MOD2;
static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
static constexpr unsigned long long i1 =
internal::inv_gcd(MOD2 * MOD3, MOD1).second;
static constexpr unsigned long long i2 =
internal::inv_gcd(MOD1 * MOD3, MOD2).second;
static constexpr unsigned long long i3 =
internal::inv_gcd(MOD1 * MOD2, MOD3).second;
static constexpr int MAX_AB_BIT = 24;
static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
assert(n + m - 1 <= (1 << MAX_AB_BIT));
auto c1 = convolution<MOD1>(a, b);
auto c2 = convolution<MOD2>(a, b);
auto c3 = convolution<MOD3>(a, b);
std::vector<long long> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
unsigned long long x = 0;
x += (c1[i] * i1) % MOD1 * M2M3;
x += (c2[i] * i2) % MOD2 * M1M3;
x += (c3[i] * i3) % MOD3 * M1M2;
// B = 2^63, -B <= x, r(real value) < B
// (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
// r = c1[i] (mod MOD1)
// focus on MOD1
// r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
// r = x,
// x - M' + (0 or 2B),
// x - 2M' + (0, 2B or 4B),
// x - 3M' + (0, 2B, 4B or 6B) (without mod!)
// (r - x) = 0, (0)
// - M' + (0 or 2B), (1)
// -2M' + (0 or 2B or 4B), (2)
// -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
// we checked that
// ((1) mod MOD1) mod 5 = 2
// ((2) mod MOD1) mod 5 = 3
// ((3) mod MOD1) mod 5 = 4
long long diff =
c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
if (diff < 0) diff += MOD1;
static constexpr unsigned long long offset[5] = {
0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
x -= offset[diff % 5];
c[i] = x;
}
return c;
}
} // namespace atcoder
#line 1 "tools/convolution.hpp"
#line 5 "tools/convolution.hpp"
#include <complex>
#line 1 "tools/pow2.hpp"
#line 6 "tools/pow2.hpp"
namespace tools {
template <typename T, typename ::std::enable_if<::std::is_unsigned<T>::value, ::std::nullptr_t>::type = nullptr>
constexpr T pow2(const T x) {
return static_cast<T>(1) << x;
}
template <typename T, typename ::std::enable_if<::std::is_signed<T>::value, ::std::nullptr_t>::type = nullptr>
constexpr T pow2(const T x) {
return static_cast<T>(static_cast<typename ::std::make_unsigned<T>::type>(1) << static_cast<typename ::std::make_unsigned<T>::type>(x));
}
}
#line 1 "tools/ceil_log2.hpp"
#line 6 "tools/ceil_log2.hpp"
namespace tools {
template <typename T>
constexpr T ceil_log2(T x) noexcept {
assert(x > 0);
return ::tools::bit_width(x - 1);
}
}
#line 1 "tools/garner3.hpp"
#line 7 "tools/garner3.hpp"
namespace tools {
template <typename M, typename M1, typename M2, typename M3>
M garner3(const M1& a, const M2& b, const M3& c, const M m) {
using ull = unsigned long long;
static const M2 m1_inv_mod_m2 = M2::raw(M1::mod()).inv();
static const M3 m1_m2_inv_mod_m3 = (M3::raw(M1::mod()) * M3::raw(M2::mod())).inv();
static const auto plus_mod = [](ull x, const ull y, const ull mod) {
assert(x < mod);
assert(y < mod);
x += y;
if (x >= mod) x -= mod;
return x;
};
assert(m >= 1);
assert(M1::mod() < M2::mod());
assert(M2::mod() < M3::mod());
assert(::tools::is_prime(M1::mod()));
assert(::tools::is_prime(M2::mod()));
assert(::tools::is_prime(M3::mod()));
// t1 = (b - a) / M1; (mod M2)
// t2 = (c - a - t1 * M1) / M1 / M2; (mod M3)
// return a + t1 * M1 + t2 * M1 * M2; (mod m)
const M2 t1 = (b - M2::raw(a.val())) * m1_inv_mod_m2;
const M3 t2 = (c - M3::raw(a.val()) - M3::raw(t1.val()) * M3::raw(M1::mod())) * m1_m2_inv_mod_m3;
ull r = ::tools::prod_mod(t2.val(), ull(M1::mod()) * ull(M2::mod()), m);
assert(r < ull(m));
r = plus_mod(r, ull(t1.val()) * ull(M1::mod()) % m, m);
assert(r < ull(m));
r = plus_mod(r, a.val() % m, m);
assert(r < ull(m));
return r;
}
}
#line 1 "tools/group.hpp"
namespace tools {
namespace group {
template <typename G>
struct plus {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs + rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return -v;
}
};
template <typename G>
struct multiplies {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
static T inv(const T& v) {
return e() / v;
}
};
template <typename G>
struct bit_xor {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs ^ rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return v;
}
};
}
}
#line 1 "tools/monoid.hpp"
#line 1 "tools/gcd.hpp"
#line 6 "tools/gcd.hpp"
namespace tools {
template <typename M, typename N>
constexpr ::std::common_type_t<M, N> gcd(const M m, const N n) {
return ::std::gcd(m, n);
}
}
#line 9 "tools/monoid.hpp"
namespace tools {
namespace monoid {
template <typename M, M ...dummy>
struct max;
template <typename M>
struct max<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::max(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::min();
} else {
return -::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct max<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(E <= lhs);
assert(E <= rhs);
return ::std::max(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M, M ...dummy>
struct min;
template <typename M>
struct min<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::min(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::max();
} else {
return ::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct min<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(lhs <= E);
assert(rhs <= E);
return ::std::min(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M>
struct multiplies {
private:
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
};
template <>
struct multiplies<bool> {
using T = bool;
static T op(const bool lhs, const bool rhs) {
return lhs && rhs;
}
static T e() {
return true;
}
};
template <typename M>
struct gcd {
private:
static_assert(!::std::is_arithmetic_v<M> || (::std::is_integral_v<M> && !::std::is_same_v<M, bool>), "If M is a built-in arithmetic type, it must be integral except for bool.");
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return ::tools::gcd(lhs, rhs);
}
static T e() {
return T(0);
}
};
template <typename M, M E>
struct update {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
return lhs == E ? rhs : lhs;
}
static T e() {
return E;
}
};
}
}
#line 22 "tools/convolution.hpp"
namespace tools {
namespace detail {
namespace convolution {
template <typename T, typename = void>
struct make_complex {
using type = T;
};
template <typename T>
struct make_complex<T, ::std::enable_if_t<::std::is_floating_point_v<T>, void>> {
using type = ::std::complex<T>;
};
template <typename T>
using make_complex_t = typename ::tools::detail::convolution::make_complex<T>::type;
template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
void naive(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
static_assert(::std::is_same_v<typename AG::T, typename MM::T>);
assert(a_begin != a_end);
assert(b_begin != b_end);
using T = typename AG::T;
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
::std::vector<T> c(n + m - 1, AG::e());
if (n < m) {
auto c_begin = c.begin();
for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_begin) {
auto c_it = c_begin;
for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_it) {
*c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
}
}
} else {
auto c_begin = c.begin();
for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_begin) {
auto c_it = c_begin;
for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_it) {
*c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
}
}
}
::std::move(c.begin(), c.end(), result);
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void fft(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using C = ::tools::detail::convolution::make_complex_t<T>;
static_assert(::std::is_same_v<C, ::std::complex<float>> || ::std::is_same_v<C, ::std::complex<double>> || ::std::is_same_v<C, ::std::complex<long double>>);
using R = typename C::value_type;
assert(a_begin != a_end);
assert(b_begin != b_end);
::std::vector<C> a, b;
if constexpr (::std::is_same_v<T, R>) {
for (auto it = a_begin; it != a_end; ++it) {
a.emplace_back(*it, 0);
}
for (auto it = b_begin; it != b_end; ++it) {
b.emplace_back(*it, 0);
}
} else if constexpr (::std::is_same_v<T, C>) {
a.assign(a_begin, a_end);
b.assign(b_begin, b_end);
}
const auto n = a.size() + b.size() - 1;
const auto z = ::tools::pow2(::tools::ceil_log2(n));
a.resize(z);
b.resize(z);
::std::vector<C> pow_root;
pow_root.reserve(z);
pow_root.emplace_back(1, 0);
if (z > 1) pow_root.push_back(::std::polar<R>(1, R(2) * ::std::acos(R(-1)) / z));
for (::std::size_t p = 2; p < z; p *= 2) {
pow_root.push_back(pow_root[p / 2] * pow_root[p / 2]);
for (::std::size_t i = p + 1; i < p * 2; ++i) {
pow_root.push_back(pow_root[p] * pow_root[i - p]);
}
}
const auto butterfly = [&](::std::vector<C>& f) {
::std::vector<C> prev(z);
for (::std::size_t p = z / 2; p >= 1; p /= 2) {
prev.swap(f);
for (::std::size_t qp = 0; qp < z; qp += p) {
for (::std::size_t r = 0; r < p; ++r) {
f[qp + r] = prev[qp * 2 % z + r] + pow_root[qp] * prev[qp * 2 % z + p + r];
}
}
}
};
butterfly(a);
butterfly(b);
for (::std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
::std::reverse(::std::next(pow_root.begin()), pow_root.end());
butterfly(a);
for (::std::size_t i = 0; i < n; ++i) {
if constexpr (::std::is_same_v<T, R>) {
*result = a[i].real() / z;
} else {
*result = a[i] / z;
}
++result;
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
static_assert(::atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(::tools::is_prime(M::mod()));
assert(a_begin != a_end);
assert(b_begin != b_end);
::std::vector<M> a(a_begin, a_end);
::std::vector<M> b(b_begin, b_end);
const auto n = a.size();
const auto m = b.size();
const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));
assert((M::mod() - 1) % z == 0);
if (n == m && 4 * n == z + 4) {
const auto afbf = a.front() * b.front();
const auto abbb = a.back() * b.back();
a.resize(z / 2);
::atcoder::internal::butterfly(a);
b.resize(z / 2);
::atcoder::internal::butterfly(b);
for (::std::size_t i = 0; i < z / 2; ++i) {
a[i] *= b[i];
}
::atcoder::internal::butterfly_inv(a);
const auto iz = M(z / 2).inv();
*result = afbf;
++result;
for (::std::size_t i = 1; i < n + m - 2; ++i) {
*result = a[i] * iz;
++result;
}
*result = abbb;
++result;
} else {
a.resize(z);
::atcoder::internal::butterfly(a);
b.resize(z);
::atcoder::internal::butterfly(b);
for (::std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
::atcoder::internal::butterfly_inv(a);
const auto iz = M(z).inv();
for (::std::size_t i = 0; i < n + m - 1; ++i) {
*result = a[i] * iz;
++result;
}
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt_and_garner(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using M1 = ::atcoder::static_modint<1107296257>; // 33 * 2^25 + 1
using M2 = ::atcoder::static_modint<1711276033>; // 51 * 2^25 + 1
using M3 = ::atcoder::static_modint<1811939329>; // 27 * 2^26 + 1
static_assert(::atcoder::internal::is_static_modint<M>::value || ::atcoder::internal::is_dynamic_modint<M>::value);
assert(a_begin != a_end);
assert(b_begin != b_end);
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));
assert((M1::mod() - 1) % z == 0);
assert((M2::mod() - 1) % z == 0);
assert((M3::mod() - 1) % z == 0);
// No need for the following assertion because the condition always holds.
// assert(std::min(a.size(), b.size()) * tools::square(M::mod() - 1) < M1::mod() * M2::mod() * M3::mod());
::std::vector<M1> c1;
c1.reserve(n + m - 1);
{
::std::vector<M1> a1;
a1.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a1.emplace_back(it->val());
}
::std::vector<M1> b1;
b1.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b1.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a1.begin(), a1.end(), b1.begin(), b1.end(), ::std::back_inserter(c1));
}
::std::vector<M2> c2;
c2.reserve(n + m - 1);
{
::std::vector<M2> a2;
a2.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a2.emplace_back(it->val());
}
::std::vector<M2> b2;
b2.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b2.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a2.begin(), a2.end(), b2.begin(), b2.end(), ::std::back_inserter(c2));
}
::std::vector<M3> c3;
c3.reserve(n + m - 1);
{
::std::vector<M3> a3;
a3.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a3.emplace_back(it->val());
}
::std::vector<M3> b3;
b3.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b3.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a3.begin(), a3.end(), b3.begin(), b3.end(), ::std::back_inserter(c3));
}
for (::std::size_t i = 0; i < c1.size(); ++i) {
*result = M::raw(::tools::garner3(c1[i], c2[i], c3[i], M::mod()));
++result;
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt_and_garner_for_ll(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using Z = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<Z, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using ll = long long;
static_assert(::std::is_integral_v<Z>);
assert(a_begin != a_end);
assert(b_begin != b_end);
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
assert(n + m - 1 <= ::tools::pow2(24));
::std::vector<ll> a, b;
a.reserve(n);
b.reserve(m);
::std::copy(a_begin, a_end, ::std::back_inserter(a));
::std::copy(b_begin, b_end, ::std::back_inserter(b));
for (const auto c_i : ::atcoder::convolution_ll(a, b)) {
*result = c_i;
++result;
}
}
}
}
template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
if (a_begin == a_end || b_begin == b_end) return;
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
if (::std::min(n, m) <= 60) {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
return;
}
if constexpr (::std::is_same_v<AG, ::tools::group::plus<T>> && (::std::is_same_v<MM, ::tools::monoid::multiplies<T>> || ::std::is_same_v<MM, ::tools::group::multiplies<T>>)) {
if constexpr (::std::is_floating_point_v<T> || ::std::is_same_v<T, ::std::complex<float>> || ::std::is_same_v<T, ::std::complex<double>> || ::std::is_same_v<T, ::std::complex<long double>>) {
::tools::detail::convolution::fft(a_begin, a_end, b_begin, b_end, result);
} else if constexpr (::std::is_integral_v<T>) {
::tools::detail::convolution::ntt_and_garner_for_ll(a_begin, a_end, b_begin, b_end, result);
} else if constexpr (::atcoder::internal::is_static_modint<T>::value || ::atcoder::internal::is_dynamic_modint<T>::value) {
if constexpr (::atcoder::internal::is_static_modint<T>::value && T::mod() <= 2000000000 && ::tools::is_prime(T::mod())) {
if ((T::mod() - 1) % ::tools::pow2(::tools::ceil_log2(n + m - 1)) == 0) {
::tools::detail::convolution::ntt(a_begin, a_end, b_begin, b_end, result);
} else {
::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, const OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
::tools::convolution<::tools::group::plus<T>, ::tools::monoid::multiplies<T>>(a_begin, a_end, b_begin, b_end, result);
}
}
#line 1 "tools/less_by_first.hpp"
#line 5 "tools/less_by_first.hpp"
namespace tools {
class less_by_first {
public:
template <class T1, class T2>
bool operator()(const ::std::pair<T1, T2>& x, const ::std::pair<T1, T2>& y) const {
return x.first < y.first;
}
};
}
#line 19 "tools/fps.hpp"
// Source: https://opt-cp.com/fps-implementation/
// License: CC0
// Author: opt
namespace tools {
template <typename M>
class fps {
private:
using F = ::tools::fps<M>;
::std::vector<M> m_vector;
// maximum 2^k s.t. x = 1 (mod 2^k)
static constexpr int pow2_k(const unsigned int x) {
return (x - 1) & -(x - 1);
}
// d <= lpf(M)
static bool is_leq_lpf_of_M(const int d) {
if (M::mod() == 1) return true;
for (int i = 2; i < d; ++i) {
if (M::mod() % i == 0) return false;
}
return true;
}
public:
using reference = M&;
using const_reference = const M&;
using iterator = typename ::std::vector<M>::iterator;
using const_iterator = typename ::std::vector<M>::const_iterator;
using size_type = ::std::size_t;
using difference_type = ::std::ptrdiff_t;
using value_type = M;
using allocator_type = typename ::std::vector<M>::allocator_type;
using pointer = M*;
using const_pointer = const M*;
using reverse_iterator = typename ::std::vector<M>::reverse_iterator;
using const_reverse_iterator = typename ::std::vector<M>::const_reverse_iterator;
fps() = default;
fps(const F&) = default;
fps(F&&) = default;
~fps() = default;
F& operator=(const F&) = default;
F& operator=(F&&) = default;
explicit fps(const size_type n) : m_vector(n) {}
fps(const size_type n, const_reference value) : m_vector(n, value) {}
template <class InputIter> fps(const InputIter first, const InputIter last) : m_vector(first, last) {}
fps(const ::std::initializer_list<M> il) : m_vector(il) {}
iterator begin() noexcept { return this->m_vector.begin(); }
const_iterator begin() const noexcept { return this->m_vector.begin(); }
iterator end() noexcept { return this->m_vector.end(); }
const_iterator end() const noexcept { return this->m_vector.end(); }
const_iterator cbegin() const noexcept { return this->m_vector.cbegin(); }
const_iterator cend() const noexcept { return this->m_vector.cend(); }
reverse_iterator rbegin() noexcept { return this->m_vector.rbegin(); }
const_reverse_iterator rbegin() const noexcept { return this->m_vector.rbegin(); }
const_reverse_iterator crbegin() const noexcept { return this->m_vector.crbegin(); }
reverse_iterator rend() noexcept { return this->m_vector.rend(); }
const_reverse_iterator rend() const noexcept { return this->m_vector.rend(); }
const_reverse_iterator crend() const noexcept { return this->m_vector.crend(); }
size_type size() const noexcept { return this->m_vector.size(); }
size_type max_size() const noexcept { return this->m_vector.max_size(); }
void resize(const size_type sz) { this->m_vector.resize(sz); }
void resize(const size_type sz, const M& c) { this->m_vector.resize(sz, c); }
size_type capacity() const noexcept { return this->m_vector.capacity(); }
bool empty() const noexcept { return this->m_vector.empty(); }
void reserve(const size_type n) { this->m_vector.reserve(n); }
void shrink_to_fit() { this->m_vector.shrink_to_fit(); }
reference operator[](const size_type n) { return this->m_vector[n]; }
const_reference operator[](const size_type n) const { return this->m_vector[n]; }
reference at(const size_type n) { return this->m_vector.at(n); }
const_reference at(const size_type n) const { return this->m_vector.at(n); }
pointer data() noexcept { return this->m_vector.data(); }
const_pointer data() const noexcept { return this->m_vector.data(); }
reference front() { return this->m_vector.front(); }
const_reference front() const { return this->m_vector.front(); }
reference back() { return this->m_vector.back(); }
const_reference back() const { return this->m_vector.back(); }
template <class InputIterator> void assign(const InputIterator first, const InputIterator last) { this->m_vector.assign(first, last); }
void assign(const size_type n, const M& u) { this->m_vector.assign(n, u); }
void assign(const ::std::initializer_list<M> il) { this->m_vector.assign(il); }
void push_back(const M& x) { this->m_vector.push_back(x); }
void push_back(M&& x) { this->m_vector.push_back(::std::forward<M>(x)); }
template <class... Args> reference emplace_back(Args&&... args) { return this->m_vector.emplace_back(::std::forward<Args>(args)...); }
void pop_back() { this->m_vector.pop_back(); }
iterator insert(const const_iterator position, const M& x) { return this->m_vector.insert(position, x); }
iterator insert(const const_iterator position, M&& x) { return this->m_vector.insert(position, ::std::forward<M>(x)); }
iterator insert(const const_iterator position, const size_type n, const M& x) { return this->m_vector.insert(position, n, x); }
template <class InputIterator> iterator insert(const const_iterator position, const InputIterator first, const InputIterator last) { return this->m_vector.insert(position, first, last); }
iterator insert(const const_iterator position, const ::std::initializer_list<M> il) { return this->m_vector.insert(position, il); }
template <class... Args> iterator emplace(const const_iterator position, Args&&... args) { return this->m_vector.emplace(position, ::std::forward<Args>(args)...); }
iterator erase(const const_iterator position) { return this->m_vector.erase(position); }
iterator erase(const const_iterator first, const const_iterator last) { return this->m_vector.erase(first, last); }
void swap(F& x) noexcept { this->m_vector.swap(x.m_vector); }
void clear() { this->m_vector.clear(); }
allocator_type get_allocator() const noexcept { return this->m_vector.get_allocator(); }
friend bool operator==(const F& x, const F& y) { return x.m_vector == y.m_vector; }
friend bool operator!=(const F& x, const F& y) { return x.m_vector != y.m_vector; }
friend void swap(F& x, F& y) noexcept { x.m_vector.swap(y.m_vector); }
F operator+() const {
return *this;
}
F operator-() const {
F res(*this);
for (auto& e : res) {
e = -e;
}
return res;
}
F& operator++() {
if (!this->empty()) ++(*this)[0];
return *this;
}
F operator++(int) {
const auto self = *this;
++*this;
return self;
}
F& operator--() {
if (!this->empty()) --(*this)[0];
return *this;
}
F operator--(int) {
const auto self = *this;
--*this;
return self;
}
F& operator*=(const M& g) {
for (auto& e : *this) {
e *= g;
}
return *this;
}
F& operator/=(const M& g) {
assert(::std::gcd(g.val(), M::mod()) == 1);
*this *= g.inv();
return *this;
}
F& operator+=(const F& g) {
const int n = this->size();
const int m = g.size();
for (int i = 0; i < ::std::min(n, m); ++i) {
(*this)[i] += g[i];
}
return *this;
}
F& operator-=(const F& g) {
const int n = this->size();
const int m = g.size();
for (int i = 0; i < ::std::min(n, m); ++i) {
(*this)[i] -= g[i];
}
return *this;
}
F& operator<<=(const int d) {
if (d < 0) *this >>= -d;
const int n = this->size();
this->resize(::std::max(0, n - d));
this->insert(this->begin(), ::std::min(n, d), M::raw(0));
return *this;
}
F& operator>>=(const int d) {
if (d < 0) *this <<= -d;
const int n = this->size();
this->erase(this->begin(), this->begin() + ::std::min(n, d));
this->resize(n);
return *this;
}
F& multiply_inplace(const F& g, const int d) {
assert(d >= 0);
const int n = this->size();
F res;
::tools::convolution(this->cbegin(), this->cbegin() + ::std::min(d, n), g.cbegin(), g.cbegin() + ::std::min<int>(d, g.size()), ::std::back_inserter(res));
res.resize(d);
*this = ::std::move(res);
return *this;
}
F& multiply_inplace(const F& g) { return this->multiply_inplace(g, this->size()); }
F& operator*=(const F& g) { return this->multiply_inplace(g); }
F multiply(const F& g, const int d) const { return F(*this).multiply_inplace(g, d); }
F multiply(const F& g) const { return this->multiply(g, this->size()); }
private:
F inv_regular(const int d) const {
assert(d > 0);
assert(M::mod() > 1);
assert(!this->empty());
assert(::std::gcd((*this)[0].val(), M::mod()) == 1);
const int n = this->size();
F res{(*this)[0].inv()};
for (int m = 1; m < d; m *= 2) {
F f(this->begin(), this->begin() + ::std::min(n, 2 * m));
f *= -1;
F r(res);
r.multiply_inplace(r, 2 * m);
r.multiply_inplace(f);
r += res;
r += res;
res = ::std::move(r);
}
res.resize(d);
return res;
}
template <typename M_ = M>
F inv_faster(const int d) const {
static_assert(::atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(::tools::is_prime(M::mod()));
assert(d > 0);
assert(!this->empty());
assert(::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod()));
assert(::std::gcd((*this)[0].val(), M::mod()) == 1);
const int n = this->size();
F res{(*this)[0].inv()};
for (int m = 1; m < d; m *= 2) {
F f(this->begin(), this->begin() + ::std::min(n, 2 * m));
F r(res);
f.resize(2 * m);
::atcoder::internal::butterfly(f.m_vector);
r.resize(2 * m);
::atcoder::internal::butterfly(r.m_vector);
for (int i = 0; i < 2 * m; ++i) {
f[i] *= r[i];
}
::atcoder::internal::butterfly_inv(f.m_vector);
f.erase(f.begin(), f.begin() + m);
f.resize(2 * m);
::atcoder::internal::butterfly(f.m_vector);
for (int i = 0; i < 2 * m; ++i) {
f[i] *= r[i];
}
::atcoder::internal::butterfly_inv(f.m_vector);
M iz = M(2 * m).inv();
iz *= -iz;
for (int i = 0; i < m; ++i) {
f[i] *= iz;
}
res.insert(res.end(), f.begin(), f.begin() + m);
}
res.resize(d);
return res;
}
public:
F inv(const int d) const {
assert(d >= 0);
if (d == 0) return F();
if (M::mod() == 1) return F(d);
assert(!this->empty());
assert(::std::gcd((*this)[0].val(), M::mod()) == 1);
if constexpr (::atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && ::tools::is_prime(M::mod())) {
if (::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod())) {
return this->inv_faster(d);
} else {
return this->inv_regular(d);
}
} else {
return this->inv_regular(d);
}
}
F inv() const { return this->inv(this->size()); }
F& divide_inplace(const F& g, const int d) {
assert(d >= 0);
const int n = this->size();
const auto g_inv = g.inv(d);
F res;
::tools::convolution(this->cbegin(), this->cbegin() + ::std::min(d, n), g_inv.cbegin(), g_inv.cend(), ::std::back_inserter(res));
res.resize(d);
*this = ::std::move(res);
return *this;
}
F& divide_inplace(const F& g) { return this->divide_inplace(g, this->size()); }
F& operator/=(const F& g) { return this->divide_inplace(g); }
F divide(const F& g, const int d) const { return F(*this).divide_inplace(g, d); }
F divide(const F& g) const { return this->divide(g, this->size()); }
// sparse
template <class InputIterator>
F& multiply_inplace(InputIterator g_begin, const InputIterator g_end) {
assert(::std::is_sorted(g_begin, g_end, ::tools::less_by_first()));
const int n = this->size();
if (g_begin == g_end) {
::std::fill(this->begin(), this->end(), M::raw(0));
return *this;
}
auto [d, c] = *g_begin;
if (d == 0) {
++g_begin;
} else {
c = M::raw(0);
}
for (int i = n - 1; i >= 0; --i) {
(*this)[i] *= c;
for (auto it = g_begin; it != g_end; ++it) {
const auto& [j, b] = *it;
if (j > i) break;
(*this)[i] += (*this)[i - j] * b;
}
}
return *this;
}
F& multiply_inplace(const ::std::initializer_list<::std::pair<int, M>> il) { return this->multiply_inplace(il.begin(), il.end()); }
template <class InputIterator>
F multiply(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).multiply_inplace(g_begin, g_end); }
F multiply(const ::std::initializer_list<::std::pair<int, M>> il) const { return this->multiply(il.begin(), il.end()); }
template <class InputIterator>
F& divide_inplace(InputIterator g_begin, const InputIterator g_end) {
assert(g_begin != g_end);
assert(::std::is_sorted(g_begin, g_end, ::tools::less_by_first()));
const int n = this->size();
if (n == 0) return *this;
if (M::mod() == 1) return *this;
const auto [d, c] = *g_begin;
assert(d == 0 && ::std::gcd(c.val(), M::mod()) == 1);
const M ic = c.inv();
++g_begin;
for (int i = 0; i < n; ++i) {
for (auto it = g_begin; it != g_end; ++it) {
const auto& [j, b] = *it;
if (j > i) break;
(*this)[i] -= (*this)[i - j] * b;
}
(*this)[i] *= ic;
}
return *this;
}
F& divide_inplace(const ::std::initializer_list<::std::pair<int, M>> il) { return this->divide_inplace(il.begin(), il.end()); }
template <class InputIterator>
F divide(const InputIterator g_begin, const InputIterator g_end) const { return F(*this).divide_inplace(g_begin, g_end); }
F divide(const ::std::initializer_list<::std::pair<int, M>> il) const { return this->divide(il.begin(), il.end()); }
// multiply and divide (1 + cz^d)
F& multiply_inplace(const int d, const M c) {
assert(d > 0);
const int n = this->size();
if (c == M(1)) {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i];
}
} else if (c == M(-1)) {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] -= (*this)[i];
}
} else {
for (int i = n - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i] * c;
}
}
return *this;
}
F multiply(const int d, const M c) const { return F(*this).multiply_inplace(d, c); }
F& divide_inplace(const int d, const M c) {
assert(d > 0);
const int n = this->size();
if (c == M(1)) {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] -= (*this)[i];
}
} else if (c == M(-1)) {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] += (*this)[i];
}
} else {
for (int i = 0; i < n - d; ++i) {
(*this)[i + d] -= (*this)[i] * c;
}
}
return *this;
}
F divide(const int d, const M c) const { return F(*this).divide_inplace(d, c); }
F& integral_inplace() {
const int n = this->size();
assert(is_leq_lpf_of_M(n));
if (n == 0) return *this;
if (n == 1) return *this = F{0};
this->insert(this->begin(), 0);
this->pop_back();
::std::vector<M> inv(n);
inv[1] = M(1);
int p = M::mod();
for (int i = 2; i < n; ++i) {
inv[i] = -inv[p % i] * (p / i);
}
for (int i = 2; i < n; ++i) {
(*this)[i] *= inv[i];
}
return *this;
}
F integral() const { return F(*this).integral_inplace(); }
F& derivative_inplace() {
const int n = this->size();
if (n == 0) return *this;
for (int i = 2; i < n; ++i) {
(*this)[i] *= i;
}
this->erase(this->begin());
this->push_back(0);
return *this;
}
F derivative() const { return F(*this).derivative_inplace(); }
F& log_inplace(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
this->resize(d);
if (d == 0) return *this;
assert((*this)[0] == M(1));
const F f_inv = this->inv();
this->derivative_inplace();
this->multiply_inplace(f_inv);
this->integral_inplace();
return *this;
}
F& log_inplace() { return this->log_inplace(this->size()); }
F log(const int d) const { return F(*this).log_inplace(d); }
F log() const { return this->log(this->size()); }
private:
F& exp_inplace_regular(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
assert(this->empty() || (*this)[0] == M::raw(0));
const int n = this->size();
F g{1};
for (int m = 1; m < d; m *= 2) {
F r(g);
r.resize(2 * m);
r.log_inplace();
r *= -1;
r += F(this->begin(), this->begin() + ::std::min(n, 2 * m));
++r[0];
r.multiply_inplace(g);
g = ::std::move(r);
}
g.resize(d);
*this = ::std::move(g);
return *this;
}
template <typename M_ = M>
F& exp_inplace_faster(const int d) {
static_assert(::atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(::tools::is_prime(M::mod()));
assert(d > 0);
assert(is_leq_lpf_of_M(d));
assert(::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod()));
assert(this->empty() || (*this)[0] == M::raw(0));
F g{1}, g_fft{1, 1};
this->resize(d);
(*this)[0] = 1;
F h_drv(this->derivative());
for (int m = 2; m < d; m *= 2) {
// prepare
F f_fft(this->begin(), this->begin() + m);
f_fft.resize(2 * m);
::atcoder::internal::butterfly(f_fft.m_vector);
// Step 2.a'
{
F g_(m);
for (int i = 0; i < m; ++i) {
g_[i] = f_fft[i] * g_fft[i];
}
::atcoder::internal::butterfly_inv(g_.m_vector);
g_.erase(g_.begin(), g_.begin() + m / 2);
g_.resize(m);
::atcoder::internal::butterfly(g_.m_vector);
for (int i = 0; i < m; ++i) {
g_[i] *= g_fft[i];
}
::atcoder::internal::butterfly_inv(g_.m_vector);
g_.resize(m / 2);
g_ /= M(-m) * m;
g.insert(g.end(), g_.begin(), g_.begin() + m / 2);
}
// Step 2.b'--d'
F t(this->begin(), this->begin() + m);
t.derivative_inplace();
{
// Step 2.b'
F r{h_drv.begin(), h_drv.begin() + m - 1};
// Step 2.c'
r.resize(m);
::atcoder::internal::butterfly(r.m_vector);
for (int i = 0; i < m; ++i) {
r[i] *= f_fft[i];
}
::atcoder::internal::butterfly_inv(r.m_vector);
r /= -m;
// Step 2.d'
t += r;
t.insert(t.begin(), t.back());
t.pop_back();
}
// Step 2.e'
if (2 * m < d) {
t.resize(2 * m);
::atcoder::internal::butterfly(t.m_vector);
g_fft = g;
g_fft.resize(2*m);
::atcoder::internal::butterfly(g_fft.m_vector);
for (int i = 0; i < 2 * m; ++i) {
t[i] *= g_fft[i];
}
::atcoder::internal::butterfly_inv(t.m_vector);
t.resize(m);
t /= 2 * m;
} else { // この場合分けをしても数パーセントしか速くならない
F g1(g.begin() + m / 2, g.end());
F s1(t.begin() + m / 2, t.end());
t.resize(m/2);
g1.resize(m);
::atcoder::internal::butterfly(g1.m_vector);
t.resize(m);
::atcoder::internal::butterfly(t.m_vector);
s1.resize(m);
::atcoder::internal::butterfly(s1.m_vector);
for (int i = 0; i < m; ++i) {
s1[i] = g_fft[i] * s1[i] + g1[i] * t[i];
}
for (int i = 0; i < m; ++i) {
t[i] *= g_fft[i];
}
::atcoder::internal::butterfly_inv(t.m_vector);
::atcoder::internal::butterfly_inv(s1.m_vector);
for (int i = 0; i < m / 2; ++i) {
t[i + m / 2] += s1[i];
}
t /= m;
}
// Step 2.f'
F v(this->begin() + m, this->begin() + ::std::min<int>(d, 2 * m));
v.resize(m);
t.insert(t.begin(), m - 1, 0);
t.push_back(0);
t.integral_inplace();
for (int i = 0; i < m; ++i) {
v[i] -= t[m + i];
}
// Step 2.g'
v.resize(2 * m);
::atcoder::internal::butterfly(v.m_vector);
for (int i = 0; i < 2 * m; ++i) {
v[i] *= f_fft[i];
}
::atcoder::internal::butterfly_inv(v.m_vector);
v.resize(m);
v /= 2 * m;
// Step 2.h'
for (int i = 0; i < ::std::min(d - m, m); ++i) {
(*this)[m + i] = v[i];
}
}
return *this;
}
public:
F& exp_inplace(const int d) {
assert(d >= 0);
assert(is_leq_lpf_of_M(d));
assert(this->empty() || (*this)[0] == M::raw(0));
if (d == 0) {
this->clear();
return *this;
}
if constexpr (::atcoder::internal::is_static_modint<M>::value && M::mod() <= 2000000000 && ::tools::is_prime(M::mod())) {
if (::tools::pow2(::tools::ceil_log2(d)) <= pow2_k(M::mod())) {
return this->exp_inplace_faster(d);
} else {
return this->exp_inplace_regular(d);
}
} else {
return this->exp_inplace_regular(d);
}
}
F& exp_inplace() { return this->exp_inplace(this->size()); }
F exp(const int d) const { return F(*this).exp_inplace(d); }
F exp() const { return this->exp(this->size()); }
private:
F& pow_inplace_regular(long long k, const int d, const int l) {
assert(k > 0);
assert(d > 0);
assert(l >= 0);
assert(d - l * k > 0);
this->erase(this->begin(), this->begin() + l);
this->resize(d - l * k);
F sum(d - l * k);
for (F p = *this; k > 0; k /= 2, p *= p) {
if (k & 1) sum += p;
}
*this = ::std::move(sum);
this->insert(this->begin(), l * k, 0);
return *this;
}
F& pow_inplace_faster(const long long k, const int d, const int l) {
assert(k > 0);
assert(d > 0);
assert(l >= 0);
assert(d - l * k > 0);
assert(is_leq_lpf_of_M(d - l * k));
assert(::std::gcd((*this)[l].val(), M::mod()) == 1);
M c{(*this)[l]};
this->erase(this->begin(), this->begin() + l);
*this /= c;
this->log_inplace(d - l * k);
*this *= k;
this->exp_inplace();
*this *= c.pow(k);
this->insert(this->begin(), l * k, 0);
return *this;
}
public:
F& pow_inplace(const long long k, const int d) {
assert(k >= 0);
assert(d >= 0);
const int n = this->size();
if (d == 0) {
this->clear();
return *this;
}
if (k == 0) {
*this = F(d);
(*this)[0] = M(1);
return *this;
}
int l = 0;
while (l < n && (*this)[l] == M::raw(0)) ++l;
if (l == n || l > (d - 1) / k) {
return *this = F(d);
}
if (::std::gcd((*this)[l].val(), M::mod()) == 1 && is_leq_lpf_of_M(d - l * k)) {
return this->pow_inplace_faster(k, d, l);
} else {
return this->pow_inplace_regular(k, d, l);
}
}
F& pow_inplace(const long long k) { return this->pow_inplace(k, this->size()); }
F pow(const long long k, const int d) const { return F(*this).pow_inplace(k, d); }
F pow(const long long k) const { return this->pow(k, this->size()); }
F operator()(const F& g) const {
assert(g.empty() || g[0] == M::raw(0));
const int n = this->size();
F h(n);
if (n == 0) return h;
const int m = g.size();
int l;
for (l = 0; l < ::std::min(m, n) && g[l] == M::raw(0); ++l);
h[0] = (*this)[0];
if (l == ::std::min(m, n)) return h;
const F g_1(g.begin() + l, g.begin() + ::std::min(m, n));
for (int i = l; i < ::std::min(m, n); ++i) {
h[i] += (*this)[1] * g[i];
}
auto g_k = g_1;
for (int k = 2, d; (d = ::std::min(k * (m - l - 1) + 1, n - l * k)) > 0; ++k) {
g_k.multiply_inplace(g_1, d);
for (int i = l * k; i < l * k + d; ++i) {
h[i] += (*this)[k] * g_k[i - l * k];
}
}
return h;
}
F compositional_inverse() const {
assert(this->size() >= 2);
assert((*this)[0] == M::raw(0));
assert(::std::gcd((*this)[1].val(), M::mod()) == 1);
const int n = this->size();
::std::vector<F> f;
f.reserve(::std::max(2, n - 1));
f.emplace_back(n);
f[0][0] = M::raw(1);
f.push_back(*this);
for (int i = 2; i < n - 1; ++i) {
f.push_back(f.back() * f[1]);
}
::std::vector<M> invpow_f11;
invpow_f11.reserve(n);
invpow_f11.push_back(M::raw(1));
invpow_f11.push_back(f[1][1].inv());
for (int i = 2; i < n; ++i) {
invpow_f11.push_back(invpow_f11.back() * invpow_f11[1]);
}
F g(n);
g[1] = invpow_f11[1];
for (int i = 2; i < n; ++i) {
for (int j = 1; j < i; ++j) {
g[i] -= f[j][i] * g[j];
}
g[i] *= invpow_f11[i];
}
return g;
}
friend F operator*(const F& f, const M& g) { return F(f) *= g; }
friend F operator*(const M& f, const F& g) { return F(g) *= f; }
friend F operator/(const F& f, const M& g) { return F(f) /= g; }
friend F operator+(const F& f, const F& g) { return F(f) += g; }
friend F operator-(const F& f, const F& g) { return F(f) -= g; }
friend F operator*(const F& f, const F& g) { return F(f) *= g; }
friend F operator/(const F& f, const F& g) { return F(f) /= g; }
friend F operator<<(const F& f, const int d) { return F(f) <<= d; }
friend F operator>>(const F& f, const int d) { return F(f) >>= d; }
};
}
#line 1 "tools/virtual_vector.hpp"
#line 6 "tools/virtual_vector.hpp"
#include <memory>
#line 10 "tools/virtual_vector.hpp"
namespace tools {
template <typename F>
class virtual_vector {
public:
using size_type = ::std::size_t;
class iterator {
const virtual_vector<F> *m_parent;
size_type m_i;
public:
using reference = decltype(::std::declval<F>()(::std::declval<size_type>()));
using value_type = ::std::remove_const_t<::std::remove_reference_t<reference>>;
using difference_type = ::std::ptrdiff_t;
using pointer = const value_type*;
using iterator_category = ::std::random_access_iterator_tag;
iterator() = default;
iterator(const virtual_vector<F> * const parent, const size_type i) : m_parent(parent), m_i(i) {
}
reference operator*() const {
return (*this->m_parent)[this->m_i];
}
pointer operator->() const {
return &(*(*this));
}
iterator& operator++() {
++this->m_i;
return *this;
}
iterator operator++(int) {
const auto self = *this;
++*this;
return self;
}
iterator& operator--() {
--this->m_i;
return *this;
}
iterator operator--(int) {
const auto self = *this;
--*this;
return self;
}
iterator& operator+=(const difference_type n) {
this->m_i += n;
return *this;
}
iterator& operator-=(const difference_type n) {
this->m_i -= n;
return *this;
}
friend iterator operator+(const iterator self, const difference_type n) {
return iterator(self.m_parent, self.m_i + n);
}
friend iterator operator+(const difference_type n, const iterator self) {
return self + n;
}
friend iterator operator-(const iterator self, const difference_type n) {
return iterator(self.m_parent, self.m_i - n);
}
friend difference_type operator-(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return static_cast<difference_type>(lhs.m_i) - static_cast<difference_type>(rhs.m_i);
}
reference operator[](const difference_type n) const {
return *(*this + n);
}
friend bool operator==(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i == rhs.m_i;
}
friend bool operator!=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i != rhs.m_i;
}
friend bool operator<(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i < rhs.m_i;
}
friend bool operator<=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i <= rhs.m_i;
}
friend bool operator>(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i > rhs.m_i;
}
friend bool operator>=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i >= rhs.m_i;
}
};
using const_reference = decltype(::std::declval<F>()(::std::declval<size_type>()));
using value_type = ::std::remove_const_t<::std::remove_reference_t<const_reference>>;
using reference = value_type&;
using const_iterator = iterator;
using difference_type = ::std::ptrdiff_t;
using allocator_type = ::std::allocator<value_type>;
using pointer = value_type*;
using const_pointer = const value_type*;
using reverse_iterator = ::std::reverse_iterator<iterator>;
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
private:
size_type m_size;
F m_selector;
public:
virtual_vector() = default;
virtual_vector(const size_type n, const F& selector) : m_size(n), m_selector(selector) {
}
iterator begin() noexcept { return iterator(this, 0); }
const_iterator begin() const noexcept { return const_iterator(this, 0); }
const_iterator cbegin() const noexcept { return const_iterator(this, 0); }
iterator end() noexcept { return iterator(this, this->size()); }
const_iterator end() const noexcept { return const_iterator(this, this->size()); }
const_iterator cend() const noexcept { return const_iterator(this, this->size()); }
reverse_iterator rbegin() noexcept { return ::std::make_reverse_iterator(this->end()); }
const_reverse_iterator rbegin() const noexcept { return ::std::make_reverse_iterator(this->end()); }
const_reverse_iterator crbegin() const noexcept { return ::std::make_reverse_iterator(this->cend()); }
reverse_iterator rend() noexcept { return ::std::make_reverse_iterator(this->begin()); }
const_reverse_iterator rend() const noexcept { return ::std::make_reverse_iterator(this->begin()); }
const_reverse_iterator crend() const noexcept { return ::std::make_reverse_iterator(this->cbegin()); }
size_type size() const noexcept { return this->m_size; }
bool empty() const noexcept { return this->size() == 0; }
const_reference operator[](const size_type n) const { assert(n < this->size()); return this->m_selector(n); }
const_reference at(const size_type n) const { return (*this)[n]; }
const_reference front() const { return *this->begin(); }
const_reference back() const { return *this->rbegin(); }
template <typename G>
friend bool operator==(const virtual_vector<F>& x, const virtual_vector<G>& y) { return ::std::equal(x.begin(), x.end(), y.begin(), y.end()); }
template <typename G>
friend bool operator!=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x == y); }
template <typename G>
friend bool operator<(const virtual_vector<F>& x, const virtual_vector<G>& y) { return ::std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
template <typename G>
friend bool operator<=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x > y); }
template <typename G>
friend bool operator>(const virtual_vector<F>& x, const virtual_vector<G>& y) { return y < x; }
template <typename G>
friend bool operator>=(const virtual_vector<F>& x, const virtual_vector<G>& y) { return !(x < y); }
};
}
#line 12 "tools/stirling_2nd.hpp"
namespace tools {
namespace stirling_2nd {
template <typename M>
auto fixed_n(const int N, const int K) {
assert(::tools::is_prime(M::mod()));
assert(0 <= ::std::min(N, K) && ::std::min(N, K) < M::mod());
::tools::fact_mod_cache<M> cache;
::tools::pow_mod_cache<M> pow_m1(-1);
::tools::fps<M> a, b;
for (int i = 0; i <= ::std::min(N, K); ++i) {
a.push_back(M(i).pow(N) * cache.fact_inv(i));
b.push_back(pow_m1[i] * cache.fact_inv(i));
}
a.multiply_inplace(b);
return ::tools::virtual_vector(K + 1, [N, a](const int k) -> const M& {
static const auto zero = M::raw(0);
return k <= N ? a[k] : zero;
});
}
template <typename M>
auto fixed_k(const int N, const int K) {
assert(::tools::is_prime(M::mod()));
assert(N >= 0);
assert(0 <= K && K < M::mod());
assert(N - K + 1 < M::mod());
::tools::fps<M> f(::std::max(0, N - K + 1));
if (!f.empty()) {
::tools::fact_mod_cache<M> cache;
for (int i = 0; i <= N - K; ++i) {
f[i] = cache.fact_inv(i + 1);
}
f.pow_inplace(K);
f *= cache.fact_inv(K);
for (int n = K; n <= N; ++n) {
f[n - K] *= cache.fact(n);
}
}
return ::tools::virtual_vector(N + 1, [K, f](const int n) -> const M& {
static const auto zero = M::raw(0);
return n < K ? zero : f[n - K];
});
}
template <typename M>
auto diagonal(const int N) {
assert(N >= 0);
return ::tools::virtual_vector(N + 1, [](const int n) -> const M& {
static const M one(1);
return one;
});
}
template <typename M>
::std::vector<::std::vector<M>> all(const int N, const int K) {
assert(N >= 0);
assert(K >= 0);
::std::vector<::std::vector<M>> S(N + 1);
S[0].emplace_back(1);
S[0].insert(S[0].end(), K, M::raw(0));
for (int n = 1; n <= N; ++n) {
S[n].resize(K + 1, M::raw(0));
for (int k = 0; k <= ::std::min(n, K); ++k) {
if (k > 0) S[n][k] += S[n - 1][k - 1];
S[n][k] += M(k) * S[n - 1][k];
}
}
return S;
}
}
}
#line 7 "tests/stirling_2nd/consistent.test.cpp"
using mint = atcoder::modint998244353;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
for (int N = 0; N < 50; ++N) {
for (int K = 0; K < 50; ++K) {
const auto all = tools::stirling_2nd::all<mint>(N, K);
for (int n = 0; n <= N; ++n) {
const auto fixed_n = tools::stirling_2nd::fixed_n<mint>(n, K);
for (int k = 0; k <= K; ++k) {
assert_that(all[n][k] == fixed_n[k]);
}
}
for (int k = 0; k <= K; ++k) {
const auto fixed_k = tools::stirling_2nd::fixed_k<mint>(N, k);
for (int n = 0; n <= N; ++n) {
assert_that(all[n][k] == fixed_k[n]);
}
}
if (N == K) {
const auto diagonal = tools::stirling_2nd::diagonal<mint>(N);
for (int n = 0; n <= N; ++n) {
assert_that(all[n][n] == diagonal[n]);
}
}
}
}
return 0;
}