Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

/// --- modulo Library {{{ ///
constexpr ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return d;
}
constexpr ll modpow(ll a, ll b, ll mod = 1e9 + 7) {
  a = (a % mod + mod) % mod;
  ll r = 1;
  while (b) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
constexpr ll modinv(ll a, ll mod = 1e9 + 7) {
  a = (a % mod + mod) % mod;
  ll x = 0, y = 0;
  extgcd(a, mod, x, y);
  return (x % mod + mod) % mod;
}
template <int N, int mod = int(1e9 + 7)> struct Factorial {
  int arr[N + 1], inv[N + 1];
  int operator[](int i) const { return arr[i]; }
  constexpr Factorial() : arr(), inv() {
    arr[0] = 1;
    for (int i = 1; i <= N; i++) {
      arr[i] = (ll)i * arr[i - 1] % mod;
    }
    inv[N] = modinv(arr[N], mod);
    for (int i = N - 1; i >= 0; i--) {
      inv[i] = (ll)(i + 1) * inv[i + 1] % mod;
    }
  }
  int C(int n, int r) const {
    if (n < 0 || r < 0 || n < r)
      return 0;
    return (((ll)arr[n] * inv[r]) % mod * inv[n - r]) % mod;
  }
};
/// }}}--- ///

constexpr int N = 2e5;
constexpr Factorial<N> fact;

ll mod = 1e9 + 7;

// これは天才
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n;
  cin >> n;
  if (n == 2)
    return (cout << 1 << endl, 0);
  ll ans = 0;
  ll prev = 0;
  for (int k = 1; k <= n - 1; k++) {
    ll patterns = fact.C(k - 1, n - 1 - k);
    patterns = (patterns * fact[k]) % mod;
    patterns = (patterns * fact[n - 1 - k]) % mod;

    // ここでpatternsは k "以下" の場合を数えている.
    // つまりこれは数え上げテクニックとして,
    // a_k - a_(k-1)
    // などで just k回目 の値を数えられる.

    ans = (((ans + (patterns - prev) * k)) % mod + mod) % mod;
    prev = patterns;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

/// --- modulo Library {{{ ///
constexpr ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return d;
}
constexpr ll modpow(ll a, ll b, ll mod = 1e9 + 7) {
  a = (a % mod + mod) % mod;
  ll r = 1;
  while (b) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
constexpr ll modinv(ll a, ll mod = 1e9 + 7) {
  a = (a % mod + mod) % mod;
  ll x = 0, y = 0;
  extgcd(a, mod, x, y);
  return (x % mod + mod) % mod;
}
template <int N, int mod = int(1e9 + 7)> struct Factorial {
  int arr[N + 1], inv[N + 1];
  int operator[](int i) const { return arr[i]; }
  constexpr Factorial() : arr(), inv() {
    arr[0] = 1;
    for (int i = 1; i <= N; i++) {
      arr[i] = (ll)i * arr[i - 1] % mod;
    }
    inv[N] = modinv(arr[N], mod);
    for (int i = N - 1; i >= 0; i--) {
      inv[i] = (ll)(i + 1) * inv[i + 1] % mod;
    }
  }
  int C(int n, int r) const {
    if (n < 0 || r < 0 || n < r)
      return 0;
    return (((ll)arr[n] * inv[r]) % mod * inv[n - r]) % mod;
  }
};
/// }}}--- ///

constexpr int N = 1e6 + 10;
constexpr Factorial<N> fact;

ll mod = 1e9 + 7;

// これは天才
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n;
  cin >> n;
  if (n == 2)
    return (cout << 1 << endl, 0);
  ll ans = 0;
  ll prev = 0;
  for (int k = 1; k <= n - 1; k++) {
    ll patterns = fact.C(k - 1, n - 1 - k);
    patterns = (patterns * fact[k]) % mod;
    patterns = (patterns * fact[n - 1 - k]) % mod;

    // ここでpatternsは k "以下" の場合を数えている.
    // つまりこれは数え上げテクニックとして,
    // a_k - a_(k-1)
    // などで just k回目 の値を数えられる.

    ans = (((ans + (patterns - prev) * k)) % mod + mod) % mod;
    prev = patterns;
  }
  cout << ans << endl;
}