Problem:
H: Permutation Score
問題文
松崎くんは順列が大好きです。今日は順列によって定まる「サイクル森」について考えることにしました。
$1$ から $k$ までの順列 $p = (p_1, \ldots, p_k)$ に対し「$p$ によって定まるサイクル森」とは、以下の2条件を満たすグラフ $G(p)$ を指します:
$G(p)$ は $k$ 頂点からなり、それぞれの頂点には $1$ から $k$ の番号が付けられている。
$i=1, \ldots, k$ について、頂点 $i$ と頂点 $p_i$ の間に辺が張られている（自己ループおよび多重辺を許し、必ず $k$ 本の辺を張る）。逆に、$G(p)$にはそれら $k$ 本以外の辺は存在しない。
順列 $p$ のスコア $f(p)$ を、「$G(p)$ の連結成分の大きさの総積」として定義します。例えば $p = (2, 1, 4, 3)$ ならば $f(p) = 4$、$p = (2, 3, 1, 4)$ ならば $f(p) = 3$です。
正整数 $N$ が与えられます。長さ $N$ の順列は $N!$ 通り考えられますが、これら全ての順列のスコアの分散はいくつになるか求めてください。
全ての順列のスコアの分散とは以下のように定義されます:
まず、$P$ を $N!$ 個の順列全てからなる集合とします。
スコアの平均を$a = \frac{1}{N!} \sum_{p \in P} f(p)$ とおきます。
この時、全ての順列のスコアの分散は $\frac{1}{N!} \sum_{p \in P} (f(p) - a)^2$ として定義されます。
入力の制約下において、求める値は以下の条件を満たす有理数 $\frac{q}{p}$ として表せることが証明できます:
$p$ と $q$ は互いに素な非負整数である。
$p$ は $10^9+7$ の倍数にならず、$p \cdot r \equiv q (\bmod 10^9+7)$ なる整数 $r$が存在する。
分散の値の代わりに $r \bmod 10^9+7$を出力してください。
制約
入力は全て整数
$1 \leq N \leq 10^5$
入力
入力は以下の形式で標準入力から与えられます。
$N$
出力
問題文中で指定された値 $r \bmod 10^9+7$ を1行に出力してください。
入出力例
入力例1
1
出力例1
0
長さ1の順列は1つのみであり、分散は0になります。
入力例2
3
出力例2
472222226
分散の値は $\frac{17}{36}$ですが、問題文中の指示に従って $17 \times 27777778 \bmod 10^9+7 = 472222226$ を出力してください。
入力例3
10
出力例3
309669455

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;
template<class A, class B>
bool chmax(A &a, B b){ return a < b && (a = b, true); }
template<class A, class B>
bool chmin(A &a, B b){ return b < a && (a = b, true); }
template <int MD, int g = 3>
struct ModInt {
  using M = ModInt;
  const static inline M G = g;
  unsigned int v;
  ModInt() : v(0) {}
  ModInt(ll w) : v(w % MD + MD) {
    if(v >= MD) v -= MD;
  }
  static M raw(unsigned int v) {
    M res;
    res.v = (v < MD) ? v : v - MD;
    return res;
  }
  explicit operator bool() const {
    return v != 0;
  }
  M operator-() const {
    return M() - *this;
  }
  M operator+(M r) const {
    return raw(v + r.v);
  }
  M operator-(M r) const {
    return raw(v + MD - r.v);
  }
  M operator*(M r) const {
    return raw(ll(v) * r.v % MD);
  }
  M operator/(M r) const {
    return *this * r.inv();
  }
  M& operator+=(M r) {
    return *this = *this + r;
  }
  M& operator-=(M r) {
    return *this = *this - r;
  }
  M& operator*=(M r) {
    return *this = *this * r;
  }
  M& operator/=(M r) {
    return *this = *this / r;
  }
  bool operator==(M r) const {
    return v == r.v;
  }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const {
    return pow(MD - 2);
  }
  friend ostream& operator<<(ostream& os, M r) {
    return os << r.v;
  }
};

template <class Mint>
void nft(bool type, V<Mint>& a) {
  int n = int(a.size()), s = 0;
  while ((1 << s) < n) s++;
  assert(1 << s == n);
  static V<Mint> ep;
  while (int(ep.size()) <= s) {
    ep.push_back(Mint::G.pow(Mint(-1).v >> ep.size()));
  }
  V<Mint> b(n);
  REP(i, s) {
    int w = 1 << (s - i - 1);
    Mint now = 1;
    for (int y = 0; y < n / 2; y += w) {
      REP(x, w) {
        auto l = a[y << 1 | x];
        auto r = now * a[y << 1 | x | w];
        b[y | x] = l + r;
        b[y | x | n >> 1] = l - r;
      }
      now *= ep[i + 1];
    }
    swap(a, b);
  }
  if(type) reverse(a.begin() + 1, a.end());
}
template <class Mint>
V<Mint> multiply(V<Mint> a, V<Mint> b) {
  int n = int(a.size()), m = int(b.size());
  if (!n || !m) return {};
  if (min(n, m) <= 8) {
    V<Mint> ans(n + m - 1);
    REP(i, n) REP(j, m) ans[i + j] += a[i] * b[j];
    return ans;
  }
  int z = 1;
  while (z < n + m - 1) z *= 2;
  a.resize(z);
  b.resize(z);
  nft(0, a);
  nft(0, b);
  REP(i, z) a[i] *= b[i];
  nft(1, a);
  a.resize(n + m - 1);
  Mint iz = Mint(z).inv();
  for(auto& x : a) x *= iz;
  return a;
}

struct EG { ll g, x, y; };
EG ext_gcd(ll a, ll b) {
  if (b == 0) {
    if (a >= 0) return EG{a, 1, 0};
    return EG{-a, -1, 0};
  }
  auto e = ext_gcd(b, a % b);
  return EG{e.g, e.y, e.x - a / b * e.y};
}
ll inv_mod(ll x, ll md) {
  return (ext_gcd(x, md).x % md + md) % md;
}

template<class E>
struct garner {
  int n;
  vector<ll> m, h;
  garner(vector<ll> md) : n(md.size()), m(md), h(n, 1) {
    REP(i,n) REP(j,i) h[i] = h[i] * m[j] % m[i];
    REP(i,n) h[i] = inv_mod(h[i], m[i]);
  }
  E operator()(const vector<ll>& r){
    vector<ll> a(n, 1);
    vector<ll> b(n, 0);
    E resa = 1, res = 0;
    REP(k, n) {
      ll t = (r[k] - b[k]) * h[k] % m[k];
      if (t < 0) t += m[k];
      for (int i = k + 1; i < n; ++i) {
        b[i] = (b[i] + t * a[i]) % m[i];
        a[i] = a[i] * m[k] % m[i];
      }
      res = res + E(t) * resa;
      resa = resa * E(m[k]);
    }
    return res;
  }
};

template <int MOD, int g>
V<int> multiply_mod(const V<int>& a, const V<int>& b) {
  V<ModInt<MOD, g>> c, d;
  for(auto x : a) c.push_back(x);
  for(auto x : b) d.push_back(x);
  c = multiply(c, d);
  V<int> res;
  for(auto x : c) res.push_back(x.v);
  return res;
}
template<class mint>
V<mint> multiply_garner(V<mint> a, V<mint> b){
    constexpr int m[] = {
        167772161, 469762049, 754974721 };
    auto g = garner<mint>(V<ll>(m, m+3));
    V<int> c(a.size()), d(b.size());
    REP(i,a.size()) c[i] = a[i].v;
    REP(i,b.size()) d[i] = b[i].v;
    V<V<int>> res(3);
    res[0] = multiply_mod<m[0], 3>(c, d);
    res[1] = multiply_mod<m[1], 3>(c, d);
    res[2] = multiply_mod<m[2], 11>(c, d);
    V<ll> r(3);
    V<mint> ans(res[0].size());
    REP(i,res[0].size()){
        REP(j,3) r[j] = res[j][i];
        ans[i] = g(r);
    }
    return ans;
}

template <class D>
struct Poly {
  using P = const Poly&;
  V<D> v;
  Poly(V<D> v = {}) : v(move(v)) {}
  void shrink() {
    while (v.size() && !v.back()) v.pop_back();
  }
  int size() const {
    return v.size();
  }
  D freq(int p) const {
    return 0 <= p && p < size() ? v[p] : D(0);
  }
  Poly operator+(P r) const {
    V<D> res(max(size(), r.size()));
    REP(i, res.size()) res[i] = freq(i) + r.freq(i);
    return res;
  }
  Poly operator-(P r) const {
    V<D> res(max(size(), r.size()));
    REP(i, res.size()) res[i] = freq(i) - r.freq(i);
    return res;
  }
  Poly operator*(P r) const {
    return {multiply_garner(v, r.v)};
  }
  Poly operator*(D r) const {
    V<D> res = v;
    REP(i, size()) res[i] *= r;
    return res;
  }
  Poly operator/(D r) const {
    return *this * r.inv();
  }
  Poly operator/(Poly r) const {
    if (size() < r.size()) return {{}};
    r.shrink();
    int n = size() - r.size() + 1;
    return (rev().pre(n) * r.rev().inv(n)).rev(n);  // 変更
  }
  Poly operator%(P r) const {
    auto x = *this - *this / r * r;
    x.shrink();
    return x;
  }
  Poly& operator+=(P r) {
    return *this = *this + r;
  }
  Poly& operator-=(P r) {
    return *this = *this - r;
  }
  Poly& operator*=(P r) {
    return *this = *this * r;
  }
  Poly& operator*=(D r) {
    return *this = *this * r;
  }
  Poly& operator/=(P r) {
    return *this = *this / r;
  }
  Poly& operator/=(D r) {
    return *this = *this / r;
  }
  Poly& operator%=(P r) {
    return *this = *this % r;
  }
  Poly clip(int l = 0, int r = -(1 << 30)) const {
    if(r == -(1 << 30)) r = size();
    if(r < l) r = l;
    V<D> res(r - l);
    REP(i, r - l) res[i] = freq(i + l);
    return res;
  }
  Poly pre(int le) const {
    return clip(0, le);
  }
  Poly rev(int n = -1) const {
    auto res = v;
    if (n != -1) res.resize(n);
    reverse(res.begin(), res.end());
    return res;
  }
  Poly diff() const {
    auto res = clip(1).v;
    REP(i,res.size()) res[i] *= i + 1;
    return res;
  }
  Poly inte() const {
    auto res = clip(-1).v;
    REP(i,res.size()) if(i) res[i] /= i;
    return res;
  }
  // f * f.inv() = 1 + g(x)x^m
  Poly inv(int m) const {
    Poly f = Poly({D(1) / freq(0)});
    for (int i = 1; i < m; i *= 2)
      f = (f * D(2) - f * f * pre(2 * i)).pre(2 * i);
    return f.pre(m);
  }
  Poly exp(int n) const {
    assert(freq(0) == 0);
    Poly f({1}), g({1}), q;
    for (int i = 2; i < n * 2; i *= 2) {
      g = (g * 2 - f * g * g).pre(i / 2);
      q = pre(i / 2).diff();
      q += (g * (f.diff() - f * q)).pre(i - 1);
      f += (f * (pre(i) - q.inte())).pre(i);
    }
    return f.pre(n);
  }
  Poly log(int n) const {
    assert(freq(0) == 1);
    auto f = pre(n);
    return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
  }
  friend ostream& operator<<(ostream& os, P p) {
    if (p.size() == 0) return os << "0";
    for (auto i = 0; i < p.size(); i++) {
      if (p.v[i]) {
        os << p.v[i] << "x^" << i;
        if (i != p.size() - 1) os << "+";
      }
    }
    return os;
  }
};

using Mint = ModInt<1000000007>;

void testcase(){
    int N; cin >> N;
    V<Mint> fact(N+1,1);
    for(int i=1; i<=N; i++) fact[i] = fact[i-1] * i;
    V<Mint> ifact(N+1, fact[N].inv());
    for(int i=N; i>=1; i--) ifact[i-1] = ifact[i] * i;
    
    V<Mint> G(N+1);
    for(int i=1; i<=N; i++) G[i] = 1;
    V<Mint> H(N+1);
    for(int i=1; i<=N; i++) H[i] = i;
    auto sc1 = Poly(G).exp(N+1).freq(N);
    auto sc2 = Poly(H).exp(N+1).freq(N);

    Mint ans = sc2 - sc1 * sc1;
    cout << ans.v << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 16092
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;
template<class A, class B>
bool chmax(A &a, B b){ return a < b && (a = b, true); }
template<class A, class B>
bool chmin(A &a, B b){ return b < a && (a = b, true); }
template <int MD, int g = 3>
struct ModInt {
  using M = ModInt;
  const static inline M G = g;
  unsigned int v;
  ModInt() : v(0) {}
  ModInt(ll w) : v(w % MD + MD) {
    if(v >= MD) v -= MD;
  }
  static M raw(unsigned int v) {
    M res;
    res.v = (v < MD) ? v : v - MD;
    return res;
  }
  explicit operator bool() const {
    return v != 0;
  }
  M operator-() const {
    return M() - *this;
  }
  M operator+(M r) const {
    return raw(v + r.v);
  }
  M operator-(M r) const {
    return raw(v + MD - r.v);
  }
  M operator*(M r) const {
    return raw(ll(v) * r.v % MD);
  }
  M operator/(M r) const {
    return *this * r.inv();
  }
  M& operator+=(M r) {
    return *this = *this + r;
  }
  M& operator-=(M r) {
    return *this = *this - r;
  }
  M& operator*=(M r) {
    return *this = *this * r;
  }
  M& operator/=(M r) {
    return *this = *this / r;
  }
  bool operator==(M r) const {
    return v == r.v;
  }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const {
    return pow(MD - 2);
  }
  friend ostream& operator<<(ostream& os, M r) {
    return os << r.v;
  }
};

template <class Mint>
void nft(bool type, V<Mint>& a) {
  int n = int(a.size()), s = 0;
  while ((1 << s) < n) s++;
  assert(1 << s == n);
  static V<Mint> ep;
  while (int(ep.size()) <= s) {
    ep.push_back(Mint::G.pow(Mint(-1).v >> ep.size()));
  }
  V<Mint> b(n);
  REP(i, s) {
    int w = 1 << (s - i - 1);
    Mint now = 1;
    for (int y = 0; y < n / 2; y += w) {
      REP(x, w) {
        auto l = a[y << 1 | x];
        auto r = now * a[y << 1 | x | w];
        b[y | x] = l + r;
        b[y | x | n >> 1] = l - r;
      }
      now *= ep[i + 1];
    }
    swap(a, b);
  }
  if(type) reverse(a.begin() + 1, a.end());
}
template <class Mint>
V<Mint> multiply(V<Mint> a, V<Mint> b) {
  int n = int(a.size()), m = int(b.size());
  if (!n || !m) return {};
  if (min(n, m) <= 8) {
    V<Mint> ans(n + m - 1);
    REP(i, n) REP(j, m) ans[i + j] += a[i] * b[j];
    return ans;
  }
  int z = 1;
  while (z < n + m - 1) z *= 2;
  a.resize(z);
  b.resize(z);
  nft(0, a);
  nft(0, b);
  REP(i, z) a[i] *= b[i];
  nft(1, a);
  a.resize(n + m - 1);
  Mint iz = Mint(z).inv();
  for(auto& x : a) x *= iz;
  return a;
}

struct EG { ll g, x, y; };
EG ext_gcd(ll a, ll b) {
  if (b == 0) {
    if (a >= 0) return EG{a, 1, 0};
    return EG{-a, -1, 0};
  }
  auto e = ext_gcd(b, a % b);
  return EG{e.g, e.y, e.x - a / b * e.y};
}
ll inv_mod(ll x, ll md) {
  return (ext_gcd(x, md).x % md + md) % md;
}

template<class E>
struct garner {
  int n;
  vector<ll> m, h;
  garner(vector<ll> md) : n(md.size()), m(md), h(n, 1) {
    REP(i,n) REP(j,i) h[i] = h[i] * m[j] % m[i];
    REP(i,n) h[i] = inv_mod(h[i], m[i]);
  }
  E operator()(const vector<ll>& r){
    vector<ll> a(n, 1);
    vector<ll> b(n, 0);
    E resa = 1, res = 0;
    REP(k, n) {
      ll t = (r[k] - b[k]) * h[k] % m[k];
      if (t < 0) t += m[k];
      for (int i = k + 1; i < n; ++i) {
        b[i] = (b[i] + t * a[i]) % m[i];
        a[i] = a[i] * m[k] % m[i];
      }
      res = res + E(t) * resa;
      resa = resa * E(m[k]);
    }
    return res;
  }
};

template <int MOD, int g>
V<int> multiply_mod(const V<int>& a, const V<int>& b) {
  V<ModInt<MOD, g>> c, d;
  for(auto x : a) c.push_back(x);
  for(auto x : b) d.push_back(x);
  c = multiply(c, d);
  V<int> res;
  for(auto x : c) res.push_back(x.v);
  return res;
}
template<class mint>
V<mint> multiply_garner(V<mint> a, V<mint> b){
    constexpr int m[] = {
        167772161, 469762049, 754974721 };
    auto g = garner<mint>(V<ll>(m, m+3));
    V<int> c(a.size()), d(b.size());
    REP(i,a.size()) c[i] = a[i].v;
    REP(i,b.size()) d[i] = b[i].v;
    V<V<int>> res(3);
    res[0] = multiply_mod<m[0], 3>(c, d);
    res[1] = multiply_mod<m[1], 3>(c, d);
    res[2] = multiply_mod<m[2], 11>(c, d);
    V<ll> r(3);
    V<mint> ans(res[0].size());
    REP(i,res[0].size()){
        REP(j,3) r[j] = res[j][i];
        ans[i] = g(r);
    }
    return ans;
}

template <class D>
struct Poly {
  using P = const Poly&;
  vector<D> v;
  Poly(const vector<D>& _v = {}) : v(_v) {}
  void shrink() {
    while (v.size() && !v.back()) v.pop_back();
  }
  int size() const {
    return int(v.size());
  }
  D freq(int p) const {
    if(p < 0) return D(0);
    return (p < size()) ? v[p] : D(0);
  }

  Poly operator+(P r) const {
    auto n = max(size(), r.size());
    vector<D> res(n);
    REP(i, n) res[i] = freq(i) + r.freq(i);
    return res;
  }
  Poly operator-(P r) const {
    int n = max(size(), r.size());
    vector<D> res(n);
    REP(i, n) res[i] = freq(i) - r.freq(i);
    return res;
  }
  Poly operator*(P r) const {
    return {multiply_garner(v, r.v)};
  }
  Poly operator*(D r) const {
    int n = size();
    vector<D> res(n);
    REP(i, n) res[i] = v[i] * r;
    return res;
  }
  Poly operator/(D r) const {
    return *this * r.inv();
  }
  Poly operator/(Poly r) const {
    if (size() < r.size()) return {{}};
    r.shrink();
    int n = size() - r.size() + 1;
    return (rev().pre(n) * r.rev().inv(n)).rev(n);  // 変更
  }
  Poly operator%(P r) const {
    auto x = *this - *this / r * r;
    x.shrink();
    return x;
  }
  Poly& operator+=(P r) {
    return *this = *this + r;
  }
  Poly& operator-=(P r) {
    return *this = *this - r;
  }
  Poly& operator*=(P r) {
    return *this = *this * r;
  }
  Poly& operator*=(D r) {
    return *this = *this * r;
  }
  Poly& operator/=(P r) {
    return *this = *this / r;
  }
  Poly& operator/=(D r) {
    return *this = *this / r;
  }
  Poly& operator%=(P r) {
    return *this = *this % r;
  }
  Poly clip(int l = 0, int r = -(1 << 30)) const {
    if(r == -(1 << 30)) r = size();
    if(r < l) r = l;
    vector<D> res(r - l);
    REP(i, r - l) res[i] = freq(i + l);
    return res;
  }
  Poly pre(int le) const {
    return clip(0, le);
  }
  Poly rev(int n = -1) const {
    auto res = v;
    if (n != -1) res.resize(n);
    reverse(res.begin(), res.end());
    return res;
  }
  Poly diff() const {
    auto res = clip(1).v;
    REP(i,res.size()) res[i] *= i + 1;
    return res;
  }
  Poly inte() const {
    auto res = clip(-1).v;
    REP(i,res.size()) if(i) res[i] /= i;
    return res;
  }
  // f * f.inv() = 1 + g(x)x^m
  Poly inv(int m) const {
    Poly res = Poly({D(1) / freq(0)});
    for (int i = 1; i < m; i *= 2) {
      res = (res * D(2) - res * res * pre(2 * i)).pre(2 * i);
    }
    return res.pre(m);
  }
  Poly exp(int n) const {
    assert(freq(0) == 0);
    Poly f({1}), g({1});
    for (int i = 1; i < n; i *= 2) {
      g = (g * 2 - f * g * g).pre(i);
      Poly q = diff().pre(i - 1);
      Poly w = (q + g * (f.diff() - f * q)).pre(2 * i - 1);
      f = (f + f * (*this - w.inte()).pre(2 * i)).pre(2 * i);
    }
    return f.pre(n);
  }
  Poly log(int n) const {
    assert(freq(0) == 1);
    auto f = pre(n);
    return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
  }
  friend ostream& operator<<(ostream& os, P p) {
    if (p.size() == 0) return os << "0";
    for (auto i = 0; i < p.size(); i++) {
      if (p.v[i]) {
        os << p.v[i] << "x^" << i;
        if (i != p.size() - 1) os << "+";
      }
    }
    return os;
  }
};

using Mint = ModInt<1000000007>;

void testcase(){
    int N; cin >> N;
    V<Mint> fact(N+1,1);
    for(int i=1; i<=N; i++) fact[i] = fact[i-1] * i;
    V<Mint> ifact(N+1, fact[N].inv());
    for(int i=N; i>=1; i--) ifact[i-1] = ifact[i] * i;
    
    V<Mint> G(N+1);
    for(int i=1; i<=N; i++) G[i] = 1;
    V<Mint> H(N+1);
    for(int i=1; i<=N; i++) H[i] = i;
    auto sc1 = Poly(G).exp(N+1).freq(N);
    auto sc2 = Poly(H).exp(N+1).freq(N);

    Mint ans = sc2 - sc1 * sc1;
    cout << ans.v << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 16652
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;
template<class A, class B>
bool chmax(A &a, B b){ return a < b && (a = b, true); }
template<class A, class B>
bool chmin(A &a, B b){ return b < a && (a = b, true); }
template <int MD, int g = 3>
struct ModInt {
  using M = ModInt;
  const static inline M G = g;
  unsigned int v;
  ModInt() : v(0) {}
  ModInt(ll w) : v(w % MD + MD) {
    if(v >= MD) v -= MD;
  }
  static M raw(unsigned int v) {
    M res;
    res.v = (v < MD) ? v : v - MD;
    return res;
  }
  explicit operator bool() const {
    return v != 0;
  }
  M operator-() const {
    return M() - *this;
  }
  M operator+(M r) const {
    return raw(v + r.v);
  }
  M operator-(M r) const {
    return raw(v + MD - r.v);
  }
  M operator*(M r) const {
    return raw(ll(v) * r.v % MD);
  }
  M operator/(M r) const {
    return *this * r.inv();
  }
  M& operator+=(M r) {
    return *this = *this + r;
  }
  M& operator-=(M r) {
    return *this = *this - r;
  }
  M& operator*=(M r) {
    return *this = *this * r;
  }
  M& operator/=(M r) {
    return *this = *this / r;
  }
  bool operator==(M r) const {
    return v == r.v;
  }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const {
    return pow(MD - 2);
  }
  friend ostream& operator<<(ostream& os, M r) {
    return os << r.v;
  }
};

template <class Mint>
void nft(bool type, V<Mint>& a) {
  int n = int(a.size()), s = 0;
  while ((1 << s) < n) s++;
  assert(1 << s == n);
  static V<Mint> ep;
  while (int(ep.size()) <= s) {
    ep.push_back(Mint::G.pow(Mint(-1).v >> ep.size()));
  }
  V<Mint> b(n);
  REP(i, s) {
    int w = 1 << (s - i - 1);
    Mint now = 1;
    for (int y = 0; y < n / 2; y += w) {
      REP(x, w) {
        auto l = a[y << 1 | x];
        auto r = now * a[y << 1 | x | w];
        b[y | x] = l + r;
        b[y | x | n >> 1] = l - r;
      }
      now *= ep[i + 1];
    }
    swap(a, b);
  }
  if(type) reverse(a.begin() + 1, a.end());
}
template <class Mint>
V<Mint> multiply(V<Mint> a, V<Mint> b) {
  int n = int(a.size()), m = int(b.size());
  if (!n || !m) return {};
  if (min(n, m) <= 8) {
    V<Mint> ans(n + m - 1);
    REP(i, n) REP(j, m) ans[i + j] += a[i] * b[j];
    return ans;
  }
  int z = 1;
  while (z < n + m - 1) z *= 2;
  a.resize(z);
  b.resize(z);
  nft(0, a);
  nft(0, b);
  REP(i, z) a[i] *= b[i];
  nft(1, a);
  a.resize(n + m - 1);
  Mint iz = Mint(z).inv();
  for(auto& x : a) x *= iz;
  return a;
}

struct EG { ll g, x, y; };
EG ext_gcd(ll a, ll b) {
  if (b == 0) {
    if (a >= 0) return EG{a, 1, 0};
    return EG{-a, -1, 0};
  }
  auto e = ext_gcd(b, a % b);
  return EG{e.g, e.y, e.x - a / b * e.y};
}
ll inv_mod(ll x, ll md) {
  return (ext_gcd(x, md).x % md + md) % md;
}

template<class E>
struct garner {
  int n;
  vector<ll> m, h;
  garner(vector<ll> md) : n(md.size()), m(md), h(n, 1) {
    REP(i,n) REP(j,i) h[i] = h[i] * m[j] % m[i];
    REP(i,n) h[i] = inv_mod(h[i], m[i]);
  }
  E operator()(const vector<ll>& r){
    vector<ll> a(n, 1);
    vector<ll> b(n, 0);
    E resa = 1, res = 0;
    REP(k, n) {
      ll t = (r[k] - b[k]) * h[k] % m[k];
      if (t < 0) t += m[k];
      for (int i = k + 1; i < n; ++i) {
        b[i] = (b[i] + t * a[i]) % m[i];
        a[i] = a[i] * m[k] % m[i];
      }
      res = res + E(t) * resa;
      resa = resa * E(m[k]);
    }
    return res;
  }
};

template <int MOD, int g>
V<int> multiply_mod(const V<int>& a, const V<int>& b) {
  V<ModInt<MOD, g>> c, d;
  for(auto x : a) c.push_back(x);
  for(auto x : b) d.push_back(x);
  c = multiply(c, d);
  V<int> res;
  for(auto x : c) res.push_back(x.v);
  return res;
}
template<class mint>
V<mint> multiply_garner(V<mint> a, V<mint> b){
    constexpr int m[] = {
        167772161, 469762049, 754974721 };
    auto g = garner<mint>(V<ll>(m, m+3));
    V<int> c(a.size()), d(b.size());
    REP(i,a.size()) c[i] = a[i].v;
    REP(i,b.size()) d[i] = b[i].v;
    V<V<int>> res(3);
    res[0] = multiply_mod<m[0], 3>(c, d);
    res[1] = multiply_mod<m[1], 3>(c, d);
    res[2] = multiply_mod<m[2], 11>(c, d);
    V<ll> r(3);
    V<mint> ans(res[0].size());
    REP(i,res[0].size()){
        REP(j,3) r[j] = res[j][i];
        ans[i] = g(r);
    }
    return ans;
}

template <class D>
struct Poly {
  using P = const Poly&;
  vector<D> v;
  Poly(const vector<D>& _v = {}) : v(_v) {}
  void shrink() {
    while (v.size() && !v.back()) v.pop_back();
  }
  int size() const {
    return int(v.size());
  }
  D freq(int p) const {
    if(p < 0) return D(0);
    return (p < size()) ? v[p] : D(0);
  }

  Poly operator+(P r) const {
    auto n = max(size(), r.size());
    vector<D> res(n);
    REP(i, n) res[i] = freq(i) + r.freq(i);
    return res;
  }
  Poly operator-(P r) const {
    int n = max(size(), r.size());
    vector<D> res(n);
    REP(i, n) res[i] = freq(i) - r.freq(i);
    return res;
  }
  Poly operator*(P r) const {
    return {multiply_garner(v, r.v)};
  }
  Poly operator*(D r) const {
    int n = size();
    vector<D> res(n);
    REP(i, n) res[i] = v[i] * r;
    return res;
  }
  Poly operator/(D r) const {
    return *this * r.inv();
  }
  Poly operator/(Poly r) const {
    if (size() < r.size()) return {{}};
    r.shrink();
    int n = size() - r.size() + 1;
    return (rev().pre(n) * r.rev().inv(n)).rev(n);  // 変更
  }
  Poly operator%(P r) const {
    auto x = *this - *this / r * r;
    x.shrink();
    return x;
  }
  Poly& operator+=(P r) {
    return *this = *this + r;
  }
  Poly& operator-=(P r) {
    return *this = *this - r;
  }
  Poly& operator*=(P r) {
    return *this = *this * r;
  }
  Poly& operator*=(D r) {
    return *this = *this * r;
  }
  Poly& operator/=(P r) {
    return *this = *this / r;
  }
  Poly& operator/=(D r) {
    return *this = *this / r;
  }
  Poly& operator%=(P r) {
    return *this = *this % r;
  }
  Poly clip(int l = 0, int r = -(1 << 30)) const {
    if(r == -(1 << 30)) r = size();
    if(r < l) r = l;
    vector<D> res(r - l);
    REP(i, r - l) res[i] = freq(i + l);
    return res;
  }
  Poly pre(int le) const {
    return clip(0, le);
  }
  Poly rev(int n = -1) const {
    auto res = v;
    if (n != -1) res.resize(n);
    reverse(res.begin(), res.end());
    return res;
  }
  Poly diff() const {
    auto res = clip(1).v;
    REP(i,res.size()) res[i] *= i + 1;
    return res;
  }
  Poly inte() const {
    auto res = clip(-1).v;
    REP(i,res.size()) if(i) res[i] /= i;
    return res;
  }
  // f * f.inv() = 1 + g(x)x^m
  Poly inv(int m) const {
    Poly res = Poly({D(1) / freq(0)});
    for (int i = 1; i < m; i *= 2) {
      res = (res * D(2) - res * res * pre(2 * i)).pre(2 * i);
    }
    return res.pre(m);
  }
  Poly exp(int n) const {
    assert(freq(0) == 0);
    Poly f({1}), g({1});
    for (int i = 1; i < n; i *= 2) {
      g = (g * 2 - f * g * g).pre(i);
      Poly q = diff().pre(i - 1);
      Poly w = (q + g * (f.diff() - f * q)).pre(2 * i - 1);
      f = (f + f * (*this - w.inte()).pre(2 * i)).pre(2 * i);
    }
    return f.pre(n);
  }
  Poly log(int n) const {
    assert(freq(0) == 1);
    auto f = pre(n);
    return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
  }
  friend ostream& operator<<(ostream& os, P p) {
    if (p.size() == 0) return os << "0";
    for (auto i = 0; i < p.size(); i++) {
      if (p.v[i]) {
        os << p.v[i] << "x^" << i;
        if (i != p.size() - 1) os << "+";
      }
    }
    return os;
  }
};

using Mint = ModInt<1000000007>;

void testcase(){
    int N; cin >> N;
    V<Mint> fact(N+1,1);
    for(int i=1; i<=N; i++) fact[i] = fact[i-1] * i;
    V<Mint> ifact(N+1, fact[N].inv());
    for(int i=N; i>=1; i--) ifact[i-1] = ifact[i] * i;
    
    V<Mint> G(N+1);
    for(int i=1; i<=N; i++) G[i] = 1;
    V<Mint> H(N+1);
    for(int i=1; i<=N; i++) H[i] = i;
    auto sc1 = Poly(G).exp(N+1).freq(N);
    auto sc2 = Poly(H).exp(N+1).freq(N);

    Mint ans = sc2 - sc1 * sc1;
    cout << ans.v << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 16360
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;
template<class A, class B>
bool chmax(A &a, B b){ return a < b && (a = b, true); }
template<class A, class B>
bool chmin(A &a, B b){ return b < a && (a = b, true); }
template <int MD, int g = 3>
struct ModInt {
  using M = ModInt;
  const static inline M G = g;
  unsigned int v;
  ModInt() : v(0) {}
  ModInt(ll w) : v(w % MD + MD) {
    if(v >= MD) v -= MD;
  }
  static M raw(unsigned int v) {
    M res;
    res.v = (v < MD) ? v : v - MD;
    return res;
  }
  explicit operator bool() const {
    return v != 0;
  }
  M operator-() const {
    return M() - *this;
  }
  M operator+(M r) const {
    return raw(v + r.v);
  }
  M operator-(M r) const {
    return raw(v + MD - r.v);
  }
  M operator*(M r) const {
    return raw(ll(v) * r.v % MD);
  }
  M operator/(M r) const {
    return *this * r.inv();
  }
  M& operator+=(M r) {
    return *this = *this + r;
  }
  M& operator-=(M r) {
    return *this = *this - r;
  }
  M& operator*=(M r) {
    return *this = *this * r;
  }
  M& operator/=(M r) {
    return *this = *this / r;
  }
  bool operator==(M r) const {
    return v == r.v;
  }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const {
    return pow(MD - 2);
  }
  friend ostream& operator<<(ostream& os, M r) {
    return os << r.v;
  }
};

template <class Mint>
void nft(bool type, V<Mint>& a) {
  int n = int(a.size()), s = 0;
  while ((1 << s) < n) s++;
  assert(1 << s == n);
  static V<Mint> ep;
  while (int(ep.size()) <= s) {
    ep.push_back(Mint::G.pow(Mint(-1).v >> ep.size()));
  }
  V<Mint> b(n);
  REP(i, s) {
    int w = 1 << (s - i - 1);
    Mint now = 1;
    for (int y = 0; y < n / 2; y += w) {
      REP(x, w) {
        auto l = a[y << 1 | x];
        auto r = now * a[y << 1 | x | w];
        b[y | x] = l + r;
        b[y | x | n >> 1] = l - r;
      }
      now *= ep[i + 1];
    }
    swap(a, b);
  }
  if(type) reverse(a.begin() + 1, a.end());
}
template <class Mint>
V<Mint> multiply(V<Mint> a, V<Mint> b) {
  int n = int(a.size()), m = int(b.size());
  if (!n || !m) return {};
  if (min(n, m) <= 8) {
    V<Mint> ans(n + m - 1);
    REP(i, n) REP(j, m) ans[i + j] += a[i] * b[j];
    return ans;
  }
  int z = 1;
  while (z < n + m - 1) z *= 2;
  a.resize(z);
  b.resize(z);
  nft(0, a);
  nft(0, b);
  REP(i, z) a[i] *= b[i];
  nft(1, a);
  a.resize(n + m - 1);
  Mint iz = Mint(z).inv();
  for(auto& x : a) x *= iz;
  return a;
}

struct EG { ll g, x, y; };
EG ext_gcd(ll a, ll b) {
  if (b == 0) {
    if (a >= 0) return EG{a, 1, 0};
    return EG{-a, -1, 0};
  }
  auto e = ext_gcd(b, a % b);
  return EG{e.g, e.y, e.x - a / b * e.y};
}
ll inv_mod(ll x, ll md) {
  return (ext_gcd(x, md).x % md + md) % md;
}

template<class E>
struct garner {
  int n;
  vector<ll> m, h;
  garner(vector<ll> md) : n(md.size()), m(md), h(n, 1) {
    REP(i,n) REP(j,i) h[i] = h[i] * m[j] % m[i];
    REP(i,n) h[i] = inv_mod(h[i], m[i]);
  }
  E operator()(const vector<ll>& r){
    vector<ll> a(n, 1);
    vector<ll> b(n, 0);
    E resa = 1, res = 0;
    REP(k, n) {
      ll t = (r[k] - b[k]) * h[k] % m[k];
      if (t < 0) t += m[k];
      for (int i = k + 1; i < n; ++i) {
        b[i] = (b[i] + t * a[i]) % m[i];
        a[i] = a[i] * m[k] % m[i];
      }
      res = res + E(t) * resa;
      resa = resa * E(m[k]);
    }
    return res;
  }
};

template <int MOD, int g>
V<int> multiply_mod(const V<int>& a, const V<int>& b) {
  V<ModInt<MOD, g>> c, d;
  for(auto x : a) c.push_back(x);
  for(auto x : b) d.push_back(x);
  c = multiply(c, d);
  V<int> res;
  for(auto x : c) res.push_back(x.v);
  return res;
}
template<class mint>
V<mint> multiply_garner(V<mint> a, V<mint> b){
    constexpr int m[] = {
        167772161, 469762049, 754974721 };
    auto g = garner<mint>(V<ll>(m, m+3));
    V<int> c(a.size()), d(b.size());
    REP(i,a.size()) c[i] = a[i].v;
    REP(i,b.size()) d[i] = b[i].v;
    V<V<int>> res(3);
    res[0] = multiply_mod<m[0], 3>(c, d);
    res[1] = multiply_mod<m[1], 3>(c, d);
    res[2] = multiply_mod<m[2], 11>(c, d);
    V<ll> r(3);
    V<mint> ans(res[0].size());
    REP(i,res[0].size()){
        REP(j,3) r[j] = res[j][i];
        ans[i] = g(r);
    }
    return ans;
}

template <class D>
struct Poly {
  vector<D> v;
  Poly(const vector<D>& _v = {}) : v(_v) {}
  void shrink() {
    while (v.size() && !v.back()) v.pop_back();
  }
  int size() const {
    return int(v.size());
  }
  D freq(int p) const {
    return (p < size()) ? v[p] : D(0);
  }

  Poly operator+(const Poly& r) const {
    auto n = max(size(), r.size());
    vector<D> res(n);
    for (int i = 0; i < n; i++) res[i] = freq(i) + r.freq(i);
    return res;
  }
  Poly operator-(const Poly& r) const {
    int n = max(size(), r.size());
    vector<D> res(n);
    for (int i = 0; i < n; i++) res[i] = freq(i) - r.freq(i);
    return res;
  }
  Poly operator*(const Poly& r) const {
    return {multiply_garner(v, r.v)};
  }
  Poly operator*(const D& r) const {
    int n = size();
    vector<D> res(n);
    for (int i = 0; i < n; i++) res[i] = v[i] * r;
    return res;
  }
  Poly operator/(const D& r) const {
    return *this * r.inv();
  }
  Poly operator/(Poly r) const {
    if (size() < r.size()) return {{}};
    r.shrink();
    int n = size() - r.size() + 1;
    return (rev().pre(n) * r.rev().inv(n)).rev(n);  // 変更
  }
  Poly operator%(const Poly& r) const {
    auto x = *this - *this / r * r;
    x.shrink();
    return x;
  }
  Poly operator<<(int s) const {
    vector<D> res(size() + s);
    for (int i = 0; i < size(); i++) res[i + s] = v[i];
    return res;
  }
  Poly operator>>(int s) const {
    if (size() <= s) return Poly();
    vector<D> res(size() - s);
    for (int i = 0; i < size() - s; i++) res[i] = v[i + s];
    return res;
  }
  Poly& operator+=(const Poly& r) {
    return *this = *this + r;
  }
  Poly& operator-=(const Poly& r) {
    return *this = *this - r;
  }
  Poly& operator*=(const Poly& r) {
    return *this = *this * r;
  }
  Poly& operator*=(const D& r) {
    return *this = *this * r;
  }
  Poly& operator/=(const Poly& r) {
    return *this = *this / r;
  }
  Poly& operator/=(const D& r) {
    return *this = *this / r;
  }
  Poly& operator%=(const Poly& r) {
    return *this = *this % r;
  }
  Poly& operator<<=(const size_t& n) {
    return *this = *this << n;
  }
  Poly& operator>>=(const size_t& n) {
    return *this = *this >> n;
  }

  Poly pre(int le) const {
    return {{v.begin(), v.begin() + min(size(), le)}};
  }
  Poly rev(int n = -1) const {
    vector<D> res = v;
    if (n != -1) res.resize(n);
    reverse(res.begin(), res.end());
    return res;
  }
  Poly diff() const {
    vector<D> res(max(0, size() - 1));
    for (int i = 1; i < size(); i++) res[i - 1] = freq(i) * i;
    return res;
  }
  Poly inte() const {
    vector<D> res(size() + 1);
    for (int i = 0; i < size(); i++)
      res[i + 1] = freq(i) / (i + 1);
    return res;
  }
  // f * f.inv() = 1 + g(x)x^m
  Poly inv(int m) const {
    Poly res = Poly({D(1) / freq(0)});
    for (int i = 1; i < m; i *= 2) {
      res = (res * D(2) - res * res * pre(2 * i)).pre(2 * i);
    }
    return res.pre(m);
  }
  Poly exp(int n) const {
    assert(freq(0) == 0);
    Poly f({1}), g({1});
    for (int i = 1; i < n; i *= 2) {
      g = (g * 2 - f * g * g).pre(i);
      Poly q = diff().pre(i - 1);
      Poly w = (q + g * (f.diff() - f * q)).pre(2 * i - 1);
      f = (f + f * (*this - w.inte()).pre(2 * i)).pre(2 * i);
    }
    return f.pre(n);
  }
  Poly log(int n) const {
    assert(freq(0) == 1);
    auto f = pre(n);
    return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
  }
};

using Mint = ModInt<1000000007>;

void testcase(){
    int N; cin >> N;
    V<Mint> fact(N+1,1);
    for(int i=1; i<=N; i++) fact[i] = fact[i-1] * i;
    V<Mint> ifact(N+1, fact[N].inv());
    for(int i=N; i>=1; i--) ifact[i-1] = ifact[i] * i;
    
    V<Mint> G(N+1);
    for(int i=1; i<=N; i++) G[i] = 1;
    V<Mint> H(N+1);
    for(int i=1; i<=N; i++) H[i] = i;
    auto sc1 = Poly(G).exp(N+1).freq(N);
    auto sc2 = Poly(H).exp(N+1).freq(N);

    Mint ans = sc2 - sc1 * sc1;
    cout << ans.v << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 16772
final_rank: 8

code_snippet: #ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; // -2^63 ～ 2^63 = 9 * 10^18（int は -2^31 ～ 2^31 = 2 * 10^9）
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 vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;
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);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍（下，右，上，左）
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004004004004004LL;
double EPS = 1e-12;

// 入出力高速化
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 < (1 << int(d)); ++set) // d ビット全探索（昇順）
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て（昇順）
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了

// 汎用関数の定義
template <class T> inline ll pow(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, 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; }

// 手元環境（Visual Studio）
#ifdef _MSC_VER
#include "local.hpp"
// 提出用（gcc）
#else
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) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
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 gcd __gcd
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_list2D(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif

#endif // 折りたたみ用


//#if __has_include(<atcoder/all>)
//#include <atcoder/all>
//using namespace atcoder;
//
//using mint = modint1000000007;
////using mint = modint998244353;
////using mint = modint; // mint::set_mod(m);
//
//istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
//ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;
//#endif


//【有限体 F_p 上の計算（32 bit）】
template <ll MOD> struct static_modint {
	ll v;

	// コンストラクタ
	static_modint() : v(0) {};
	static_modint(const static_modint& a) = default;
	static_modint(int a) : v(safe_mod(a)) {};
	static_modint(ll a) : v(safe_mod(a)) {};

	// 代入
	static_modint& operator=(const static_modint& a) { v = a.v; return *this; }
	static_modint& operator=(int a) { v = safe_mod(a); return *this; }
	static_modint& operator=(ll a) { v = safe_mod(a); return *this; }

	// 入出力
	friend istream& operator>> (istream& is, static_modint& x) { is >> x.v; x.v = safe_mod(x.v); return is; }
	friend ostream& operator<< (ostream& os, const static_modint& x) { os << x.v; return os; }

	// 非負 mod
	template <class T> static ll safe_mod(T a) { return (((ll)a % MOD) + MOD) % MOD; }

	// 比較
	bool operator==(const static_modint& b) const { return v == b.v; }
	bool operator==(int b) const { return v == safe_mod(b); }
	bool operator==(ll b) const { return v == safe_mod(b); }
	friend bool operator==(int a, const static_modint& b) { return b == a; }
	friend bool operator==(ll a, const static_modint& b) { return b == a; }
	bool operator!=(const static_modint& b) const { return v != b.v; }
	bool operator!=(int b) const { return v != safe_mod(b); }
	bool operator!=(ll b) const { return v != safe_mod(b); }
	friend bool operator!=(int a, const static_modint& b) { return b != a; }
	friend bool operator!=(ll a, const static_modint& b) { return b != a; }

	// 単項演算
	static_modint operator-() const { static_modint a = *this; return a *= -1; }
	static_modint& operator++() { v = safe_mod(v + 1); return *this; }
	static_modint operator++(int) { static_modint tmp = *this; ++(*this); return tmp; }
	static_modint& operator--() { v = safe_mod(v - 1); return *this; }
	static_modint operator--(int) { static_modint tmp = *this; --(*this); return tmp; }

	// 二項演算
	static_modint& operator+=(const static_modint& b) { v = safe_mod(v + b.v); return *this; }
	static_modint& operator-=(const static_modint& b) { v = safe_mod(v - b.v); return *this; }
	static_modint& operator*=(const static_modint& b) { v = safe_mod(v * b.v); return *this; }
	static_modint& operator/=(const static_modint& b) { *this *= b.inv(); return *this; }
	static_modint operator+(const static_modint& b) const { static_modint a = *this; return a += b; }
	static_modint operator-(const static_modint& b) const { static_modint a = *this; return a -= b; }
	static_modint operator*(const static_modint& b) const { static_modint a = *this; return a *= b; }
	static_modint operator/(const static_modint& b) const { static_modint a = *this; return a /= b; }

	// int との演算
	static_modint& operator+=(int b) { v = safe_mod(v + b); return *this; }
	static_modint& operator-=(int b) { v = safe_mod(v - b); return *this; }
	static_modint& operator*=(int b) { v = safe_mod(v * b); return *this; }
	static_modint& operator/=(int b) { *this *= static_modint(b).inv(); return *this; }
	static_modint operator+(int b) const { static_modint a = *this; return a += b; }
	static_modint operator-(int b) const { static_modint a = *this; return a -= b; }
	static_modint operator*(int b) const { static_modint a = *this; return a *= b; }
	static_modint operator/(int b) const { static_modint a = *this; return a /= b; }
	friend static_modint operator+(int a, const static_modint& b) { return b + a; }
	friend static_modint operator-(int a, const static_modint& b) { return -(b - a); }
	friend static_modint operator*(int a, const static_modint& b) { return b * a; }
	friend static_modint operator/(int a, const static_modint& b) { return b.inv() * a; }

	// ll との演算
	static_modint& operator+=(ll b) { v = safe_mod(v + (b % MOD)); return *this; }
	static_modint& operator-=(ll b) { v = safe_mod(v - (b % MOD)); return *this; }
	static_modint& operator*=(ll b) { v = safe_mod(v * (b % MOD)); return *this; }
	static_modint& operator/=(ll b) { *this *= static_modint(b).inv(); return *this; }
	static_modint operator+(ll b) const { static_modint a = *this; return a += b; }
	static_modint operator-(ll b) const { static_modint a = *this; return a -= b; }
	static_modint operator*(ll b) const { static_modint a = *this; return a *= b; }
	static_modint operator/(ll b) const { static_modint a = *this; return a /= b; }
	friend static_modint operator+(ll a, const static_modint& b) { return b + a; }
	friend static_modint operator-(ll a, const static_modint& b) { return -(b - a); }
	friend static_modint operator*(ll a, const static_modint& b) { return b * a; }
	friend static_modint operator/(ll a, const static_modint& b) { return b.inv() * a; }

	// 累乗（0^0 := 1）
	static_modint pow(ll d) const {
		static_modint res(1), pow2 = *this;
		while (d > 0) {
			if (d & 1LL) res *= pow2;
			pow2 *= pow2;
			d /= 2;
		}
		return res;
	}

	// 逆元（0^(-1) は未定義）
	static_modint inv() const {
		Assert(v != 0);
		return pow(MOD - 2);
	}

	// 法の確認
	static int mod() { return int(MOD); }

	// 値の確認
	int val() const { return int(v); }
};
using mint = static_modint<1000000007>;
//using mint = static_modint<998244353>;
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	// 参考 : https://37zigen.com/exponential-generating-function/

	int n;
	cin >> n;

	vm dp(n + 10);
	dp[0] = 1;
	dp[1] = 1;
	repi(i, 0, n) dp[2 + i] = -((-3 - 2 * i) * dp[1 + i] + i * dp[i]) / (2 + i);

	vm dp2(n + 10);
	dp2[0] = 1;
	dp2[1] = 1;
	dp2[2] = mint(5) / 2;
	repi(i, 0, n) dp2[3 + i] = -((-7 - 3 * i) * dp2[2 + i] + (2 + 3 * i) * dp2[1 + i] - i * dp2[i]) / (3 + i);

	mint res = dp2[n] - dp[n] * dp[n];

	cout << res << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 4576
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif

namespace ttl{

using namespace std;
using f80=long double;
using i64=int64_t;
using u64=uint64_t;

template<int mod> struct mymodint{
  i64 vl;
  static constexpr i64 get_mod(){ return mod; }
  i64 val(){ return vl; }
  mymodint(i64 vl_=0):vl(vl_%mod){}
  mymodint operator-(){ return (vl==0)?0:mod-vl; }
  mymodint operator+(mymodint rhs){ return mymodint(*this)+=rhs; }
  mymodint operator-(mymodint rhs){ return mymodint(*this)-=rhs; }
  mymodint operator*(mymodint rhs){ return mymodint(*this)*=rhs; }
  mymodint operator/(mymodint rhs){ return mymodint(*this)/=rhs; }
  mymodint pow(i64 rhs){
    mymodint res=1,now=(*this);
    while(rhs){
      res*=((rhs&1)?now:1),now*=now,rhs>>=1;
    }
    return res;
  }
  mymodint& operator+=(mymodint rhs){
    vl+=rhs.vl,vl-=((vl>=mod)?mod:0);
    return (*this);
  }
  mymodint& operator-=(mymodint rhs){
    vl+=((vl<rhs.vl)?mod:0),vl-=rhs.vl;
    return (*this);
  }
  mymodint& operator*=(mymodint rhs){
    vl=vl*rhs.vl%mod;
    return (*this);
  }
  mymodint& operator/=(mymodint rhs){ return (*this)*=rhs.pow(mod-2); }
  bool operator==(mymodint rhs){ return vl==rhs.vl; }
  bool operator!=(mymodint rhs){ return vl!=rhs.vl; }
};

template<u64 mod> using modint=
#if __has_include(<atcoder/all>)
  atcoder::static_modint<mod>;
#else
  mymodint<mod>;
#endif

template<int mod> std::ostream& operator<<(std::ostream& os,modint<mod> x){
  return os<<(x.val());
}
template<int mod> std::istream& operator>>(std::istream& is,modint<mod>& x){
  i64 t;
  is>>t,x=t;
  return is;
}

template<class T> void scn_(T& a){ cin>>a; }
template<class T,class U> void scn_(pair<T,U>& a){ scn_(a.first),scn_(a.second); }
template<class T> void scn_(vector<T>& a){
  for(auto& v:a){
    scn_(v);
  }
}
template<class T> void scn_(vector<vector<T>>& a){
  for(auto& v:a){
    for(auto& u:v){
      cin>>u;
    }
  }
}
void scn(){}
template<class T,class... Args> void scn(T& n,Args&... args){ scn_(n),scn(args...); }
template<class T> void prt_(T a){ cout<<a; }
template<class T,class U> void prt_(pair<T,U> a){ cout<<"{"<<a.first<<", "<<a.second<<"}"; }
void prt_(f80 a){ printf("%.15Lf",a); }
template<class T> void prt(vector<T> a){
  for(size_t i=0;i<a.size();++i){
    prt_(a[i]);
    cout<<" \n"[i==a.size()-1];
  }
}
template<class T> void prt(vector<vector<T>> a){
  for(auto& v:a){
    for(size_t i=0;i<v.size();++i){
      cout<<v[i]<<" \n"[i==v.size()-1];
    }
  }
}
template<class T> void prt(T a){ prt_(a),cout<<"\n"; }
template<class T,class... Args> void prt(T a,Args... args){ prt_(a),cout<<" ",prt(args...); }

template<class Head,class... Tail> struct multi_dim_vector{ using type=vector<typename multi_dim_vector<Tail...>::type>; };
template<class T> struct multi_dim_vector<T>{ using type=T; };
template<class T,class Arg> vector<T> mvec(int n,Arg&& arg){ return vector<T>(n,arg); }
template<class T,class... Args> class multi_dim_vector<Args...,T>::type mvec(int n,Args&&... args){
  return typename multi_dim_vector<Args...,T>::type(n,mvec<T>(args...));
}

template<class T> void rev(T& a){ reverse(a.begin(),a.end());}
template<class T> void srt(T& a){ sort(a.begin(),a.end()); }
template<class T> void rsrt(T& a){ sort(a.rbegin(),a.rend()); }
template<class T> T revd(T a){
  reverse(a.begin(),a.end());
  return a;
}
template<class T> T srtd(T a){
  sort(a.begin(),a.end());
  return a;
}
template<class T> T rsrtd(T a){
  sort(a.rbegin(),a.rend());
  return a;
}
template<class T> T summ(vector<T> a){ return accumulate(a.begin(),a.end(),T(0)); }
template<class T> T maxi(vector<T> a){ return *max_element(a.begin(),a.end()); }
template<class T> T mini(vector<T> a){ return *min_element(a.begin(),a.end()); }
template<class T> void chmx(T& a,T b){ a=max(a,b); }
template<class T> void chmn(T& a,T b){ a=min(a,b); }
i64 ppcnt(u64 k){ return __builtin_popcountll(k); }
template<class T> T powe(T a,i64 n){
  T res=1;
  while(n){
    if(n&1){ res*=a; }
    a*=a;
    n/=2;
  }
  return res;
}
i64 powe(i64 a,i64 n,i64 m){
  i64 res=1;
  while(n){
    if(n&1){ res=res*a%m; }
    a=a*a%m;
    n/=2;
  }
  return res;
}
template<class T> void zip(vector<T>& a){
  auto b=srtd(a);
  b.erase(unique(b.begin(),b.end()),b.end());
  map<T,int> mp;
  for(int i=0;i<b.size();++i){
    mp[b[i]]=i;
  }
  for(auto& v:a){
    v=mp[v];
  }
}
i64 sqrf(i64 n){
  i64 ok=0,ng=1e9+5;
  while(std::abs(ok-ng)>1){
    i64 mid=(ok+ng)/2;
    (mid*mid<=n?ok:ng)=mid;
  }
  return ok;
}
i64 sqrc(i64 n){
  i64 ok=1e9+5,ng=0;
  while(std::abs(ok-ng)>1){
    i64 mid=(ok+ng)/2;
    (mid*mid>=n?ok:ng)=mid;
  }
  return ok;
}
i64 dvf(i64 a,i64 b){
  if(b<0){ a*=-1,b*=-1; }
  if(a<0){ return -(-a+b-1)/b; }
  return a/b;
}
i64 dvc(i64 a,i64 b){
  if(b<0){ a*=-1,b*=-1; }
  if(a<0){ return -(-a)/b; }
  return (a+b-1)/b;
}
vector<int> bfs(vector<vector<int>> G,int v){
  int N=G.size();
  vector<int> dst(N,-1);
  queue<int> q;
  dst[v]=0;
  q.emplace(v);
  while(q.size()){
    int t=q.front();
    q.pop();
    for(auto u:G[t]){
      if(dst[u]==-1){
        dst[u]=dst[t]+1;
        q.emplace(u);
      }
    }
  }
  return dst;
}
vector<i64> dijkstra(vector<vector<pair<int,i64>>>& G,int s){
  int N=G.size();
  vector<i64> dst(N,1e18);
  dst[s]=0;
  priority_queue<pair<i64,int>,vector<pair<i64,int>>,greater<pair<i64,int>>> pq;
  pq.emplace(0,s);
  vector<int> f(N);
  while(pq.size()){
    auto [t,u]=pq.top();
    pq.pop();
    if(f[u]){ continue; }
    f[u]=1;
    for(auto [v,w]:G[u]){
      if(dst[v]>dst[u]+w){
        dst[v]=dst[u]+w;
        pq.emplace(dst[v],v);
      }
    }
  }
  return dst;
}
vector<pair<i64,i64>> pfct(i64 n){
  vector<pair<i64,i64>> res;
  for(i64 i=2;i*i<=n;++i){
    if(n%i!=0){ continue; }
    i64 ex=0;
    while(n%i==0){ ex++,n/=i; }
    res.emplace_back(i,ex);
  }
  if(n!=1){ res.emplace_back(n,1); }
  return res;
}
vector<i64> ediv(i64 n){
  vector<i64> res;
  for(i64 i=1;i*i<=n;++i){
    if(n%i!=0){ continue; }
    res.emplace_back(i);
    if(i*i!=n){ res.emplace_back(n/i); }
  }
  srt(res);
  return res;
}
template<class T> struct csm2d{
  int n,m;
  vector<vector<T>> a,c;
  csm2d(vector<vector<T>> a_):n(a_.size()),m(a_[0].size()),a(a_){
    auto b=mvec<T>(n,m+1,0);
    for(int i=0;i<n;++i){
      for(int j=0;j<m;++j){
        b[i][j+1]=b[i][j]+a[i][j];
      }
    }
    c=mvec<T>(n+1,m+1,0);
    for(int j=0;j<m+1;++j){
      for(int i=0;i<n;++i){
        c[i+1][j]=c[i][j]+b[i][j];
      }
    }
  }
  T operator()(int p,int q,int r,int s){
    return c[p][r]-c[p][s]-c[q][r]+c[q][s];
  }
};
template<class T> auto runlng(T a) -> vector<pair<typename decltype(a)::value_type,i64>>{
  int n=a.size();
  vector<pair<typename decltype(a)::value_type,i64>> res;
  typename decltype(a)::value_type now=a[0];
  i64 l=1;
  for(int i=1;i<n;++i){
    if(a[i-1]==a[i]){ l++; }
    else{
      res.emplace_back(now,l);
      now=a[i],l=1;
    }
  }
  res.emplace_back(now,l);
  return res;
}
template<class T> struct cmb{
  vector<T> fac,ifac;
  cmb(int mx=3000000):fac(mx+1,1),ifac(mx+1,1){
    for(int i=1;i<=mx;++i){ fac[i]=fac[i-1]*i; }
    ifac[mx]/=fac[mx];
    for(int i=mx;i>0;--i){ ifac[i-1]=ifac[i]*i; }
  }
  T operator()(i64 n,i64 k){
    if(n<0||k<0||n<k){ return 0; }
    return fac[n]*ifac[k]*ifac[n-k];
  }
  T f(i64 n){
    return n<0?T(0):fac[n];
  }
  T fi(i64 n){
    return n<0?T(0):ifac[n];
  }
};

}

using namespace ttl;

int main(){
  constexpr int mod=1000000007;
  using mint=modint<mod>;
  cmb<mint> Cb;
  int N;
  scn(N);
  mint c1=0,c2=0;
  for(int i=1;i<=N;++i){
    c1+=Cb(N-i+i-1,i-1)*Cb.fi(i);
    c2+=Cb(N-i+2*i-1,2*i-1)*Cb.fi(i);
  }
  prt(c2-c1*c1);
}
accuracy: 1
time_ms: 20
memory_kb: 49884
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,b) for (ll i=a;i<b;i++)


namespace po167{
struct combination{
	int upper;
	long long MOD;
	std::vector<long long> fact;
	std::vector<long long> rev;
	std::vector<long long> fact_rev;
	combination(int max,long long _mod):upper(max),MOD(_mod),fact(max+1),rev(max+1),fact_rev(max+1){
		for(long long i=0;i<=max;i++){
			if(i<2){
				fact[i]=1;
				fact_rev[i]=1;
				rev[i]=1;
				continue;
			}
			fact[i]=(fact[i-1]*i)%_mod;
			rev[i]=_mod-((_mod/i)*rev[_mod%i])%_mod;
			fact_rev[i]=(fact_rev[i-1]*rev[i])%_mod;
		}
	}
	void upper_change(int n_upper){
		assert(n_upper<MOD);
		while(upper<n_upper){
			upper++;
			fact.push_back(fact[upper-1]*upper%MOD);
			rev.push_back(MOD-((MOD/upper)*rev[MOD%upper])%MOD);
			fact_rev.push_back((fact_rev[upper-1]*rev[upper])%MOD);
		}
	}
	long long Comb(int x,int y){
		upper_change(x);
		if (x<y||y<0||x<0) return 0;
		return (((fact_rev[y]*fact_rev[x-y])%MOD)*fact[x])%MOD;
	}
	long long P(int x,int y){
		upper_change(x);
		if (x<y||y<0||x<0) return 0;
		return (fact_rev[x-y]*fact[x])%MOD;
	}
};
}
using po167::combination;


int main() {
	const ll mod=1e9+7;
	int N;
	cin>>N;
	ll ans=0;
	combination table(N*2,mod);
	ll tmp=0;
	rep(i,1,N+1){
		tmp+=(table.fact[N-i]*((table.Comb(N,i)*table.Comb(N-1,i-1))%mod))%mod;
	}
	tmp%=mod;
	tmp=(tmp*table.fact_rev[N])%mod;
	ans=-(tmp*tmp)%mod;
	rep(i,1,N+1){
		tmp=(table.fact_rev[i]*table.Comb(N-1+i,N-i))%mod;
		ans=(ans+tmp)%mod;
	}
	cout<<(ans+mod)%mod<<"\n";
}
accuracy: 1
time_ms: 10
memory_kb: 7620
final_rank: 4

code_snippet: #include<bits/stdc++.h>


#include <cassert>
#include <numeric>
#include <type_traits>

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

#include <cassert>
#include <vector>

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder



#include <utility>

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 < 2^31`
    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 int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @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);

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

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());
    }
    static_modint(bool 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());
    }
    dynamic_modint(bool 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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace atcoder;
typedef long long ll;
const ll INF = 1e18;
typedef modint1000000007 mint;
//cout << fixed << setprecision(12);

int main(){
	int n;
	cin >> n;
	vector<mint> fact(n+1, 1);
	vector<mint> factinv(n+1, 1);
	for (int i=2; i<=n; i++){
		fact[i] = fact[i-1] * i;
	}
	factinv[n] /= fact[n];
	for (int i=n-1; i>=2; i--){
		factinv[i] = factinv[i+1] * (i+1);
	}
	fenwick_tree<mint> dp3(n+1);
	fenwick_tree<mint> dp4(n+1);
	fenwick_tree<mint> dp5(n+1);
	fenwick_tree<mint> dp6(n+1);
	fenwick_tree<mint> dp7(n+1);
	// dp3 = dp1[j] / j!
	// dp4 = dp1[j] / j! * j
	// dp5 = dp2[j] / j! * j * j
	// dp6 = dp2[j] / j! * j
	// dp7 = dp2[j] / j!
	mint tmp;
	mint ans1, ans2;
	dp3.add(0, 1);
	dp7.add(0, 1);
	for (int i=1; i<=n; i++){
		tmp = fact[i-1] * (i * dp3.sum(0, i) - dp4.sum(0, i));
		dp3.add(i, tmp * factinv[i]);
		dp4.add(i, tmp * factinv[i] * i);
		if (i==n){
			ans1 = tmp;
		}
		tmp = fact[i-1] * (dp5.sum(0, i) - 2 * i * dp6.sum(0, i) + dp7.sum(0, i) * i * i);
		dp5.add(i, tmp * factinv[i] * i * i);
		dp6.add(i, tmp * factinv[i] * i);
		dp7.add(i, tmp * factinv[i]);
		if (i==n){
			ans2 = tmp;
		}
	}
	mint a = ans1 * factinv[n];
	mint ans = (ans2 - 2 * ans1 * a + a * a * fact[n]) * factinv[n];
	cout << ans.val() << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5744
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

using ll = long long;


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  // find x s.t. a^x = b
  static T log(T a,T b){
    const T sq=40000;
    unordered_map<T, T> dp;
    dp.reserve(sq);
    Mint res(1);
    for(int r=0;r<sq;r++){
      if(!dp.count(res.v)) dp[res.v]=r;
      res*=a;
    }
    Mint p=Mint(a).inv().pow(sq);
    res=b;
    for(int q=0;q<=MOD/sq+1;q++){
      if(dp.count(res.v)){
        T idx=q*sq+dp[res.v];
        if(idx>0) return idx;
      }
      res*=p;
    }
    assert(0);
    return T(-1);
  }

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


namespace FFT{
  using dbl = double;

  struct num{
    dbl x,y;
    num(){x=y=0;}
    num(dbl x,dbl y):x(x),y(y){}
  };

  inline num operator+(num a,num b){
    return num(a.x+b.x,a.y+b.y);
  }
  inline num operator-(num a,num b){
    return num(a.x-b.x,a.y-b.y);
  }
  inline num operator*(num a,num b){
    return num(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
  }
  inline num conj(num a){
    return num(a.x,-a.y);
  }

  int base=1;
  vector<num> rts={{0,0},{1,0}};
  vector<int> rev={0,1};

  const dbl PI=acosl(-1.0);

  void ensure_base(int nbase){
    if(nbase<=base) return;

    rev.resize(1<<nbase);
    for(int i=0;i<(1<<nbase);i++)
      rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1));

    rts.resize(1<<nbase);
    while(base<nbase){
      dbl angle=2*PI/(1<<(base+1));
      for(int i=1<<(base-1);i<(1<<base);i++){
        rts[i<<1]=rts[i];
        dbl angle_i=angle*(2*i+1-(1<<base));
        rts[(i<<1)+1]=num(cos(angle_i),sin(angle_i));
      }
      base++;
    }
  }

  void fft(vector<num> &a,int n=-1){
    if(n==-1) n=a.size();
    assert((n&(n-1))==0);

    int zeros=__builtin_ctz(n);
    ensure_base(zeros);
    int shift=base-zeros;
    for(int i=0;i<n;i++)
      if(i<(rev[i]>>shift))
        swap(a[i],a[rev[i]>>shift]);

    for(int k=1;k<n;k<<=1){
      for(int i=0;i<n;i+=2*k){
        for(int j=0;j<k;j++){
          num z=a[i+j+k]*rts[j+k];
          a[i+j+k]=a[i+j]-z;
          a[i+j]=a[i+j]+z;
        }
      }
    }
  }

  vector<num> fa;

  vector<Int> multiply(vector<int> &a,vector<int> &b){
    int need=a.size()+b.size()-1;
    int nbase=0;
    while((1<<nbase)<need) nbase++;
    ensure_base(nbase);

    int sz=1<<nbase;
    if(sz>(int)fa.size()) fa.resize(sz);
    for(int i=0;i<sz;i++){
      int x=(i<(int)a.size()?a[i]:0);
      int y=(i<(int)b.size()?b[i]:0);
      fa[i]=num(x,y);
    }
    fft(fa,sz);

    num r(0,-0.25/sz);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      num z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r;
      if(i!=j)
        fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r;
      fa[i]=z;
    }
    fft(fa,sz);

    vector<Int> res(need);
    for(int i=0;i<need;i++)
      res[i]=fa[i].x+0.5;

    return res;
  }

};

template<typename T>
struct ArbitraryModConvolution{
  using dbl=FFT::dbl;
  using num=FFT::num;

  vector<T> multiply(vector<T> as,vector<T> bs){
    int need=as.size()+bs.size()-1;
    int sz=1;
    while(sz<need) sz<<=1;
    vector<num> fa(sz),fb(sz);
    for(int i=0;i<(int)as.size();i++)
      fa[i]=num(as[i].v&((1<<15)-1),as[i].v>>15);
    for(int i=0;i<(int)bs.size();i++)
      fb[i]=num(bs[i].v&((1<<15)-1),bs[i].v>>15);

    fft(fa,sz);fft(fb,sz);

    dbl ratio=0.25/sz;
    num r2(0,-1),r3(ratio,0),r4(0,-ratio),r5(0,1);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      num a1=(fa[i]+conj(fa[j]));
      num a2=(fa[i]-conj(fa[j]))*r2;
      num b1=(fb[i]+conj(fb[j]))*r3;
      num b2=(fb[i]-conj(fb[j]))*r4;
      if(i!=j){
        num c1=(fa[j]+conj(fa[i]));
        num c2=(fa[j]-conj(fa[i]))*r2;
        num d1=(fb[j]+conj(fb[i]))*r3;
        num d2=(fb[j]-conj(fb[i]))*r4;
        fa[i]=c1*d1+c2*d2*r5;
        fb[i]=c1*d2+c2*d1;
      }
      fa[j]=a1*b1+a2*b2*r5;
      fb[j]=a1*b2+a2*b1;
    }
    fft(fa,sz);fft(fb,sz);

    vector<T> cs(need);
    using ll = long long;
    for(int i=0;i<need;i++){
      ll aa=T(llround(fa[i].x)).v;
      ll bb=T(llround(fb[i].x)).v;
      ll cc=T(llround(fa[i].y)).v;
      cs[i]=T(aa+(bb<<15)+(cc<<30));
    }
    return cs;
  }
};


template<typename T>
struct FormalPowerSeries{
  using Poly = vector<T>;
  using Conv = function<Poly(Poly, Poly)>;
  Conv conv;
  FormalPowerSeries(Conv conv):conv(conv){}

  Poly add(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]+=bs[i];
    return cs;
  }

  Poly sub(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]-=bs[i];
    return cs;
  }

  Poly mul(Poly as,Poly bs){
    return conv(as,bs);
  }

  Poly mul(Poly as,T k){
    for(auto &a:as) a*=k;
    return as;
  }

  // F(0) must not be 0
  Poly inv(Poly as,int deg){
    assert(as[0]!=T(0));
    Poly rs({T(1)/as[0]});
    int sz=1;
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      rs=sub(add(rs,rs),mul(mul(rs,rs),ts));
      rs.resize(sz);
    }
    return rs;
  }

  // not zero
  Poly div(Poly as,Poly bs){
    while(as.back()==T(0)) as.pop_back();
    while(bs.back()==T(0)) bs.pop_back();
    if(bs.size()>as.size()) return Poly();
    reverse(as.begin(),as.end());
    reverse(bs.begin(),bs.end());
    int need=as.size()-bs.size()+1;
    Poly ds=mul(as,inv(bs,need));
    ds.resize(need);
    reverse(ds.begin(),ds.end());
    return ds;
  }

  // F(0) must be 1
  Poly sqrt(Poly as,int deg){
    assert(as[0]==T(1));

    int sz=1;
    T inv2=T(1)/T(2);
    Poly ss({T(1)});
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      ss=add(ss,mul(ts,inv(ss,sz)));
      ss.resize(sz);
      for(T &x:ss) x*=inv2;
    }
    return ss;
  }

  Poly diff(Poly as){
    int n=as.size();
    Poly res(n-1);
    for(int i=1;i<n;i++) res[i-1]=as[i]*T(i);
    return res;
  }

  Poly integral(Poly as){
    int n=as.size();
    Poly res(n+1);
    res[0]=T(0);
    for(int i=0;i<n;i++) res[i+1]=as[i]/T(i+1);
    return res;
  }

  Poly exp(Poly as,int deg){
    Poly f({T(1)}),g({T(1)});

    int sz=1;
    while(sz<deg){
      g=sub(mul(g,T(2)),mul(f,mul(g,g)));
      g.resize(sz+1);
      Poly q=diff(as);
      q.resize(sz);
      Poly w=add(q,mul(g,sub(diff(f),mul(f,q))));
      w.resize(2*sz);
      f=add(f,mul(f,sub(as,integral(w))));
      f.resize(2*sz+1);
      sz<<=1;
    }
    return f;
  }

  // F(0) must be 1
  Poly log(Poly as,int deg){
    Poly rs=integral(mul(diff(as),inv(as,deg)));
    rs.resize(deg);
    return rs;
  }

  Poly partition(int n){
    Poly rs(n+1);
    rs[0]=T(1);
    for(int k=1;k<=n;k++){
      if(1LL*k*(3*k+1)/2<=n) rs[k*(3*k+1)/2]+=T(k%2?-1LL:1LL);
      if(1LL*k*(3*k-1)/2<=n) rs[k*(3*k-1)/2]+=T(k%2?-1LL:1LL);
    }
    return inv(rs,n+1);
  }
};

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;

  using M = Mint<int>;
  ArbitraryModConvolution<M> arb;
  auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);};
  FormalPowerSeries<M> FPS(conv);

  vector<M> line(n+1,0),quad(n+1,0);
  for(int i=1;i<=n;i++){
    line[i]=M(i).pow(0);
    quad[i]=M(i).pow(1);
  }

  M v1=FPS.exp(line,n+1)[n];
  M v2=FPS.exp(quad,n+1)[n];

  M ans=v2-v1*v1;
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 26088
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 200005


ll fact[SIZE],inverse_fact[SIZE];


ll extgcd(ll a,ll b,ll &x,ll &y){
	ll d = a;
	if(b != 0){
		d = extgcd(b,a%b,y,x);
		y -= (a/b)*x;
	}else{
		x = 1;
		y = 0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
    ll x,y;
    extgcd(a,m,x,y);
    return (m+x%m)%m;
}

ll mod_fact(ll n,ll p,ll &e){
	e = 0;
	if(n == 0)return 1;

	int res = mod_fact(n/p,p,e);
	e += n/p;

	if(n/p%2 != 0)return res*(p-fact[n%p])%p;
	return res*fact[n%p]%p;
}

ll nCm(ll n,ll m,ll p){
   if(n < m) return 0;
   ll e1,e2,e3;
    ll a1 = mod_fact(n,p,e1),a2 = mod_fact(m,p,e2),a3 = mod_fact(n-m,p,e3);
    if(e1 > e2+e3)return 0;
    return a1 * mod_inverse(a2*a3%p,p)%p;
}

int main(){

	fact[0] = 1;
	for(ll i = 1; i < SIZE; i++){
		fact[i] = i*fact[i-1];
		fact[i] %= MOD;
	}

	inverse_fact[SIZE-1] = mod_inverse(fact[SIZE-1],MOD);
	for(ll i = SIZE-1; i >= 1; i--){

		inverse_fact[i-1] = inverse_fact[i]*i;
		inverse_fact[i-1]%= MOD;
	}

	ll N;
	scanf("%lld",&N);

	ll EX = 0,EX2 = 0;

	for(int k = 1; k <= N; k++){

		EX += (nCm(N-1,k-1,MOD)*inverse_fact[k])%MOD;
		EX %= MOD;
	}

	for(int k = 1; k <= N; k++){

		EX2 += (nCm(N+k-1,2*k-1,MOD)*inverse_fact[k])%MOD;
		EX2 %= MOD;
	}

	ll ans = EX2-EX*EX;
	while(ans < 0){

		ans += MOD;
	}
	ans %= MOD;

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 6340
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
const int mod = 1000000007;
struct fft{
	struct compleX{
		double real,imag;
		compleX(){
		    real = imag = 0.;
		}
		compleX(double a,double b){
		    real = a; imag = b;
		}
		inline compleX operator +(const compleX &x) const { return {real+x.real, imag+x.imag}; }
		inline compleX operator -(const compleX &x) const { return {real-x.real, imag-x.imag}; }
		inline compleX operator *(const compleX &x) const { return {real*x.real-imag*x.imag, real*x.imag+imag*x.real}; }
		inline compleX operator *(const double  &y) const { return {real*y, imag*y}; }
	};
    compleX conj(const compleX &a) { return {a.real, -a.imag}; }
	const int MOD = 1000000007;
	const double PI = acos(-1);
	typedef compleX Complex;

	//Nは畳み込んだ後のサイズ以上
	const int N = (1<<18), L = 15, MASK = (1 << L) - 1;
	vector<Complex>w[20];
	void FFTInit() {
	    w[18].resize(N);
		for (int i = 0; i < N; ++i) {
			w[18][i] = Complex(cos(2 * i * PI / N), sin(2 * i * PI / N));
		}
		for(int i=17;i>=0;i--){
		    w[i].resize((1<<i));
		    for(int j=0;j<(1<<i);j++){
		        w[i][j] = w[i+1][j<<1];
		    }
		}
	}
	void FFT(vector<Complex>&p, int n) {
		for (int i = 1, j = 0; i < n - 1; ++i) {
			for (int s = n; j ^= s >>= 1, ~j & s;);
			if (i < j) {
				swap(p[i], p[j]);
			}
		}
	//	int bg = __lg(n);
		for (int d = 0; (1 << d) < n; ++d) {
			int m = 1 << d, m2 = m * 2, rm = n >> (d + 1);
			for (int i = 0; i < n; i += m2) {
				for (int j = 0; j < m; ++j) {
					Complex &p1 = p[i + j + m], &p2 = p[i + j];
					Complex t = w[d+1][j] * p1;
					p1 = p2 - t;
					p2 = p2 + t;
				}
			}
		}
	}
    vector<int> mod_mul(vector<int>a,vector<int>b){
        int NN = 1;
        while(NN < a.size()+b.size()) NN <<= 1;
        vector<Complex>A(NN), B(NN), C(NN), D(NN);
    	for (int i = 0; i < NN; ++i) {
    		if(i < a.size()){
    			A[i] = Complex(a[i] >> L, a[i] & MASK);
    		}
    		else{
    			A[i] = Complex(0.0, 0.0);
    		}
    		if(i < b.size()){
    			B[i] = Complex(b[i] >> L, b[i] & MASK);
    		}
    		else{
    			B[i] = Complex(0.0, 0.0);
    		}
    	}
    	FFT(A, NN); FFT(B, NN);
    	for (int i = 0; i < NN; ++i) {
    		int j = (NN - i) % NN;
    		Complex da = (A[i] - conj(A[j])) * Complex(0, -0.5),
    				db = (A[i] + conj(A[j])) * Complex(0.5, 0),
    				dc = (B[i] - conj(B[j])) * Complex(0, -0.5),
    				dd = (B[i] + conj(B[j])) * Complex(0.5, 0);
    		C[j] = da * dd + da * dc * Complex(0, 1);
    		D[j] = db * dd + db * dc * Complex(0, 1);
    	}
    	FFT(C, NN); FFT(D, NN);
    	a.resize(NN);
    	for (int i = 0; i < NN; ++i) {
    		long long da = (long long)(C[i].imag / NN + 0.5) % MOD,
    				  db = (long long)(C[i].real / NN + 0.5) % MOD,
    				  dc = (long long)(D[i].imag / NN + 0.5) % MOD,
    				  dd = (long long)(D[i].real / NN + 0.5) % MOD;
    		a[i] = ((dd << (L * 2)) + ((db + dc) << L) + da) % MOD;
    	}
    	return a;
    }
}kaede;

ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}

namespace power_series_master {
    //x^iの係数をvecc[i]にもつものとする
    //各々の処理はmod x^Mをサポートするものとする
    
    //add,sub,diff,intg,mul_int,mul,inv,sqrt,log,exp
    //add subでミスってたら流石に引退
    //diff,intgも流石に大丈夫だと思うけど、diffに定数を噛ませて0を返す仕様で変なハマり方をしないか心配
    //一番怖いのはlogで、手元でexpと組み合わせてテストしたら大丈夫そうだったけど、GP peterhof H でverifyしたいよね
    //mod は998244353固定なので、他の素数だった場合はmulをarbitrary_mod_convに変えないといけない
    
    //verify list
    
    //mul_int,sub,sqrt,inv... CF 438E
    //exp... yosupo judge
    #define vi vector<int>
    vi init(vi a, int x){
    	a.resize(x,0);
    	return a;
    }
    vi add(vi a, vi b,int M=-1){
        if(a.size() < b.size()) swap(a,b);
        for(int i=0;i<b.size();i++){
            a[i]+=b[i];
            if(a[i] < 0) a[i] += mod;
            if(a[i] >= mod) a[i] -= mod;
        }
        if(M >= 0 && a.size() > M) a.resize(M);
        return a;
    }
    vi sub(vi a, vi b,int M=-1){
        if(a.size() < b.size()) a.resize(b.size(),0);
        for(int i=0;i<b.size();i++){
            a[i]-=b[i];
            if(a[i] < 0) a[i] += mod;
            if(a[i] >= mod) a[i] -= mod;
        }
        if(M >= 0 && a.size() > M) a.resize(M);
        return a;
    }
    //微分
    vi diff(vi a,int M=-1){
    	reverse(a.begin(),a.end());
    	a.pop_back();
    	reverse(a.begin(),a.end());
    	for(int i=0;i<a.size();i++) a[i] = 1LL*a[i]*(i+1)%mod;
    	if(M >= 0 && a.size() > M) a.resize(M);
    	if(a.empty()) a.pb(0);
    	return a;
    }
    //積分、定数項は指定可能
    vi intg(vi a,int coef=0,int M=-1){
    	reverse(a.begin(),a.end());
    	a.push_back(coef);
    	reverse(a.begin(),a.end());
    	for(int i=1;i<a.size();i++) a[i] = 1LL*a[i]*modpow(i,mod-2)%mod;
    	if(M >= 0 && a.size() > M) a.resize(M);
    	return a;
    }
    vi mul_int(vi a,int x,int M=-1){
        for(int i=0;i<a.size();i++){
            ll A = 1LL*a[i]*x%mod;
            if(A < 0) A += mod;
            a[i] = A;
        }
        if(M >= 0 && a.size() > M) a.resize(M);
        return a;
    }
    vi mul(vi a,vi b,int M=-1){
        //modが99...のときはnttできる
        //任意modの場合は、適当に区切ってもいいしgarnerしてもいいし任せます
		a = kaede.mod_mul(a,b);
		if(M >= 0 && a.size() > M) a.resize(M);
        return a;
    }
    //vecで表される多項式の-1乗、x^(M-1)の項までを求めてM長のvectorを返す
    //ただし、vec[0] = 0の場合は無理
    //r1(z)f(z) = x*? + 1
    //rn(z)f(z) = x^n*? + 1とする
    //r(2n)(z) = 2*rn(z)-rn(z)^2f(z)とすればOK!
    vi inv(vi a,int M){
        if(a.empty() || a[0] == 0) return vi();
        //1の場合をやっておく
        vi r[30];
        r[0].pb(modpow(a[0],mod-2));
        int cur = 1;
        int nxt = 1;
        vi vec3;
        while(cur < M){
        	int nw = min(cur*2,(int)(a.size()));
        	int sz = vec3.size();
        	vec3.resize(nw,0);
        	for(int t=sz;t<nw;t++){
        		vec3[t] = a[t];
        	}
            r[nxt] = sub(mul_int(r[nxt-1],2),mul(mul(r[nxt-1],r[nxt-1]),vec3));
            r[nxt].resize(r[nxt-1].size()*2);
            nxt++;
            cur *= 2;
        }
        assert(r[nxt-1].size() >= M);
        r[nxt-1].resize(M);
        assert(r[nxt-1].size() == M);
        return r[nxt-1];
    }
    //vecで表される多項式の平方根、x^(M-1)の項までを求めてM長のvectorを返す
    //ただし、vec[0] = 0の場合は無理
    //整数の平方根求めるのかったるいのでそれはまた後日
    //s1(z)^2 = f(z) (mod z)
    //sn(z)^2 = f(z) (mod z^n) とする
    //s(2n)(z) = 1/2 * (sn(z)+f(z)*sn(z)^-1)
    vi sqrt(vi a,int M){
        if(a.empty() || a[0] == 0) return vi();
        //1の場合をやっておく
        vi s[30];
        //今日はvec[0] = 1のことしか考えません
        s[0].pb(1);
        assert(1LL*s[0][0]*s[0][0]%mod == a[0]);
        int cur = 1;
        int nxt = 1;
        vi vec3;
        while(cur < M){
            int nw = min(cur*2,(int)(a.size()));
        	int sz = vec3.size();
        	vec3.resize(nw,0);
        	for(int t=sz;t<nw;t++){
        		vec3[t] = a[t];
        	}
            s[nxt] = add(s[nxt-1],mul(vec3,inv(s[nxt-1],cur*2)));
            s[nxt] = mul_int(s[nxt],modpow(2,mod-2));
            s[nxt].resize(cur*2);
            nxt++;
            cur *= 2;
        }
        s[nxt-1].resize(M);
        assert(s[nxt-1].size() == M);
        return s[nxt-1];
    }
    //vecで表される多項式のlog()、x^(M-1)の項までを求めてM長のvectorを返す
    //ただし、vec[0] = 0の場合は無理
    //log(f(x)) = intg(f'(x)/f(x)) = intg(mul(diff(f(x),need+1),inv(f(x),need+1),need+1))
    //積分定数は0固定なので、vec[0]!=1のものを噛ませた場合は壊れる
    vi log(vi a,int M){
        if(a.empty() || a[0] == 0) return vi();
        vi x = diff(a,M);
        vi y = inv(a,M);
        vi z = mul(x,y,M);
        return intg(z,0,M);
    }
    //vecで表される多項式のexp()、x^(M-1)の項までを求めてM長のvectorを返す
    //ただし、vec[0] != 0の場合は無理
    //exp(a)の定数項は必ず1なので、定数項!=1の多項式のlogを噛ませた場合は壊れる
    //http://www.csd.uwo.ca/~eschost/publications/BoSc09-final.pdf
    vi exp(vi a,int M){
        if(a.empty() || a[0] != 0) return vi();
        vi f={1},g={1},q,aa;
        int m = 1;
        vi h = diff(a);
        while(m<=M){
			g = sub(mul_int(g,2,m),mul(mul(f,g,m),g,m),m);
			while(aa.size() < 2*m){
			    if(aa.size() >= a.size()) aa.pb(0);
			    else aa.pb(a[aa.size()]);
			}
			if(m>1){
				vi w = add(q,mul(g,sub(diff(f,2*m-1),mul(f,q,2*m-1),2*m-1),2*m-1),2*m-1);
				f = add(f,mul(f,sub(aa,intg(w,0,2*m),2*m),2*m),2*m);
			}
			else{
				vi w = mul(g,diff(f,2*m-1),2*m-1);
				f = add(f,mul(f,sub(aa,intg(w,0,2*m),2*m),2*m),2*m);
			}
			m*=2;
			while(q.size() < m-1){
				if(q.size() >= h.size()) q.pb(0);
				else q.pb(h[q.size()]);
			}
		}
		f.resize(M);
		assert(f.size() == M);
		return f;
    }
};

//ここから
int n;
vector<int>one,two;
int main(){
    kaede.FFTInit();
	cin >> n;
	one.pb(0); two.pb(0);
	for(int i=1;i<=n;i++){
		one.pb(1); two.pb(i);
	}
	auto O = power_series_master::exp(one,n+5);
	auto T = power_series_master::exp(two,n+5);
	ll ans = 1LL*T[n] - modpow(1LL*O[n],2);
	cout << (ans%mod+mod)%mod << endl;
}
accuracy: 1
time_ms: 680
memory_kb: 39824
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

using ll = long long;


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  // find x s.t. a^x = b
  static T log(T a,T b){
    const T sq=40000;
    unordered_map<T, T> dp;
    dp.reserve(sq);
    Mint res(1);
    for(int r=0;r<sq;r++){
      if(!dp.count(res.v)) dp[res.v]=r;
      res*=a;
    }
    Mint p=Mint(a).inv().pow(sq);
    res=b;
    for(int q=0;q<=MOD/sq+1;q++){
      if(dp.count(res.v)){
        T idx=q*sq+dp[res.v];
        if(idx>0) return idx;
      }
      res*=p;
    }
    assert(0);
    return T(-1);
  }

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


namespace FFT{
  using dbl = double;

  struct num{
    dbl x,y;
    num(){x=y=0;}
    num(dbl x,dbl y):x(x),y(y){}
  };

  inline num operator+(num a,num b){
    return num(a.x+b.x,a.y+b.y);
  }
  inline num operator-(num a,num b){
    return num(a.x-b.x,a.y-b.y);
  }
  inline num operator*(num a,num b){
    return num(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
  }
  inline num conj(num a){
    return num(a.x,-a.y);
  }

  int base=1;
  vector<num> rts={{0,0},{1,0}};
  vector<int> rev={0,1};

  const dbl PI=acosl(-1.0);

  void ensure_base(int nbase){
    if(nbase<=base) return;

    rev.resize(1<<nbase);
    for(int i=0;i<(1<<nbase);i++)
      rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1));

    rts.resize(1<<nbase);
    while(base<nbase){
      dbl angle=2*PI/(1<<(base+1));
      for(int i=1<<(base-1);i<(1<<base);i++){
        rts[i<<1]=rts[i];
        dbl angle_i=angle*(2*i+1-(1<<base));
        rts[(i<<1)+1]=num(cos(angle_i),sin(angle_i));
      }
      base++;
    }
  }

  void fft(vector<num> &a,int n=-1){
    if(n==-1) n=a.size();
    assert((n&(n-1))==0);

    int zeros=__builtin_ctz(n);
    ensure_base(zeros);
    int shift=base-zeros;
    for(int i=0;i<n;i++)
      if(i<(rev[i]>>shift))
        swap(a[i],a[rev[i]>>shift]);

    for(int k=1;k<n;k<<=1){
      for(int i=0;i<n;i+=2*k){
        for(int j=0;j<k;j++){
          num z=a[i+j+k]*rts[j+k];
          a[i+j+k]=a[i+j]-z;
          a[i+j]=a[i+j]+z;
        }
      }
    }
  }

  vector<num> fa;

  vector<Int> multiply(vector<int> &a,vector<int> &b){
    int need=a.size()+b.size()-1;
    int nbase=0;
    while((1<<nbase)<need) nbase++;
    ensure_base(nbase);

    int sz=1<<nbase;
    if(sz>(int)fa.size()) fa.resize(sz);
    for(int i=0;i<sz;i++){
      int x=(i<(int)a.size()?a[i]:0);
      int y=(i<(int)b.size()?b[i]:0);
      fa[i]=num(x,y);
    }
    fft(fa,sz);

    num r(0,-0.25/sz);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      num z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r;
      if(i!=j)
        fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r;
      fa[i]=z;
    }
    fft(fa,sz);

    vector<Int> res(need);
    for(int i=0;i<need;i++)
      res[i]=fa[i].x+0.5;

    return res;
  }

};

template<typename T>
struct ArbitraryModConvolution{
  using dbl=FFT::dbl;
  using num=FFT::num;

  vector<T> multiply(vector<T> as,vector<T> bs){
    int need=as.size()+bs.size()-1;
    int sz=1;
    while(sz<need) sz<<=1;
    vector<num> fa(sz),fb(sz);
    for(int i=0;i<(int)as.size();i++)
      fa[i]=num(as[i].v&((1<<15)-1),as[i].v>>15);
    for(int i=0;i<(int)bs.size();i++)
      fb[i]=num(bs[i].v&((1<<15)-1),bs[i].v>>15);

    fft(fa,sz);fft(fb,sz);

    dbl ratio=0.25/sz;
    num r2(0,-1),r3(ratio,0),r4(0,-ratio),r5(0,1);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      num a1=(fa[i]+conj(fa[j]));
      num a2=(fa[i]-conj(fa[j]))*r2;
      num b1=(fb[i]+conj(fb[j]))*r3;
      num b2=(fb[i]-conj(fb[j]))*r4;
      if(i!=j){
        num c1=(fa[j]+conj(fa[i]));
        num c2=(fa[j]-conj(fa[i]))*r2;
        num d1=(fb[j]+conj(fb[i]))*r3;
        num d2=(fb[j]-conj(fb[i]))*r4;
        fa[i]=c1*d1+c2*d2*r5;
        fb[i]=c1*d2+c2*d1;
      }
      fa[j]=a1*b1+a2*b2*r5;
      fb[j]=a1*b2+a2*b1;
    }
    fft(fa,sz);fft(fb,sz);

    vector<T> cs(need);
    using ll = long long;
    for(int i=0;i<need;i++){
      ll aa=T(llround(fa[i].x)).v;
      ll bb=T(llround(fb[i].x)).v;
      ll cc=T(llround(fa[i].y)).v;
      cs[i]=T(aa+(bb<<15)+(cc<<30));
    }
    return cs;
  }
};


template<typename T>
struct FormalPowerSeries{
  using Poly = vector<T>;
  using Conv = function<Poly(Poly, Poly)>;
  Conv conv;
  FormalPowerSeries(Conv conv):conv(conv){}

  Poly add(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]+=bs[i];
    return cs;
  }

  Poly sub(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]-=bs[i];
    return cs;
  }

  Poly mul(Poly as,Poly bs){
    return conv(as,bs);
  }

  Poly mul(Poly as,T k){
    for(auto &a:as) a*=k;
    return as;
  }

  // F(0) must not be 0
  Poly inv(Poly as,int deg){
    assert(as[0]!=T(0));
    Poly rs({T(1)/as[0]});
    int sz=1;
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      rs=sub(add(rs,rs),mul(mul(rs,rs),ts));
      rs.resize(sz);
    }
    return rs;
  }

  // not zero
  Poly div(Poly as,Poly bs){
    while(as.back()==T(0)) as.pop_back();
    while(bs.back()==T(0)) bs.pop_back();
    if(bs.size()>as.size()) return Poly();
    reverse(as.begin(),as.end());
    reverse(bs.begin(),bs.end());
    int need=as.size()-bs.size()+1;
    Poly ds=mul(as,inv(bs,need));
    ds.resize(need);
    reverse(ds.begin(),ds.end());
    return ds;
  }

  // F(0) must be 1
  Poly sqrt(Poly as,int deg){
    assert(as[0]==T(1));

    int sz=1;
    T inv2=T(1)/T(2);
    Poly ss({T(1)});
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      ss=add(ss,mul(ts,inv(ss,sz)));
      ss.resize(sz);
      for(T &x:ss) x*=inv2;
    }
    return ss;
  }

  Poly diff(Poly as){
    int n=as.size();
    Poly res(n-1);
    for(int i=1;i<n;i++) res[i-1]=as[i]*T(i);
    return res;
  }

  Poly integral(Poly as){
    int n=as.size();
    Poly res(n+1);
    res[0]=T(0);
    for(int i=0;i<n;i++) res[i+1]=as[i]/T(i+1);
    return res;
  }

  Poly exp(Poly as,int deg){
    Poly f({T(1)}),g({T(1)});

    int sz=1;
    while(sz<deg){
      g=sub(mul(g,T(2)),mul(f,mul(g,g)));
      g.resize(sz+1);
      Poly q=diff(as);
      q.resize(sz);
      Poly w=add(q,mul(g,sub(diff(f),mul(f,q))));
      w.resize(2*sz);
      f=add(f,mul(f,sub(as,integral(w))));
      f.resize(2*sz+1);
      sz<<=1;
    }
    return f;
  }

  // F(0) must be 1
  Poly log(Poly as,int deg){
    Poly rs=integral(mul(diff(as),inv(as,deg)));
    rs.resize(deg);
    return rs;
  }

  Poly partition(int n){
    Poly rs(n+1);
    rs[0]=T(1);
    for(int k=1;k<=n;k++){
      if(1LL*k*(3*k+1)/2<=n) rs[k*(3*k+1)/2]+=T(k%2?-1LL:1LL);
      if(1LL*k*(3*k-1)/2<=n) rs[k*(3*k-1)/2]+=T(k%2?-1LL:1LL);
    }
    return inv(rs,n+1);
  }
};

//INSERT ABOVE HERE
signed main(){
  int n;
  cin>>n;

  using M = Mint<int>;
  ArbitraryModConvolution<M> arb;
  auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);};
  FormalPowerSeries<M> FPS(conv);

  vector<M> line(n+1,0),quad(n+1,0);
  for(int i=1;i<=n;i++){
    line[i]=M(i).pow(0);
    quad[i]=M(i).pow(1);
  }

  M v1=FPS.exp(line,n+1)[n];
  M v2=FPS.exp(quad,n+1)[n];

  M ans=v2-v1*v1;
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 26224
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

using ll = long long;

template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}

namespace FastFourierTransform {
  using real = double;

  struct C {
    real x, y;

    C() : x(0), y(0) {}

    C(real x, real y) : x(x), y(y) {}

    inline C operator+(const C &c) const { return C(x + c.x, y + c.y); }

    inline C operator-(const C &c) const { return C(x - c.x, y - c.y); }

    inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); }

    inline C conj() const { return C(x, -y); }
  };

  const real PI = acosl(-1);
  int base = 1;
  vector< C > rts = {{0, 0},
                     {1, 0}};
  vector< int > rev = {0, 1};


  void ensure_base(int nbase) {
    if(nbase <= base) return;
    rev.resize(1 << nbase);
    rts.resize(1 << nbase);
    for(int i = 0; i < (1 << nbase); i++) {
      rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    }
    while(base < nbase) {
      real angle = PI * 2.0 / (1 << (base + 1));
      for(int i = 1 << (base - 1); i < (1 << base); i++) {
        rts[i << 1] = rts[i];
        real angle_i = angle * (2 * i + 1 - (1 << base));
        rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));
      }
      ++base;
    }
  }

  void fft(vector< C > &a, int n) {
    assert((n & (n - 1)) == 0);
    int zeros = __builtin_ctz(n);
    ensure_base(zeros);
    int shift = base - zeros;
    for(int i = 0; i < n; i++) {
      if(i < (rev[i] >> shift)) {
        swap(a[i], a[rev[i] >> shift]);
      }
    }
    for(int k = 1; k < n; k <<= 1) {
      for(int i = 0; i < n; i += 2 * k) {
        for(int j = 0; j < k; j++) {
          C z = a[i + j + k] * rts[j + k];
          a[i + j + k] = a[i + j] - z;
          a[i + j] = a[i + j] + z;
        }
      }
    }
  }

  vector< ll > multiply(const vector< int > &a, const vector< int > &b) {
    int need = (int) a.size() + (int) b.size() - 1;
    int nbase = 1;
    while((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    vector< C > fa(sz);
    for(int i = 0; i < sz; i++) {
      int x = (i < (int) a.size() ? a[i] : 0);
      int y = (i < (int) b.size() ? b[i] : 0);
      fa[i] = C(x, y);
    }
    fft(fa, sz);
    C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
    for(int i = 0; i <= (sz >> 1); i++) {
      int j = (sz - i) & (sz - 1);
      C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
      fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
      fa[i] = z;
    }
    for(int i = 0; i < (sz >> 1); i++) {
      C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
      C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i];
      fa[i] = A0 + A1 * s;
    }
    fft(fa, sz >> 1);
    vector< ll > ret(need);
    for(int i = 0; i < need; i++) {
      ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
    }
    return ret;
  }
};

template< typename T >
struct ArbitraryModConvolution {
  using real = FastFourierTransform::real;
  using C = FastFourierTransform::C;

  ArbitraryModConvolution() = default;

  vector< T > multiply(const vector< T > &a, const vector< T > &b, int need = -1) {
    if(need == -1) need = a.size() + b.size() - 1;
    int nbase = 0;
    while((1 << nbase) < need) nbase++;
    FastFourierTransform::ensure_base(nbase);
    int sz = 1 << nbase;
    vector< C > fa(sz);
    for(int i = 0; i < (int)a.size(); i++) {
      fa[i] = C(a[i].v & ((1 << 15) - 1), a[i].v >> 15);
    }
    fft(fa, sz);
    vector< C > fb(sz);
    if(a == b) {
      fb = fa;
    } else {
      for(int i = 0; i < (int)b.size(); i++) {
        fb[i] = C(b[i].v & ((1 << 15) - 1), b[i].v >> 15);
      }
      fft(fb, sz);
    }
    real ratio = 0.25 / sz;
    C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1);
    for(int i = 0; i <= (sz >> 1); i++) {
      int j = (sz - i) & (sz - 1);
      C a1 = (fa[i] + fa[j].conj());
      C a2 = (fa[i] - fa[j].conj()) * r2;
      C b1 = (fb[i] + fb[j].conj()) * r3;
      C b2 = (fb[i] - fb[j].conj()) * r4;
      if(i != j) {
        C c1 = (fa[j] + fa[i].conj());
        C c2 = (fa[j] - fa[i].conj()) * r2;
        C d1 = (fb[j] + fb[i].conj()) * r3;
        C d2 = (fb[j] - fb[i].conj()) * r4;
        fa[i] = c1 * d1 + c2 * d2 * r5;
        fb[i] = c1 * d2 + c2 * d1;
      }
      fa[j] = a1 * b1 + a2 * b2 * r5;
      fb[j] = a1 * b2 + a2 * b1;
    }
    fft(fa, sz);
    fft(fb, sz);
    vector< T > ret(need);
    for(int i = 0; i < need; i++) {
      ll aa = llround(fa[i].x);
      ll bb = llround(fb[i].x);
      ll cc = llround(fa[i].y);
      aa = T(aa).v, bb = T(bb).v, cc = T(cc).v;
      ret[i] = aa + (bb << 15) + (cc << 30);
    }
    return ret;
  }
};

template<typename T>
struct FormalPowerSeries{
  using Poly = vector<T>;
  using Conv = function<Poly(Poly, Poly)>;
  Conv conv;
  FormalPowerSeries(Conv conv):conv(conv){}

  Poly add(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]+=bs[i];
    return cs;
  }

  Poly sub(Poly as,Poly bs){
    int sz=max(as.size(),bs.size());
    Poly cs(sz,T(0));
    for(int i=0;i<(int)as.size();i++) cs[i]+=as[i];
    for(int i=0;i<(int)bs.size();i++) cs[i]-=bs[i];
    return cs;
  }

  Poly mul(Poly as,Poly bs){
    return conv(as,bs);
  }

  // F(0) must not be 0
  Poly inv(Poly as,int deg){
    assert(as[0]!=T(0));
    Poly rs({T(1)/as[0]});
    int sz=1;
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      rs=sub(add(rs,rs),mul(mul(rs,rs),ts));
      rs.resize(sz);
    }
    return rs;
  }

  // Only for divisable
  Poly div(Poly as,Poly bs){
    reverse(as.begin(),as.end());
    reverse(bs.begin(),bs.end());
    while(bs.back()==T(0)){
      assert(as.back()==T(0));
      as.pop_back();
      bs.pop_back();
    }
    reverse(as.begin(),as.end());
    reverse(bs.begin(),bs.end());
    int need=as.size()-bs.size()+1;
    return mul(as,inv(bs,need));
  }

  // as[0] must be 1
  Poly sqrt(Poly as,int deg){
    assert(as[0]==T(1));

    int sz=1;
    T inv2=T(1)/T(2);
    Poly ss({T(1)});
    while(sz<deg){
      sz<<=1;
      Poly ts(min(sz,(int)as.size()));
      for(int i=0;i<(int)ts.size();i++) ts[i]=as[i];
      ss=add(ss,mul(ts,inv(ss,sz)));
      ss.resize(sz);
      for(T &x:ss) x*=inv2;
    }
    return ss;
  }

  Poly partition(int n){
    Poly rs(n+1);
    rs[0]=T(1);
    for(int k=1;k<=n;k++){
      if(1LL*k*(3*k+1)/2<=n) rs[k*(3*k+1)/2]+=T(k%2?-1LL:1LL);
      if(1LL*k*(3*k-1)/2<=n) rs[k*(3*k-1)/2]+=T(k%2?-1LL:1LL);
    }
    return inv(rs,n+1);
  }

  Poly diff(Poly as){
    int n=as.size();
    Poly res(n-1);
    for(int i=1;i<n;i++) res[i-1]=as[i]*T(i);
    return res;
  }

  Poly integral(Poly as){
    int n=as.size();
    Poly res(n+1);
    res[0]=T(0);
    for(int i=0;i<n;i++) res[i+1]=as[i]/T(i+1);
    return res;
  }

  Poly exp(Poly as,int deg){
    Poly f({T(1)}),g({T(1)});

    int sz=1;
    while(sz<deg){
      g=sub(add(g,g),mul(f,mul(g,g)));
      g.resize(sz+1);
      Poly q=diff(as);
      q.resize(sz);
      Poly w=add(q,mul(g,sub(diff(f),mul(f,q))));
      w.resize(2*sz);
      f=add(f,mul(f,sub(as,integral(w))));
      f.resize(2*sz+1);
      sz<<=1;
    }
    return f;
  }
};
//INSERT ABOVE HERE

signed main(){
  int n;
  cin>>n;

  using M = Mint<int>;
  ArbitraryModConvolution<M> arb;
  auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);};
  FormalPowerSeries<M> FPS(conv);

  vector<M> line(n+1,0),quad(n+1,0);
  for(int i=1;i<=n;i++){
    line[i]=M(i).pow(0);
    quad[i]=M(i).pow(1);
  }

  M v1=FPS.exp(line,n+1)[n];
  M v2=FPS.exp(quad,n+1)[n];

  M ans=v2-v1*v1;
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 970
memory_kb: 25092
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
constexpr ll TEN(int n){return (n==0) ? 1 : 10 * TEN(n-1);}
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define rep1(i,N) for(int i=1;i<=(int)(N);i++)
#define fs first
#define sc second
#define eb emplace_back
#define pb eb
#define all(x) x.begin(),x.end()
template<class T, class U> void chmin(T& t, const U& u){ if(t>u) t = u;}
template<class T, class U> void chmax(T& t, const U& u){ if(t<u) t = u;}

#ifdef LOCAL
#define show(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& p){
    return os << "P(" << p.fs << ", " << p.sc << ")";
}
template<class T> ostream& operator<<(ostream& os, const V<T>& v){
    os << "[";
    for(auto d: v) os << d << ", ";
    return os << "]";
}
template<unsigned int mod_>
struct ModInt{
    using M = ModInt;
    constexpr static uint mod = mod_;
    uint v;
    ModInt():v(0){}
    ModInt(ll _v):v(normS(_v%mod+mod)){}
    explicit operator bool() const {return v!=0;}
    static uint normS(const uint&x){return (x<mod)?x:x-mod;}
    static M make(const uint&x){M m;m.v=x;return m;}
    M operator+(const M& b) const {return make(normS(v+b.v));}
    M operator-(const M& b) const {return make(normS(v+mod-b.v));}
    M operator-() const {return make(normS(mod-v));}
    M operator*(const M& b) const {return make((ull)v*b.v%mod);}
    M operator/(const M& b){return *this*b.inv();}
    M& operator+=(const M& b){return *this=*this+b;}
    M& operator-=(const M& b){return *this=*this-b;}
    M& operator*=(const M& b){return *this=*this*b;}
    M& operator/=(const M& b){return *this=*this/b;}
    ll extgcd(ll a,ll b,ll &x,ll& y) const {
        ll p[]={a,1,0},q[]={b,0,1};
        while(*q){
            ll t=*p/ *q;
            rep(i,3) swap(p[i]-=t*q[i],q[i]);
        }
        if(p[0]<0) rep(i,3) p[i] = -p[i];
        x = p[1], y = p[2];
        return p[0];
    }
    ModInt inv() const {
        ll x,y;
        extgcd(v,mod,x,y);
        return make(normS(x+mod));
    }
    M pow(ll p) const {
        if(p<0) return inv().pow(-p);
        M a = 1;
        M x = *this;
        while(p){
            if(p&1) a *= x;
            x *= x;
            p /= 2;
        }
        return a;
    }
    bool operator==(const M& b) const {return v == b.v;}
    bool operator!=(const M& b) const {return v != b.v;}
    friend ostream& operator<<(ostream &o, const M& x){return o<<x.v;}
};
using mint = ModInt<1000000007>;
V<mint> fact,ifact;
void InitFact(int N){
    fact.resize(N);
    ifact.resize(N);
    fact[0] = 1;
    rep1(i,N-1) fact[i] = fact[i-1] * i;
    ifact[N-1] = fact[N-1].inv();
    for(int i=N-2;i>=0;i--) ifact[i] = ifact[i+1] * (i+1);
}
int bsr(int x){return 31-__builtin_clz(x);}
using D = double;
const D pi = acos(-1);
using Pc = complex<D>;
void fft(bool type, V<Pc> &c){
    static V<Pc> buf[30];
    int N = c.size();
    int s = bsr(N);
    assert(1<<s == N);
    if(buf[s].empty()){
        buf[s] = V<Pc>(N);
        rep(i,N) buf[s][i] = polar<D>(1,i*2*pi/N);
    }
    V<Pc> a(N),b(N);
    copy(begin(c),end(c),begin(a));
    rep1(i,s){
        int W = 1<<(s-i);
        for(int y=0;y<N/2;y+=W){
            Pc now = buf[s][y];
            if(type) now = conj(now);
            rep(x,W){
                auto l = a[y<<1 | x];
                auto r = now * a[y<<1 | x | W];
                b[y|x] = l+r;
                b[y|x|N>>1] = l-r;
            }
        }
        swap(a,b);
    }
    copy(begin(a),end(a),begin(c));
}
template<class Mint>
V<Mint> multiply_fft(const V<Mint>& x, const V<Mint>& y){
    if(x.empty() || y.empty()) return {};
    const int B = 15;
    const int K = 2;
    int S = x.size() + y.size() - 1;
    int N = 1;while(N<S) N *= 2;
    V<Pc> a[K],b[K];
    rep(t,K){
        a[t] = V<Pc>(N);
        b[t] = V<Pc>(N);
        rep(i,x.size()) a[t][i] = Pc( (x[i].v >> (t*B)) & ((1<<B)-1) , 0);
        rep(i,y.size()) b[t][i] = Pc( (y[i].v >> (t*B)) & ((1<<B)-1) , 0);
        fft(false,a[t]);
        fft(false,b[t]);
    }
    V<Mint> z(S);
    V<Pc> c(N);
    Mint base = 1;
    rep(t,K+K-1){
        fill_n(begin(c),N,Pc(0,0));
        rep(xt,K){
            int yt = t-xt;
            if(0<=yt && yt<K){
                rep(i,N) c[i] += a[xt][i] * b[yt][i];
            }
        }
        fft(true,c);
        rep(i,S){
            c[i] *= 1.0/N;
            z[i] += Mint(ll(round(c[i].real()))) * base;
        }
        base *= 1<<B;
    }
    return z;
}

template<class D>
struct Poly{
    V<D> v;
    int size() const{return v.size();}
    Poly(){}
    Poly(V<D> _v) : v(_v){shrink();}

    Poly& shrink(){
        while(!v.empty() && v.back() == D(0)) v.pop_back();
        return *this;
    }
    D at(int i) const{
        return (i<size()) ? v[i] : D(0);
    }
    void set(int i,const D& x){
        if(i>=size() && !x) return;
        while(i>=size()) v.pb(D(0));
        v[i] = x;
        shrink();
        return;
    }
    Poly operator+(const Poly& r) const{
        int N = max(size(),r.size());
        V<D> ret(N);
        rep(i,N) ret[i] = at(i) + r.at(i);
        return Poly(ret);
    }
    Poly operator-(const Poly& r) const{
        int N = max(size(),r.size());
        V<D> ret(N);
        rep(i,N) ret[i] = at(i) - r.at(i);
        return Poly(ret);
    }
    Poly operator*(const Poly &r) const {
        if(size() == 0 || r.size() == 0) return Poly();
        return mul_fft(r);
    }
    Poly operator*(const D& r) const{
        int N = size();
        V<D> ret(N);
        rep(i,N) ret[i] = v[i] * r;
        return Poly(ret);
    }

    Poly mul_naive(const Poly &r) const{
        int N = size(), M = r.size();
        V<D> ret(N+M-1);
        rep(i,N) rep(j,M) ret[i+j] += at(i) * r.at(j);
        return Poly(ret);
    }
    Poly mul_fft(const Poly &r) const{
        return Poly(multiply_fft(v,r.v));
    }
    Poly strip(int n) const {
        V<D> res = v;
        res.resize(min(n,size()));
        return Poly(res);
    }
    
    Poly diff() const{
        int n = size();
        if(n == 0) return Poly();
        V<D> u(n-1);
        rep(i,n-1) u[i] = at(i+1) * (i+1);
        return Poly(u);
    }
    Poly intg() const {
        int n = size();
        V<D> u(n+1);
        rep(i,n) u[i+1] = at(i) / (i+1);
        return Poly(u);
    }
    Poly exp(int n) const {
        assert(at(0) == 0);
        Poly f({1}), g({1});
        for(int i=1;i<=n;i*=2){
            g = (g*2 - f*g*g).strip(i);
            auto q = (this->diff()).strip(i-1);
            auto w = (q+g*(f.diff()-f*q)).strip(2*i-1);
            f = (f + f * (*this - w.intg()).strip(2*i)).strip(2*i);
        }
        return f.strip(n+1);
    }

    friend ostream& operator<<(ostream&o,const Poly& x){
        if(x.size() == 0) return o<<0;
        rep(i,x.size()) if(x.v[i] != D(0)){
            o<<x.v[i]<<"x^"<<i;
            if(i!=x.size()-1) o<<" + ";
        }
        return o;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    InitFact(1000000);

    int N;
    cin >> N;
    mint ans = 0;
    {
        Poly<mint> logf;
        rep1(i,N+10) logf.set(i,1);
        auto f = logf.exp(N);
        ans -= f.at(N) * f.at(N);
    }
    {
        Poly<mint> logg;
        rep1(i,N+10) logg.set(i,i);
        auto g = logg.exp(N);
        ans += g.at(N);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 1830
memory_kb: 56200
final_rank: 15

code_snippet: #include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP rep
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD=1e9+7;

#define rall(X) (X).rbegin(),(X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}

int N;
LL fact[214514];
LL revf[214514];

LL mypow(LL base, LL exp) {
  if (!exp) return 1;
  LL res = mypow(base*base%MOD, exp/2);
  if (exp%2) res = res*base%MOD;
  return res;
}


LL Comb(LL n, LL k) {
  if (n < 0) return 0;
  if (n < k) return 0;
  return fact[n]*revf[k]%MOD*revf[n-k] % MOD; 
}

signed main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);

  fact[0] = revf[0] = 1;
  reps(i, 1, 214514) {
    fact[i] = fact[i-1]*i % MOD;
    revf[i] = revf[i-1]*mypow(i, MOD-2) % MOD;
  }

  cin >> N;
  
  LL a = 0;
  LL b = 0;
  reps(i, 1, N+1) {
    a += Comb(N+i-1, 2*i-1) * revf[i];
    a %= MOD;
    b += Comb(N-1, N-i) * revf[i];
    b %= MOD;
  }
  LL ans = (a + MOD - b*b%MOD) % MOD;
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6540
final_rank: 3