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

///////////////////// author : TrEbLeCleF /////////////////////
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;

#define pb(x) push_back(x)
#define deb(x) cerr << #x << " = " << x << "\\n"
#define print(v, s, e)                                                         \\
  for (int i = s; i < e; i++)                                                  \\
    cout << v[i] << " ";                                                       \\
  cout << "\\n";
#define INF 1e17
#define PI 3.14159265358979323846
int del_x[4] = {-1, 1, 0, 0};
int del_y[4] = {0, 0, 1, -1};
int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
/////////////////////////// GLOBAL ///////////////////////////

template <int MOD_> class modnum {
  static constexpr int MOD = MOD_;
  static_assert(MOD_ > 0, "MOD must be positive");

private:
  using ll = long long;

  int v;

  static int minv(int a, int m) {
    a %= m;
    assert(a);
    return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
  }

public:
  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) {
    if (v < 0)
      v += MOD;
  }
  explicit operator int() const { return v; }
  friend std::ostream &operator<<(std::ostream &out, const modnum &n) {
    return out << int(n);
  }
  friend std::istream &operator>>(std::istream &in, modnum &n) {
    ll v_;
    in >> v_;
    n = modnum(v_);
    return in;
  }

  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum inv() const {
    modnum res;
    res.v = minv(v, MOD);
    return res;
  }
  friend modnum inv(const modnum &m) { return m.inv(); }
  modnum neg() const {
    modnum res;
    res.v = v ? MOD - v : 0;
    return res;
  }
  friend modnum neg(const modnum &m) { return m.neg(); }

  modnum operator-() const { return neg(); }
  modnum operator+() const { return modnum(*this); }

  modnum &operator++() {
    v++;
    if (v == MOD)
      v = 0;
    return *this;
  }
  modnum &operator--() {
    if (v == 0)
      v = MOD;
    v--;
    return *this;
  }
  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MOD;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= o.inv(); }

  friend modnum operator++(modnum &a, int) {
    modnum r = a;
    ++a;
    return r;
  }
  friend modnum operator--(modnum &a, int) {
    modnum r = a;
    --a;
    return r;
  }
  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }
};

template <typename T> T power(T a, long long b) {
  assert(b >= 0);
  T r = 1;
  while (b) {
    if (b & 1)
      r *= a;
    b >>= 1;
    a = a * a;
  }
  return r;
}

const int mod = 1e9 + 7;
using Mint = modnum<mod>;
const int mxn = 5e5 + 1;

Mint f1[mxn], f2[mxn];
void init() {
  f1[0] = f2[0] = 1;
  for (int i = 1; i < mxn; i++) {
    f1[i] = i * f1[i - 1];
    f2[i] = inv(f1[i]);
  }
}
Mint choose(int n, int r) {
  Mint ans = 1;
  ans = f1[n] * f2[r] * f2[n - r];
  return ans;
}
/////////////////////////// GLOBAL ///////////////////////////
void solve() {
  init();
  int n, m;
  cin >> n >> m;
  Mint ans = 0;
  int c = 1;
  for (int i = 0; i <= n; i++) {
    ans += c * choose(n, i) * choose(m - i, n - i) * f1[n - i];
    // deb(ans);
    c = -c;
  }
  cout << ans * choose(m, n) * f1[n] << "\\n";
}

int32_t main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  freopen("TC", "r", stdin);
  // freopen("output.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    // cout << "Case #"<<tt<<": ";
    solve();
  }
#ifndef ONLINE_JUDGE
  cerr << "\\nRun Time -> " << (double)(clock() - tStart) / CLOCKS_PER_SEC
       << " sec\\n";
#endif
}

Error: WA

Fixed submission:

///////////////////// author : TrEbLeCleF /////////////////////
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
/////////////////////////// ..... ///////////////////////////
#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\\n"; }
template <typename T, typename... Args> void DBG(T first, Args... args) {
  cerr << first;
  if (sizeof...(args))
    cerr << ", ";
  DBG(args...);
}
/////////////////////////// ..... ///////////////////////////
#define int ll
#define pb(x) push_back(x)
#define print(v, s, e)                                                         \\
  for (int i = s; i < e; i++)                                                  \\
    cout << v[i] << " ";                                                       \\
  cout << "\\n";
#define INF 1e17
#define PI 3.14159265358979323846
int del_x[4] = {-1, 1, 0, 0};
int del_y[4] = {0, 0, 1, -1};
int XX[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int YY[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
/////////////////////////// GLOBAL ///////////////////////////
const int mxn = 5e5, M = 1e9 + 7;
int n, m;
ll iv[mxn + 1], f1[mxn + 1], f2[mxn + 1];

ll nck(int n, int k) { return f1[n] * f2[k] % M * f2[n - k] % M; }
void init() {
  f1[0] = f2[0] = 1;
  iv[1] = 1;
  for (int i = 2; i < mxn; i++) {
    iv[i] = M - M / i * iv[M % i] % M;
  }
  for (int i = 1; i < mxn; i++) {
    f1[i] = i * f1[i - 1] % M;
    f2[i] = iv[i] * f2[i - 1] % M;
  }
}

/////////////////////////// GLOBAL ///////////////////////////
void solve() {
  init();
  cin >> n >> m;
  int ans = 0;
  int c = 1;
  for (int i = 0; i < n + 1; i++) {
    ans += (M + c * nck(n, i) * nck(m - i, n - i) * f1[n - i]) % M;
    c *= -1;
  }
  cout << ans * nck(m, n) * f1[n] % M << "\\n";
}

int32_t main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  freopen("TC", "r", stdin);
  // freopen("output.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    // cout << "Case #"<<tt<<": ";
    solve();
  }
#ifndef ONLINE_JUDGE
  cerr << "\\nRun Time -> " << (double)(clock() - tStart) / CLOCKS_PER_SEC
       << " sec\\n";
#endif
}