結果
| 問題 | No.93 ペガサス |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-01-12 18:19:56 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 44,750 bytes |
| 記録 | |
| コンパイル時間 | 14,083 ms |
| コンパイル使用メモリ | 473,192 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-01-12 18:20:12 |
| 合計ジャッジ時間 | 12,708 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
// QCFium 法
//#pragma GCC target("avx2") // yukicoder と codechef では消す
#pragma GCC optimize("O3") // たまにバグる
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline int getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint998244353;
using mint = static_modint<(int)1e9+7>;
//using mint = modint; // mint::set_mod(m);
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
int mute_dump = 0;
int frac_print = 0;
#if __has_include(<atcoder/all>)
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
#endif
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); rep(i,9)cout<<MLE[i]; exit(0); } } // RE の代わりに MLE を出す
#endif
#ifndef HIDDEN_IN_VS // 折りたたみ用
//【階乗など(法が大きな素数)】
class Factorial_mint {
int n_max;
// 階乗と階乗の逆数の値を保持するテーブル
vm fac, fac_inv;
public:
// n! までの階乗とその逆数を前計算しておく.O(n)
Factorial_mint(int n) : n_max(n), fac(n + 1), fac_inv(n + 1) {
fac[0] = 1;
repi(i, 1, n) fac[i] = fac[i - 1] * i;
fac_inv[n] = fac[n].inv();
repir(i, n - 1, 0) fac_inv[i] = fac_inv[i + 1] * (i + 1);
}
Factorial_mint() : n_max(0) {} // ダミー
// n! を返す.
mint fact(int n) const {
Assert(0 <= n && n <= n_max);
return fac[n];
}
// 1/n! を返す(n が負なら 0 を返す)
mint fact_inv(int n) const {
Assert(n <= n_max);
if (n < 0) return 0;
return fac_inv[n];
}
// 1/n を返す.
mint inv(int n) const {
Assert(n > 0);
Assert(n <= n_max);
return fac[n - 1] * fac_inv[n];
}
// 1/n を返す(n < 0 も可)
mint inv_neg(int n) const {
Assert(n != 0);
Assert(abs(n) <= n_max);
if (n > 0) return fac[n - 1] * fac_inv[n];
else return -fac[-n - 1] * fac_inv[-n];
}
// 順列の数 nPr を返す.
mint perm(int n, int r) const {
Assert(n <= n_max);
if (r < 0 || n - r < 0) return 0;
return fac[n] * fac_inv[n - r];
}
// 順列の数 nPr の逆数を返す.
mint perm_inv(int n, int r) const {
Assert(n <= n_max);
Assert(0 <= r); Assert(r <= n);
return fac_inv[n] * fac[n - r];
}
// 二項係数 nCr を返す.
mint bin(int n, int r) const {
Assert(n <= n_max);
if (r < 0 || n - r < 0) return 0;
return fac[n] * fac_inv[r] * fac_inv[n - r];
}
// 二項係数の逆数 1/nCr を返す.
mint bin_inv(int n, int r) const {
Assert(n <= n_max);
Assert(r >= 0);
Assert(n - r >= 0);
return fac_inv[n] * fac[r] * fac[n - r];
}
// 多項係数 nC[rs] を返す.
mint mul(const vi& rs) const {
if (rs.empty()) return 1;
if (*min_element(all(rs)) < 0) return 0;
int n = accumulate(all(rs), 0);
Assert(n <= n_max);
mint res = fac[n];
repe(r, rs) res *= fac_inv[r];
return res;
}
// 重複組合せの数 nHr = n+r-1Cr を返す(0H0 = 1 とする)
mint hom(int n, int r) {
if (n == 0) return (int)(r == 0);
if (r < 0 || n - 1 < 0) return 0;
Assert(n + r - 1 <= n_max);
return fac[n + r - 1] * fac_inv[r] * fac_inv[n - 1];
}
// 負の二項係数 nCr を返す(n ≦ 0, r ≧ 0)
mint neg_bin(int n, int r) {
if (n == 0) return (int)(r == 0);
if (r < 0 || -n - 1 < 0) return 0;
Assert(-n + r - 1 <= n_max);
return (r & 1 ? -1 : 1) * fac[-n + r - 1] * fac_inv[r] * fac_inv[-n - 1];
}
// ポッホハマー記号 x^(n) を返す(n ≧ 0)
mint pochhammer(int x, int n) {
int x2 = x + n - 1;
if (x <= 0 && 0 <= x2) return 0;
if (x > 0) {
Assert(x2 <= n_max);
return fac[x2] * fac_inv[x - 1];
}
else {
Assert(-x <= n_max);
return (n & 1 ? -1 : 1) * fac[-x] * fac_inv[-x2 - 1];
}
}
// ポッホハマー記号の逆数 1/x^(n) を返す(n ≧ 0)
mint pochhammer_inv(int x, int n) {
int x2 = x + n - 1;
Assert(!(x <= 0 && 0 <= x2));
if (x > 0) {
Assert(x2 <= n_max);
return fac_inv[x2] * fac[x - 1];
}
else {
Assert(-x <= n_max);
return (n & 1 ? -1 : 1) * fac_inv[-x] * fac[-x2 - 1];
}
}
};
vm inv_all(int n) {
vm inv(n + 1);
constexpr int MOD = mint::mod();
inv[1] = 1;
repi(i, 2, n) {
inv[i] = MOD - mint(MOD / i) * inv[MOD % i];
}
return inv;
}
//【形式的冪級数】
struct MFPS {
using SMFPS = vector<pim>;
int n; // 係数の個数(次数 + 1)
vm c; // 係数列
inline static vm(*CONV)(const vm&, const vm&) = convolution; // 畳込み用の関数
// コンストラクタ(0,定数,係数列で初期化)
MFPS() : n(0) {}
MFPS(mint c0) : n(1), c({ c0 }) {}
MFPS(int c0) : n(1), c({ mint(c0) }) {}
MFPS(mint c0, int d) : n(d), c(n) { if (n > 0) c[0] = c0; }
MFPS(int c0, int d) : n(d), c(n) { if (n > 0) c[0] = c0; }
MFPS(const vm& c_) : n(sz(c_)), c(c_) {}
MFPS(const vi& c_) : n(sz(c_)), c(n) { rep(i, n) c[i] = c_[i]; }
// 代入
MFPS(const MFPS& f) = default;
MFPS& operator=(const MFPS& f) = default;
MFPS& operator=(const mint& c0) { n = 1; c = { c0 }; return *this; }
void push_back(mint cn) { c.emplace_back(cn); ++n; }
void pop_back() { c.pop_back(); --n; }
[[nodiscard]] mint back() { return c.back(); }
// 比較
[[nodiscard]] bool operator==(const MFPS& g) const { return c == g.c; }
[[nodiscard]] bool operator!=(const MFPS& g) const { return c != g.c; }
// アクセス
inline mint const& operator[](int i) const { return c[i]; }
inline mint& operator[](int i) { return c[i]; }
// 次数
[[nodiscard]] int deg() const { return n - 1; }
[[nodiscard]] int size() const { return n; }
static void set_conv(vm(*CONV_)(const vm&, const vm&)) {
CONV = CONV_;
}
// 加算
MFPS& operator+=(const MFPS& g) {
if (n >= g.n) rep(i, g.n) c[i] += g.c[i];
else {
rep(i, n) c[i] += g.c[i];
repi(i, n, g.n - 1) c.push_back(g.c[i]);
n = g.n;
}
return *this;
}
[[nodiscard]] MFPS operator+(const MFPS& g) const { return MFPS(*this) += g; }
// 定数加算
MFPS& operator+=(const mint& sc) {
if (n == 0) { n = 1; c = { sc }; }
else { c[0] += sc; }
return *this;
}
[[nodiscard]] MFPS operator+(const mint& sc) const { return MFPS(*this) += sc; }
[[nodiscard]] friend MFPS operator+(const mint& sc, const MFPS& f) { return f + sc; }
MFPS& operator+=(const int& sc) { *this += mint(sc); return *this; }
[[nodiscard]] MFPS operator+(const int& sc) const { return MFPS(*this) += sc; }
[[nodiscard]] friend MFPS operator+(const int& sc, const MFPS& f) { return f + sc; }
// 減算
MFPS& operator-=(const MFPS& g) {
if (n >= g.n) rep(i, g.n) c[i] -= g.c[i];
else {
rep(i, n) c[i] -= g.c[i];
repi(i, n, g.n - 1) c.push_back(-g.c[i]);
n = g.n;
}
return *this;
}
[[nodiscard]] MFPS operator-(const MFPS& g) const { return MFPS(*this) -= g; }
// 定数減算
MFPS& operator-=(const mint& sc) { *this += -sc; return *this; }
[[nodiscard]] MFPS operator-(const mint& sc) const { return MFPS(*this) -= sc; }
[[nodiscard]] friend MFPS operator-(const mint& sc, const MFPS& f) { return -(f - sc); }
MFPS& operator-=(const int& sc) { *this += -sc; return *this; }
[[nodiscard]] MFPS operator-(const int& sc) const { return MFPS(*this) -= sc; }
[[nodiscard]] friend MFPS operator-(const int& sc, const MFPS& f) { return -(f - sc); }
// 加法逆元
[[nodiscard]] MFPS operator-() const { return MFPS(*this) *= -1; }
// 定数倍
MFPS& operator*=(const mint& sc) { rep(i, n) c[i] *= sc; return *this; }
[[nodiscard]] MFPS operator*(const mint& sc) const { return MFPS(*this) *= sc; }
[[nodiscard]] friend MFPS operator*(const mint& sc, const MFPS& f) { return f * sc; }
MFPS& operator*=(const int& sc) { *this *= mint(sc); return *this; }
[[nodiscard]] MFPS operator*(const int& sc) const { return MFPS(*this) *= sc; }
[[nodiscard]] friend MFPS operator*(const int& sc, const MFPS& f) { return f * sc; }
// 右からの定数除算
MFPS& operator/=(const mint& sc) { *this *= sc.inv(); return *this; }
[[nodiscard]] MFPS operator/(const mint& sc) const { return MFPS(*this) /= sc; }
MFPS& operator/=(const int& sc) { *this /= mint(sc); return *this; }
[[nodiscard]] MFPS operator/(const int& sc) const { return MFPS(*this) /= sc; }
// 積
MFPS& operator*=(const MFPS& g) { c = CONV(c, g.c); n = sz(c); return *this; }
[[nodiscard]] MFPS operator*(const MFPS& g) const { return MFPS(*this) *= g; }
// 除算
[[nodiscard]] MFPS inv(int d) const {
Assert(!c.empty());
Assert(c[0] != 0);
MFPS g(c[0].inv());
for (int k = 1; k < d; k <<= 1) {
int len = max(min(2 * k, d), 1);
MFPS tmp(0, len);
rep(i, min(len, n)) tmp[i] = -c[i]; // -f
tmp *= g; // -f h
tmp.resize(len);
tmp[0] += 2; // 2 - f h
g *= tmp; // (2 - f h) h
g.resize(len);
}
return g;
}
MFPS& operator/=(const MFPS& g) { return *this *= g.inv(max(n, g.n)); }
[[nodiscard]] MFPS operator/(const MFPS& g) const { return MFPS(*this) /= g; }
// 余り付き除算
[[nodiscard]] MFPS quotient(const MFPS& g) const {
if (n < g.n) return MFPS();
return ((this->rev() / g.rev()).resize(n - g.n + 1)).rev();
}
[[nodiscard]] MFPS reminder(const MFPS& g) const {
return (*this - this->quotient(g) * g).resize();
}
[[nodiscard]] pair<MFPS, MFPS> quotient_remainder(const MFPS& g) const {
pair<MFPS, MFPS> res;
res.first = this->quotient(g);
res.second = (*this - res.first * g).resize();
return res;
}
// スパース積
MFPS& operator*=(const SMFPS& g) {
// g の定数項だけ例外処理
auto it0 = g.begin();
mint g0 = 0;
if (it0->first == 0) {
g0 = it0->second;
it0++;
}
// 後ろからインライン配る DP
repir(i, n - 1, 0) {
// 上位項に係数倍して配っていく.
for (auto it = it0; it != g.end(); it++) {
auto [j, gj] = *it;
if (i + j >= n) break;
c[i + j] += c[i] * gj;
}
// 定数項は最後に配るか消去しないといけない.
c[i] *= g0;
}
return *this;
}
[[nodiscard]] MFPS operator*(const SMFPS& g) const { return MFPS(*this) *= g; }
// スパース商
MFPS& operator/=(const SMFPS& g) {
// g の定数項だけ例外処理
auto it0 = g.begin();
Assert(it0->first == 0 && it0->second != 0);
mint g0_inv = it0->second.inv();
it0++;
// 前からインライン配る DP(後ろに累積効果あり)
rep(i, n) {
// 定数項は最初に配らないといけない.
c[i] *= g0_inv;
// 上位項に係数倍して配っていく.
for (auto it = it0; it != g.end(); it++) {
auto [j, gj] = *it;
if (i + j >= n) break;
c[i + j] -= c[i] * gj;
}
}
return *this;
}
[[nodiscard]] MFPS operator/(const SMFPS& g) const { return MFPS(*this) /= g; }
// 係数反転
[[nodiscard]] MFPS rev() const { MFPS h = *this; reverse(all(h.c)); return h; }
// 単項式
[[nodiscard]] static MFPS monomial(int d, mint coef = 1) {
MFPS mono(0, d + 1);
mono[d] = coef;
return mono;
}
// 不要な高次項の除去
MFPS& resize() {
// 最高次の係数が非 0 になるまで削る.
while (n > 0 && c[n - 1] == 0) {
c.pop_back();
n--;
}
return *this;
}
// x^d 以上の項を除去する.
MFPS& resize(int d) {
n = d;
c.resize(d);
return *this;
}
// 不定元への代入
[[nodiscard]] mint assign(const mint& x) const {
mint val = 0;
repir(i, n - 1, 0) val = val * x + c[i];
return val;
}
// 係数のシフト
MFPS& operator>>=(int d) {
n += d;
c.insert(c.begin(), d, 0);
return *this;
}
MFPS& operator<<=(int d) {
n -= d;
if (n <= 0) { c.clear(); n = 0; }
else c.erase(c.begin(), c.begin() + d);
return *this;
}
[[nodiscard]] MFPS operator>>(int d) const { return MFPS(*this) >>= d; }
[[nodiscard]] MFPS operator<<(int d) const { return MFPS(*this) <<= d; }
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const MFPS& f) {
if (f.n == 0) os << 0;
else {
rep(i, f.n) {
os << f[i] << "z^" << i;
if (i < f.n - 1) os << " + ";
}
}
return os;
}
#endif
};
//【対数関数】O(n log n)
MFPS log_fps(const MFPS& f, int d, const Factorial_mint& fm) {
int n = sz(f);
MFPS g(0, max(n - 1, 1));
repi(i, 1, n - 1) g[i - 1] = f[i] * i; // f'(z)
g *= f.inv(d - 1); // f'(z) / f(z)
g.resize(d);
repir(i, d - 1, 1) g[i] = g[i - 1] * fm.inv(i); // ∫ f'(z) / f(z) dz
g[0] = 0;
return g;
}
//【指数関数】O(n log n)
MFPS exp_fps(const MFPS& f, int d, const Factorial_mint& fm) {
// ニュートン法で log g = f なる g を見つける.
MFPS g(1);
for (int k = 1; k < d; k <<= 1) {
int len = max(min(2 * k, d), 1);
auto tmp = log_fps(g, len, fm); // log h
rep(i, len) tmp[i] = (i < sz(f) ? f[i] : 0) - tmp[i]; // f - log h
tmp[0] += 1; // f + 1 - log h
g *= tmp; // h (f + 1 - log h)
g.resize(len);
}
return g;
}
//【累乗(有理数)】O(n log n)
MFPS rational_pow_fps(const MFPS& f, ll num, ll dnm, int d, const Factorial_mint& fm) {
Assert(sz(f) > 0 && f[0] == 1);
// f^(num/dnm) = exp(num/dnm log f(x)) を用いて f^(num/dnm) を計算する.
return exp_fps(log_fps(f, d, fm) * mint(num) / mint(dnm), d, fm);
}
//【行列】
template <class T>
struct Matrix {
int n, m; // 行列のサイズ(n 行 m 列)
vector<vector<T>> v; // 行列の成分
// n×m 零行列で初期化する.
Matrix(int n, int m) : n(n), m(m), v(n, vector<T>(m)) {}
// n×n 単位行列で初期化する.
Matrix(int n) : n(n), m(n), v(n, vector<T>(n)) { rep(i, n) v[i][i] = T(1); }
// 二次元配列 a[0..n)[0..m) の要素で初期化する.
Matrix(const vector<vector<T>>& a) : n(sz(a)), m(sz(a[0])), v(a) {}
Matrix() : n(0), m(0) {}
// 代入
Matrix(const Matrix&) = default;
Matrix& operator=(const Matrix&) = default;
// アクセス
inline vector<T> const& operator[](int i) const { return v[i]; }
inline vector<T>& operator[](int i) { return v[i]; }
// 入力
friend istream& operator>>(istream& is, Matrix& a) {
rep(i, a.n) rep(j, a.m) is >> a.v[i][j];
return is;
}
// 行の追加
void push_back(const vector<T>& a) {
Assert(sz(a) == m);
v.push_back(a);
n++;
}
// 行の削除
void pop_back() {
Assert(n > 0);
v.pop_back();
n--;
}
// サイズ変更
void resize(int n_) {
v.resize(n_);
n = n_;
}
void resize(int n_, int m_) {
n = n_;
m = m_;
v.resize(n);
rep(i, n) v[i].resize(m);
}
// 空か
bool empty() const { return min(n, m) == 0; }
// 比較
bool operator==(const Matrix& b) const { return n == b.n && m == b.m && v == b.v; }
bool operator!=(const Matrix& b) const { return !(*this == b); }
// 加算,減算,スカラー倍
Matrix& operator+=(const Matrix& b) {
rep(i, n) rep(j, m) v[i][j] += b[i][j];
return *this;
}
Matrix& operator-=(const Matrix& b) {
rep(i, n) rep(j, m) v[i][j] -= b[i][j];
return *this;
}
Matrix& operator*=(const T& c) {
rep(i, n) rep(j, m) v[i][j] *= c;
return *this;
}
Matrix operator+(const Matrix& b) const { return Matrix(*this) += b; }
Matrix operator-(const Matrix& b) const { return Matrix(*this) -= b; }
Matrix operator*(const T& c) const { return Matrix(*this) *= c; }
friend Matrix operator*(const T& c, const Matrix<T>& a) { return a * c; }
Matrix operator-() const { return Matrix(*this) *= T(-1); }
// 行列ベクトル積 : O(m n)
vector<T> operator*(const vector<T>& x) const {
vector<T> y(n);
rep(i, n) rep(j, m) y[i] += v[i][j] * x[j];
return y;
}
// ベクトル行列積 : O(m n)
friend vector<T> operator*(const vector<T>& x, const Matrix& a) {
vector<T> y(a.m);
rep(i, a.n) rep(j, a.m) y[j] += x[i] * a[i][j];
return y;
}
// 積:O(n^3)
Matrix operator*(const Matrix& b) const {
Matrix res(n, b.m);
rep(i, res.n) rep(k, m) rep(j, res.m) res[i][j] += v[i][k] * b[k][j];
return res;
}
Matrix& operator*=(const Matrix& b) { *this = *this * b; return *this; }
// 累乗:O(n^3 log d)
Matrix pow(ll d) const {
Matrix res(n), pow2 = *this;
while (d > 0) {
if (d & 1) res *= pow2;
pow2 *= pow2;
d >>= 1;
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Matrix& a) {
rep(i, a.n) {
os << "[";
rep(j, a.m) os << a[i][j] << " ]"[j == a.m - 1];
if (i < a.n - 1) os << "\n";
}
return os;
}
#endif
};
//【線形方程式】O(n m min(n, m))
template <class T>
vector<T> gauss_jordan_elimination(const Matrix<T>& A, const vector<T>& b, vector<vector<T>>* xs = nullptr) {
int n = A.n, m = A.m;
// v : 拡大係数行列 (A | b)
vector<vector<T>> v(n, vector<T>(m + 1));
rep(i, n) rep(j, m) v[i][j] = A[i][j];
rep(i, n) v[i][m] = b[i];
// pivots[i] : 第 i 行のピボットが第何列にあるか
vi pivots;
// 注目位置を v[i][j] とする.
int i = 0, j = 0;
while (i < n && j <= m) {
// 注目列の下方の行から非 0 成分を見つける.
int i2 = i;
while (i2 < n && v[i2][j] == T(0)) i2++;
// 見つからなかったら注目位置を右に移す.
if (i2 == n) { j++; continue; }
// 見つかったら第 i 行とその行を入れ替える.
if (i != i2) swap(v[i], v[i2]);
// v[i][j] をピボットに選択する.
pivots.push_back(j);
// v[i][j] が 1 になるよう第 i 行全体を v[i][j] で割る.
T vij_inv = T(1) / v[i][j];
repi(j2, j, m) v[i][j2] *= vij_inv;
// 第 i 行以外の第 j 列の成分が全て 0 になるよう第 i 行を定数倍して減じる.
rep(i2, n) {
if (v[i2][j] == T(0) || i2 == i) continue;
T mul = v[i2][j];
repi(j2, j, m) v[i2][j2] -= v[i][j2] * mul;
}
// 注目位置を右下に移す.
i++; j++;
}
// 最後に見つかったピボットの位置が第 m 列ならば解なし.
if (!pivots.empty() && pivots.back() == m) return vector<T>();
// A x = b の特殊解 x0 の構成(任意定数は全て 0 にする)
vector<T> x0(m);
int rnk = sz(pivots);
rep(i, rnk) x0[pivots[i]] = v[i][m];
// 同次形 A x = 0 の一般解 {x} の基底の構成(任意定数を 1-hot にする)
if (xs != nullptr) {
xs->clear();
int i = 0;
rep(j, m) {
if (i < rnk && j == pivots[i]) {
i++;
continue;
}
vector<T> x(m);
x[j] = T(1);
rep(i2, i) x[pivots[i2]] = -v[i2][j];
xs->emplace_back(move(x));
}
}
return x0;
}
// https://qiita.com/satoshin_astonish/items/a628ec64f29e77501d07
namespace satoshin {
/* 内積 */
double dot(const vl& x, const vd& y) {
double z = 0.0;
const int n = sz(x);
for (int i = 0; i < n; ++i) z += x[i] * y[i];
return z;
}
double dot(const vd& x, const vd& y) {
double z = 0.0;
const int n = sz(x);
for (int i = 0; i < n; ++i) z += x[i] * y[i];
return z;
}
double dot(const vl& x, const vl& y) {
double z = 0.0;
const int n = sz(x);
for (int i = 0; i < n; ++i) z += x[i] * y[i];
return z;
}
/* Gram-Schmidtの直交化 */
tuple<vd, vvd> Gram_Schmidt_squared(const vvl& b) {
const int n = sz(b), m = sz(b[0]); int i, j, k;
vd B(n);
vvd GSOb(n, vd(m)), mu(n, vd(n));
for (i = 0; i < n; ++i) {
mu[i][i] = 1.0;
for (j = 0; j < m; ++j) GSOb[i][j] = (double)b[i][j];
for (j = 0; j < i; ++j) {
mu[i][j] = dot(b[i], GSOb[j]) / dot(GSOb[j], GSOb[j]);
for (k = 0; k < m; ++k) GSOb[i][k] -= mu[i][j] * GSOb[j][k];
}
B[i] = dot(GSOb[i], GSOb[i]);
}
return std::forward_as_tuple(B, mu);
}
/* 部分サイズ基底簡約 */
void SizeReduce(vvl& b, vvd& mu, const int i, const int j) {
ll q;
const int m = sz(b[0]);
if (mu[i][j] > 0.5 || mu[i][j] < -0.5) {
q = (ll)round(mu[i][j]);
for (int k = 0; k < m; ++k) b[i][k] -= q * b[j][k];
for (int k = 0; k <= j; ++k) mu[i][k] -= mu[j][k] * q;
}
}
/* LLL基底簡約 */
void LLLReduce(vvl& b, const float d = 0.99) {
const int n = sz(b), m = sz(b[0]); int j, i, h;
double t, nu, BB, C;
auto [B, mu] = Gram_Schmidt_squared(b);
ll tmp;
for (int k = 1; k < n;) {
h = k - 1;
for (j = h; j > -1; --j) SizeReduce(b, mu, k, j);
//Checks if the lattice basis matrix b satisfies Lovasz condition.
if (k > 0 && B[k] < (d - mu[k][h] * mu[k][h]) * B[h]) {
for (i = 0; i < m; ++i) { tmp = b[h][i]; b[h][i] = b[k][i]; b[k][i] = tmp; }
nu = mu[k][h]; BB = B[k] + nu * nu * B[h]; C = 1.0 / BB;
mu[k][h] = nu * B[h] * C; B[k] *= B[h] * C; B[h] = BB;
for (i = 0; i <= k - 2; ++i) {
t = mu[h][i]; mu[h][i] = mu[k][i]; mu[k][i] = t;
}
for (i = k + 1; i < n; ++i) {
t = mu[i][k]; mu[i][k] = mu[i][h] - nu * t;
mu[i][h] = t + mu[k][h] * mu[i][k];
}
--k;
}
else ++k;
}
}
}
vl LLLReduce(const vvm& lat_) {
int h = sz(lat_);
int w = sz(lat_[0]);
vvl lat(h + w, vl(w));
rep(i, h) rep(j, w) lat[i][j] = lat_[i][j].val();
rep(i, w) lat[h + i][i] = mint::mod();
h = sz(lat);
satoshin::LLLReduce(lat);
// L1 ノルムをチェックする.
ll sum = 0;
rep(j, w) sum += abs(lat[0][j]);
dump("L1:", sum);
// L1 ノルムが大きいものは捨てる.
repi(i, 1, h - 1) {
ll sum2 = 0;
rep(j, w) sum2 += abs(lat[i][j]);
if (sum2 > sum * 10.) {
lat.resize(i);
h = i;
break;
}
}
dump("lat:"); frac_print = 1; dumpel(lat); frac_print = 0;
return lat[0];
}
vl LLLReduce2(const vvm& xs) {
int h = sz(xs);
int w = sz(xs[0]);
vl lat0(w);
#ifdef _MSC_VER
string cmd;
cmd += "wolframscript -code \"MOD=";
cmd += to_string(mint::mod());
cmd += ";";
cmd += "SortBy[LatticeReduce@Join[{";
rep(i, h) {
cmd += "{";
rep(j, w) {
cmd += to_string(xs[i][j].val());
cmd += ",";
}
if (cmd.back() == ',') cmd.pop_back();
cmd += "},";
}
if (cmd.back() == ',') cmd.pop_back();
cmd += "},MOD IdentityMatrix[";
cmd += to_string(w);
cmd += "]],N@Norm@# &]\"";
//dump("cmd:", cmd);
FILE* fp = _popen(cmd.c_str(), "r");
char buf[1 << 16];
while (fgets(buf, sizeof(buf), fp)) printf("%s", buf);
_pclose(fp);
stringstream ss{ buf + 2 };
rep(j, w) {
string s;
getline(ss, s, ' ');
lat0[j] = stol(s);
}
#endif
return lat0;
}
string to_signed_string(mint x) {
int v = x.val();
if (v > mint::mod() / 2) v -= mint::mod();
return to_string(v);
}
#endif // 折りたたみ用
// i=n に対する愚直解を返す.
mint naive_1(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int nL = n / 2, nR = n - nL;
// O(n^4) の挿入 DP
// dp[l][cl][cr][tp]:
// l : 左側の長さ
// cl : 左側の {i-1, i-2} 以外の差が 1 の場所の数
// cr : 右側の {i-1, i-2} 以外の差が 1 の場所の数
// tp : 0: i-1 が左で i-2 と隣接していない
// 1: i-1 が左で i-2 と隣接している
// 2: i-1 が右で i-2 と隣接していない
// 3: i-1 が右で i-2 と隣接している
vvvvm dp(nL + 1, vvvm(nL, vvm(nR, vm(4))));
auto print_dp = [&](int i) {
repi(l, 0, nL) rep(cl, nL) rep(cr, nR) rep(tp, 4) {
int r = i - l;
if (dp[l][cl][cr][tp] != 0) {
//dump("l, r, cl, cr, tp:", l, r, cl, cr, tp, "dp:", dp[l][cl][cr][tp]);
}
}
};
//dump("-------------- i:", 0, "---------------");
dp[1][0][0][0] = 1;
dp[0][0][0][2] = 1;
print_dp(1);
repi(i, 1, n - 1) {
//dump("-------------- i:", i, "---------------");
vvvvm ndp(nL + 1, vvvm(nL, vvm(nR, vm(4))));
repi(l, max(i - nR, 0), min(nL, i)) {
int r = i - l; // <= nR
rep(cl, max(l, 1)) rep(cr, max(r, 1)) {
// ---------------- L ----------------
if (l < nL) {
// (i-1, i-2) -> (i-1, i, i-2)
ndp[l + 1][cl][cr][1] += dp[l][cl][cr][1];
// (i-1, x) -> (i-1, i, x)
if (cl + 1 < nL) ndp[l + 1][cl + 1][cr][1] += dp[l][cl][cr][1];
ndp[l + 1][cl][cr][1] += 2 * dp[l][cl][cr][0];
// (x, x+1) -> (x, i, x+1)
if (cl > 0) {
ndp[l + 1][cl - 1][cr][0] += cl * dp[l][cl][cr][0];
ndp[l + 1][cl - 1 + 1][cr][0] += cl * dp[l][cl][cr][1];
ndp[l + 1][cl - 1][cr][0] += cl * dp[l][cl][cr][2];
if (cr + 1 < nR) ndp[l + 1][cl - 1][cr + 1][0] += cl * dp[l][cl][cr][3];
}
// (x, y) -> (x, i, y)
ndp[l + 1][cl][cr][0] += max(l + 1 - cl - 2, 0) * dp[l][cl][cr][0];
if (cl + 1 < nL) ndp[l + 1][cl + 1][cr][0] += max(l + 1 - cl - 2, 0) * dp[l][cl][cr][1];
ndp[l + 1][cl][cr][0] += (l + 1 - cl) * dp[l][cl][cr][2];
if (cr + 1 < nR) ndp[l + 1][cl][cr + 1][0] += (l + 1 - cl) * dp[l][cl][cr][3];
}
// ---------------- R ----------------
if (r < nR) {
// (i-1, i-2) -> (i-1, i, i-2)
ndp[l][cl][cr][3] += dp[l][cl][cr][3];
// (i-1, x) -> (i-1, i, x)
if (cr + 1 < nR) ndp[l][cl][cr + 1][3] += dp[l][cl][cr][3];
ndp[l][cl][cr][3] += 2 * dp[l][cl][cr][2];
// (x, x+1) -> (x, i, x+1)
if (cr > 0) {
ndp[l][cl][cr - 1][2] += cr * dp[l][cl][cr][2];
ndp[l][cl][cr - 1 + 1][2] += cr * dp[l][cl][cr][3];
ndp[l][cl][cr - 1][2] += cr * dp[l][cl][cr][0];
if (cl + 1 < nL) ndp[l][cl + 1][cr - 1][2] += cr * dp[l][cl][cr][1];
}
// (x, y) -> (x, i, y)
ndp[l][cl][cr][2] += max(r + 1 - cr - 2, 0) * dp[l][cl][cr][2];
if (cr + 1 < nR) ndp[l][cl][cr + 1][2] += max(r + 1 - cr - 2, 0) * dp[l][cl][cr][3];
ndp[l][cl][cr][2] += (r + 1 - cr) * dp[l][cl][cr][0];
if (cl + 1 < nL) ndp[l][cl + 1][cr][2] += (r + 1 - cr) * dp[l][cl][cr][1];
}
}
}
dp = move(ndp);
print_dp(i + 1);
}
mint res = dp[nL][0][0][0] + dp[nL][0][0][2];
return res;
}
// i=[0..n) に対する愚直解を返す.
vm naive() {
int n = 100;
vm seq;
// seq0 : 元の数列
vm seq0;
repi(i, 0, n) seq0.push_back(naive_1(i));
seq = seq0;
// f = 1/(1-g) なる OGF g が P-recursive と予想する場合([z^0]f(z) = 1)
//MFPS f(seq0);
//auto g = f.inv(sz(seq0));
//seq = g.c;
// g = 1/(1-f) なる OGF g が P-recursive と予想する場合([z^0]f(z) = 0)
//MFPS f(seq0);
//auto g = (1 - f).inv(sz(seq0));
//seq = g.c;
// f = exp(g) なる EGF g が P-recursive と予想する場合([z^0]f(z) = 1)
//Factorial_mint fm((int)2.5e5 + 10);
//rep(i, sz(seq0)) seq0[i] *= fm.fact_inv(i);
//MFPS f(seq0);
//auto g = log_fps(f, sz(seq0), fm);
//seq = g.c;
//rep(i, sz(seq)) seq[i] *= fm.fact(i);
// g = exp(f) なる EGF g が P-recursive と予想する場合([z^0]f(z) = 0)
//Factorial_mint fm((int)2.5e5 + 10);
//rep(i, sz(seq0)) seq0[i] *= fm.fact_inv(i);
//MFPS f(seq0);
//auto g = exp_fps(f, sz(seq0), fm);
//seq = g.c;
//rep(i, sz(seq)) seq[i] *= fm.fact(i);
#ifdef _MSC_VER
// 埋め込み用
string eb;
eb += "vm seq = {";
rep(i, sz(seq)) eb += to_string(seq[i].val()) + ",";
if (eb.back() == ',') eb.pop_back();
eb += "};\n\n";
cout << eb;
#endif
return seq;
}
// 変数係数線形漸化式の係数を計算し,埋め込み用のコードを出力する.
vvm embed_coefs_1D(const vm& seq, int TRM_ini, int DEG_ini, int LLL) {
int n = sz(seq);
// TRM 項間の,係数多項式の次数 DEG 未満の変数係数線形漸化式
// Σt∈[0..TRM) Σd∈[0..DEG) coefs[t][d] (i-TRM+1+t)^d seq[i-t] = 0
// を探す.
int TRM = TRM_ini, DEG = DEG_ini;
int P_MAX = max(TRM, DEG);
while (1) {
//dump("TRM:", TRM, "DEG:", DEG);
int h = n - TRM + 1;
int w = TRM * DEG;
// 行列方程式 A x = 0 を解いて一般解の基底 xs を求める.
Matrix<mint> A(h, w);
repi(i, TRM - 1, n - 1) {
rep(t, TRM) rep(d, DEG) {
A[i - TRM + 1][t * DEG + d] = mint(i - TRM + 1 + t).pow(d) * seq[i - t];
}
}
vvm xs;
gauss_jordan_elimination(A, vm(h), &xs);
// 自明解 x = 0 しか存在しない場合は失敗.
if (xs.empty()) {
while (1) {
DEG++;
if (DEG > P_MAX) { DEG = 1; TRM++; };
if (TRM > P_MAX) { TRM = 1; P_MAX++; };
if (max(TRM, DEG) == P_MAX) break;
}
continue;
}
dump("TRM:", TRM, "DEG:", DEG);
dump("#eq:", h, "#var:", w);
dump("xs:"); frac_print = 1; dumpel(xs); frac_print = 0;
// 変数係数線形漸化式の係数
vvm coefs(TRM, vm(DEG));
if (LLL == 0) {
rep(t, TRM) rep(d, DEG) coefs[t][d] = xs.back()[t * DEG + d];
}
else if (LLL == 1) {
// A x = 0 の解空間の基底に LLL を適用する.
auto lat0 = LLLReduce(xs);
rep(t, TRM) rep(d, DEG) coefs[t][d] = lat0[t * DEG + d];
}
else if (LLL == 2) {
// A x = 0 の解空間の基底に本気の LLL を適用する(埋め込み専用)
auto lat0 = LLLReduce2(xs);
rep(t, TRM) rep(d, DEG) coefs[t][d] = lat0[t * DEG + d];
}
// 分母チェック
#ifdef _MSC_VER
cout << "dnm 1D:" << endl;
string cmd;
cmd += "wolframscript -code \"MOD=";
cmd += to_string(mint::mod());
cmd += ";";
cmd += "toFrac[x_]:=Module[{},Do[num=Mod[x*dnm,MOD,-MOD/2];If[Abs[num]<=Sqrt@MOD,Return[num/dnm,Module]],{dnm,1,Sqrt@MOD}]];";
cmd += "Factor[";
rep(d, DEG) {
cmd += to_string(coefs[0][d].val());
cmd += "*(i-";
cmd += to_string(TRM);
cmd += "+1)^";
cmd += to_string(d);
cmd += "+";
}
cmd.pop_back();
cmd += ",Modulus->MOD]/.x_Integer:>toFrac[x]\"";
//dump("cmd:", cmd);
FILE* fp = _popen(cmd.c_str(), "r");
char buf[4096];
while (fgets(buf, sizeof(buf), fp)) printf("%s", buf);
_pclose(fp);
#endif
#ifdef _MSC_VER
// 埋め込み用の文字列を出力する.
string eb = "\n";
repi(t, 1, TRM - 1) {
eb += "tmp=seq[i-" + to_string(t) + "];";
rep(d, DEG) {
eb += "num+=" + to_signed_string(coefs[t][d]) + "*tmp;\n";
if (d < DEG - 1) eb += "tmp*=i-" + to_string(-t + TRM - 1) + ";";
}
}
eb += "\ntmp=1;";
rep(d, DEG) {
eb += "dnm+=" + to_signed_string(coefs[0][d]) + "*tmp;\n";
if (d < DEG - 1) eb += "tmp*=i-" + to_string(TRM - 1) + ";";
}
eb += "\n";
cout << eb;
#endif
return coefs;
}
return vvm();
}
// 数列 seq を延長して seq[0..N] にする.
void solve_1D(vm& seq, int N, vvm coefs) {
int TRM = sz(coefs);
int DEG = sz(coefs[0]);
int n = sz(seq);
seq.resize(N + 1);
// TRM 項間の,係数多項式の次数 DEG 未満の変数係数線形漸化式
// Σt∈[0..TRM) Σd∈[0..DEG) coefs[t][f] (i-TRM+1+t)^d a[i-t] = 0
// を用いて数列 a を延長する.
repi(i, n, N) {
mint dnm = 0;
mint pow_i = 1;
rep(d, DEG) {
dnm += coefs[0][d] * pow_i;
pow_i *= i - TRM + 1;
}
mint num = 0;
repi(t, 1, TRM - 1) {
mint pow_i = 1;
rep(d, DEG) {
num += coefs[t][d] * pow_i * seq[i - t];
pow_i *= i - TRM + 1 + t;
}
}
// dnm * a[i] + num = 0 を解く.分母 0 に注意!
if (dnm == 0) {
dump("DIVISION BY ZERO at i =", i);
Assert(dnm != 0);
}
seq[i] = -num / dnm;
}
}
// 数列 seq を延長して seq[0..N] にする.
void solve_1D(vm& seq, int N) {
int n = sz(seq);
seq.resize(N + 1);
// 除算回避用
//auto inv = inv_all(3 * N + 10);
// TRM 項間の,係数多項式の次数 DEG 未満の変数係数線形漸化式
// Σt∈[0..TRM) Σd∈[0..DEG) coefs[t][f] (i-TRM+1+t)^d a[i-t] = 0
// を用いて数列 a を延長する.
repi(i, n, N) {
mint num = 0, dnm = 0, tmp;
// --------------- embed_coefs() からの出力を貼る ----------------
tmp = seq[i - 1]; num += 38672 * tmp;
tmp *= i - 7; num += 8304 * tmp;
tmp *= i - 7; num += -24700 * tmp;
tmp *= i - 7; num += -16352 * tmp;
tmp *= i - 7; num += -4970 * tmp;
tmp *= i - 7; num += -860 * tmp;
tmp *= i - 7; num += -90 * tmp;
tmp *= i - 7; num += -4 * tmp;
tmp *= i - 7; num += 0 * tmp;
tmp = seq[i - 2]; num += -32232 * tmp;
tmp *= i - 6; num += -38476 * tmp;
tmp *= i - 6; num += -4178 * tmp;
tmp *= i - 6; num += 4655 * tmp;
tmp *= i - 6; num += 3299 * tmp;
tmp *= i - 6; num += 827 * tmp;
tmp *= i - 6; num += 127 * tmp;
tmp *= i - 6; num += 10 * tmp;
tmp *= i - 6; num += 0 * tmp;
tmp = seq[i - 3]; num += 10560 * tmp;
tmp *= i - 5; num += -5560 * tmp;
tmp *= i - 5; num += -20020 * tmp;
tmp *= i - 5; num += -3756 * tmp;
tmp *= i - 5; num += 525 * tmp;
tmp *= i - 5; num += 433 * tmp;
tmp *= i - 5; num += 105 * tmp;
tmp *= i - 5; num += 23 * tmp;
tmp *= i - 5; num += 2 * tmp;
tmp = seq[i - 4]; num += 13120 * tmp;
tmp *= i - 4; num += -31552 * tmp;
tmp *= i - 4; num += 14008 * tmp;
tmp *= i - 4; num += -783 * tmp;
tmp *= i - 4; num += 4066 * tmp;
tmp *= i - 4; num += -420 * tmp;
tmp *= i - 4; num += 0 * tmp;
tmp *= i - 4; num += -17 * tmp;
tmp *= i - 4; num += -6 * tmp;
tmp = seq[i - 5]; num += -16960 * tmp;
tmp *= i - 3; num += 49792 * tmp;
tmp *= i - 3; num += -28020 * tmp;
tmp *= i - 3; num += 5658 * tmp;
tmp *= i - 3; num += -732 * tmp;
tmp *= i - 3; num += 150 * tmp;
tmp *= i - 3; num += -56 * tmp;
tmp *= i - 3; num += 8 * tmp;
tmp *= i - 3; num += 0 * tmp;
tmp = seq[i - 6]; num += 190536 * tmp;
tmp *= i - 2; num += -270820 * tmp;
tmp *= i - 2; num += 198766 * tmp;
tmp *= i - 2; num += -105021 * tmp;
tmp *= i - 2; num += 38739 * tmp;
tmp *= i - 2; num += -9429 * tmp;
tmp *= i - 2; num += 1543 * tmp;
tmp *= i - 2; num += -162 * tmp;
tmp *= i - 2; num += 8 * tmp;
tmp = seq[i - 7]; num += -131040 * tmp;
tmp *= i - 1; num += 237904 * tmp;
tmp *= i - 1; num += -194384 * tmp;
tmp *= i - 1; num += 90650 * tmp;
tmp *= i - 1; num += -26527 * tmp;
tmp *= i - 1; num += 5117 * tmp;
tmp *= i - 1; num += -659 * tmp;
tmp *= i - 1; num += 53 * tmp;
tmp *= i - 1; num += -2 * tmp;
tmp = seq[i - 8]; num += -978880 * tmp;
tmp *= i - 0; num += 1324856 * tmp;
tmp *= i - 0; num += -829696 * tmp;
tmp *= i - 0; num += 308411 * tmp;
tmp *= i - 0; num += -73792 * tmp;
tmp *= i - 0; num += 11668 * tmp;
tmp *= i - 0; num += -1198 * tmp;
tmp *= i - 0; num += 73 * tmp;
tmp *= i - 0; num += -2 * tmp;
tmp = 1; dnm += 0 * tmp;
tmp *= i - 8; dnm += 10480 * tmp;
tmp *= i - 8; dnm += 9384 * tmp;
tmp *= i - 8; dnm += 3378 * tmp;
tmp *= i - 8; dnm += 664 * tmp;
tmp *= i - 8; dnm += 74 * tmp;
tmp *= i - 8; dnm += 4 * tmp;
tmp *= i - 8; dnm += 0 * tmp;
tmp *= i - 8; dnm += 0 * tmp;
// --------------------------------------------------------------
// dnm * a[i] + num = 0 を解く.分母 0 に注意!
if (dnm == 0) {
dump("DIVISION BY ZERO at i =", i);
Assert(dnm != 0);
}
seq[i] = -num / dnm;
// 除算回避用
//mint dnm_inv = -inv[3] * inv[3 * i - 1] * inv[3 * i] * inv[3 * i + 1];
//seq[i] = -num * dnm_inv;
}
}
vm seq = { 0,1,2,2,8,28,152,952,7208,62296,605864,6522952,76951496,986411272,647501133,653303042,170637030,248109503,700583494,619914523,682935856,443753916,423068688,507501942,315541972,110825117,848156395,798418282,920964362,23823302,114894774,279365223,992413784,833179437,785518302,524368220,42214454,140345871,188150268,808714798,718376249,732000901,955005007,139255097,484615744,615066955,726914809,856989248,460819998,321277105,536397091,555447300,597473569,217709372,24981477,143561526,171000806,137649694,749333590,700935246,916763337,762367836,296796066,236278263,398507715,148909632,568524543,926513708,163591024,339393165,549241395,548924577,915489821,706913104,380913764,993919668,895691202,628078606,542382606,735060428,385303214,453133962,470556393,439972973,4764973,459438929,49172129,93448766,14767450,302365655,44994640,637650527,462797839,174866371,963824426,761996745,999013044,209330964,997280223,561428453,300321098 };
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
//【方法】
// 愚直を書いて集めたデータをもとに変数係数線形漸化式を復元する.
//【使い方】
// 1. vm seq = naive() を実装する.
// 2. coefs = embed_coefs(seq, TRM_ini, DEG_ini, LLL); を実行する.
// 3. 出力を solve() 内に貼る.
// 4. solve(seq, n, [coefs]) で勝手に seq[0..n] を求めてくれる.
// 愚直解を用意する.再計算がイヤなら埋め込む.
// auto seq = naive();
// 愚直解を渡して変数係数線形漸化式の係数を得る.再計算がイヤなら埋め込む.
// 引数:seq, TRM_ini, DEG_ini, LLL
// auto coefs = embed_coefs_1D(seq, 1, 1, 2);
int n;
cin >> n;
// 数列 seq を seq[0..n] に延長する.
// solve_1D(seq, n, coefs);
solve_1D(seq, n);
//dump(seq);
// OGF f = 1/(1-g)
//MFPS g(seq);
//MFPS f = (1 - g).inv(sz(seq));
//seq = f.c;
// OGF g = 1/(1-f)
//MFPS g(seq);
//MFPS f = 1 - g.inv(sz(seq));
//seq = f.c;
// EGF f = exp(g)
//Factorial_mint fm(sz(seq) + 10);
//rep(i, sz(seq)) seq[i] *= fm.fact_inv(i);
//MFPS g(seq);
//auto f = exp_fps(g, sz(seq), fm);
//rep(i, sz(seq)) f[i] *= fm.fact(i);
//seq = f.c;
// EGF g = exp(f)
//Factorial_mint fm(sz(seq) + 10);
//rep(i, sz(seq)) seq[i] *= fm.fact_inv(i);
//MFPS g(seq);
//auto f = log_fps(g, sz(seq), fm);
//rep(i, sz(seq)) f[i] *= fm.fact(i);
//seq = f.c;
//dump(seq);
cout << seq[n] << "\n";
}
/*
vm seq = {1,1,6,47,420,4059,41316,436345,4737018,52535950,592667532,790232955,595159212,497898061,131187228,242830337,235263705,925665672};
TRM: 4 DEG: 4
#eq: 15 #var: 16
xs:
0: 27/650 1089/26000 -18899/21257 19223/16687 -9/325 -519/3250 -5697/26000 -2241/26000 -51/130 4161/2600 -81/40 1023/1300 -45/4 63/4 -7 1
{{-2160, -2178, -729, -81, 1440, 8304, 11394, 4482, 20400, -83220, 105300, -40920, 585000, -819000, 364000, -52000}, {-146128659, -122390289, -36840368, 106822665, 97419106, 29386523, -52722915, 78611988, 49111531, -56481530, -113499433, 60032738, 145859869, -4554946, -219807658, 31401094}, {2846160, 2869878, 960579, 106731, -1897440, -10941904, -15013494, -5905782, -26880400, 109656220, -138750300, 53918920, 227409353, 80924647, -146882549, -264229451}, {102820248, 303325954, 134526269, -95968676, -68546832, 4011010, -143079067, -13703144, 27164233, -31541746, -21265325, -48616230, 103691384, 54480933, -24213748, 146065443}, {-110621718, -61631348, -12378721, 220456665, 73747812, 25981308, -65329267, -219669894, 46516317, 230090620, -97535189, -99178285, 12718035, -17805249, 7913444, -1130492}, {145874499, 122134011, 36754589, -106832196, -97249666, -28409419, 54063609, -78084606, -46711131, 46689310, 125889733, -64847658, -77024869, -91814054, -70109793, 295228357}, {-4773600, -4813380, -1611090, -179010, 3182400, 18351840, 25180740, 9905220, 45084000, -183916200, 232713000, -90433200, 294605647, 186498706, -193804353, -114920000}, {156520293, -16868133, -34520782, 217996436, -104346862, 130312288, -52005172, 249210895, -147254741, -42274081, 106029452, 136830995, 34472315, -48261241, -200382638, -113980245}, {-3007440, -3032502, -1015011, -112779, 2004960, 11561936, 15864246, 6240438, 28403600, -115869980, 146612700, -56974280, -183729353, -142076647, -158686902, -405149451}, {-70689390, 178282620, 100922875, 122129692, 47126260, -60986685, 123325444, -102880604, 334873899, 104853891, -47747473, -8178751, 178400418, 149537156, 44455081, -148957062}, {72348411, -151653665, -87884901, -9764989, -48232274, 187707918, 42615982, -125166844, -17794313, -124122528, -282690889, 205982041, -128596429, -19613870, 230549354, -32935622}, {130461219, 106592287, 31552607, -107410194, -86974146, -301902038, 135368661, -46102050, -233889382, -214401999, -120957220, -24095567, 104427719, 53450064, -23755584, 145999991}, {-167315379, -143753565, -43990886, 106028163, 111543586, 110837691, 59037033, 122574432, 249208331, 125485583, -78891186, -341337902, -105536249, -51898122, 23065832, -145901455}, {-102823164, -203504459, 414507141, 46056349, 68548776, 195649071, -156378857, 113533630, -27136693, 31429399, 21407480, 48560988, -102901634, -55586583, 24705148, -146135643}, {-9214030, -92477843, 121670809, 124435018, -326605431, -186409252, -200957080, -230441976, -134810684, 33210037, -49938214, 47277399, -166518483, -166171865, -37061877, 147900890}, {-199780957, 39796587, -279552998, 79854595, -199560813, -52731900, 80556304, 239852724, -331500635, -71638619, 6439212, -69051927, 35773400, -50082760, -199573074, -114095897}}
dnm 1D:
-81*(-1/3 + i)*i*(1/3 + i)
tmp=seq[i-1];num+=1440*tmp;
tmp*=i-2;num+=8304*tmp;
tmp*=i-2;num+=11394*tmp;
tmp*=i-2;num+=4482*tmp;
tmp=seq[i-2];num+=20400*tmp;
tmp*=i-1;num+=-83220*tmp;
tmp*=i-1;num+=105300*tmp;
tmp*=i-1;num+=-40920*tmp;
tmp=seq[i-3];num+=585000*tmp;
tmp*=i-0;num+=-819000*tmp;
tmp*=i-0;num+=364000*tmp;
tmp*=i-0;num+=-52000*tmp;
tmp=1;dnm+=-2160*tmp;
tmp*=i-3;dnm+=-2178*tmp;
tmp*=i-3;dnm+=-729*tmp;
tmp*=i-3;dnm+=-81*tmp;
222222
675337738
*/