Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T> T power(T x, long long n, const T &m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x;
  T tmp = power(x, n / 2, m);
  if (n % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * x % m;
}

template <typename T> T inverse(T x, const T &m) { return power(x, m - 2, m); }

struct Combination {
private:
  int N;
  long long M;
  vector<long long> f, i;

public:
  void init(int n, long long m) {
    N = n;
    M = m;
    f.resize(N + 1);
    f[0] = 1;
    for (long long j = 1; j <= N; j++) {
      f[j] = f[j - 1] * j % M;
    }
    i.resize(N + 1);
    i[N] = inverse(f[N], M);
    for (long long j = N - 1; j >= 0; j--) {
      i[j] = i[j + 1] * (j + 1) % M;
    }
  }
  long long fact(int n) { return f[n]; }
  long long P(int n, int r) { return f[n] * i[n - r] % M; }
  long long C(int n, int r) { return f[n] * i[r] % M * i[n - r] % M; }
};

int N, M;
Combination C;

signed main() {
  cin >> N >> M;
  C.init(M, MOD);
  ll ans = 0;
  rep(i, N + 1) {
    if (i % 2 == 0)
      ans += C.C(N, i) * C.P(M, i) % MOD * C.P(M - i, N - i) % MOD *
             C.P(M - i, N - i) % MOD;
    else
      ans -= C.C(N, i) * C.P(M, i) % MOD * C.P(M - i, N - i) % MOD *
             C.P(M - i, N - i) % MOD;
    ans = (ans + 2 * MOD) % MOD;
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T> T power(T x, long long n, const T &m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x;
  T tmp = power(x, n / 2, m);
  if (n % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * x % m;
}

template <typename T> T inverse(T x, const T &m) { return power(x, m - 2, m); }

struct Combination {
  int N;
  long long M;
  vector<long long> f, i;

public:
  void init(int n, long long m) {
    N = n;
    M = m;
    f.resize(N + 1);
    f[0] = 1;
    for (long long j = 1; j <= N; j++) {
      f[j] = f[j - 1] * j % M;
    }
    i.resize(N + 1);
    i[N] = inverse(f[N], M);
    for (long long j = N - 1; j >= 0; j--) {
      i[j] = i[j + 1] * (j + 1) % M;
    }
  }
  long long fact(int n) { return f[n]; }
  long long C(int n, int r) { return f[n] * i[r] % M * i[n - r] % M; }
};

int N, M;
Combination C;

ll dp[1000][1000] = {};

signed main() {
  cin >> N >> M;
  C.init(M, MOD);

  dp[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= N - i; j++) {
      dp[i][j] = (j > 0 ? dp[i - 1][j - 1] * j * j : 0) +
                 2 * dp[i - 1][j] * j * i + dp[i - 1][j + 1] * i * (i - 1);
      dp[i][j] %= MOD;
    }
  }

  ll ans = 0;
  for (int i = max(0, 2 * N - M); i <= N; i++) { // 被る個数
    ll tmp = 1;
    tmp *= C.C(M, i);
    tmp %= MOD;
    ll x = 0;
    for (int j = 0; j <= min(i, N - i); j++) {
      x += dp[i][j];
      cout << dp[i][j] << " ";
    }
    cout << endl;
    tmp *= x;
    tmp %= MOD;
    cout << x << endl;

    tmp *= C.C(M - i, N - i);
    tmp %= MOD;
    tmp *= C.fact(N - i);
    tmp %= MOD;
    tmp *= C.C(M - N, N - i);
    tmp %= MOD;
    tmp *= C.fact(N - i);
    tmp %= MOD;
    ans += tmp;
  }
  cout << ans % MOD << endl;
}