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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

typedef long long i64;

const i64 MOD = 1e9 + 7;
template <i64 MOD> struct ModInt {
  i64 value;

  ModInt(i64 v = 0) { value = v % MOD; }

  operator i64() { return value; }
  ModInt<MOD> operator+(int v) { return (value + v) % MOD; }
  ModInt<MOD> operator+(i64 v) { return (value + v) % MOD; }

  ModInt<MOD> operator-(i64 v) {
    v %= MOD;
    return (MOD + value - v) % MOD;
  }

  ModInt<MOD> operator*(i64 v) { return (value * v) % MOD; }
};

// Eucli
i64 inv_mod(i64 a, i64 m = MOD) {
  i64 b, x, u, q, abs_m, tmp;

  abs_m = (m < 0) ? -m : m;
  b = m;
  x = 1;
  u = 0;
  while (b > 0) {
    q = a / b;
    tmp = u;
    u = x - q * u;
    x = tmp;
    tmp = b;
    b = a - q * b;
    a = tmp;
  }

  return (x < 0) ? abs_m + x : x;
}

i64 fact[404040];
i64 inv_fact[404040];
using mint = ModInt<MOD>;

mint nCr(int n, int r) {
  return (fact[n] * inv_fact[r] % MOD * inv_fact[n - r] % MOD) % MOD;
}

// 重複組み合わせ
i64 nHr(int n, int r) { return (nCr(n + r - 1, r)) % MOD; }

int main() {
  fact[0] = 1;
  for (int i = 1; i < 404040; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  inv_fact[404040 - 1] = inv_mod(fact[404040 - 1]);
  for (int i = 404040 - 2; i >= 0; i--) {
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
  }

  mint result = 0;

  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i <= k; i++) {
    int j = k - i;
    mint l = 1, r = 1;
    if (i != 0)
      l = nCr(n + 2 * i, i) - nCr(n + 2 * i, i - 1);
    if (j != 0)
      r = nCr(m + 2 * j, j) - nCr(m + 2 * j, j - 1);
    result = result + l * r * nCr(n + m + 2 * k, n + 2 * i);
  }
  cout << result << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

typedef long long i64;

const i64 MOD = 1e9 + 7;
template <i64 MOD> struct ModInt {
  i64 value;

  ModInt(i64 v = 0) { value = v % MOD; }

  operator i64() { return value; }
  ModInt<MOD> operator+(int v) { return (value + v) % MOD; }
  ModInt<MOD> operator+(i64 v) { return (value + v) % MOD; }

  ModInt<MOD> operator-(i64 v) {
    v %= MOD;
    return (MOD + value - v) % MOD;
  }

  ModInt<MOD> operator*(i64 v) { return (value * v) % MOD; }
};

// Eucli
i64 inv_mod(i64 a, i64 m = MOD) {
  i64 b, x, u, q, abs_m, tmp;

  abs_m = (m < 0) ? -m : m;
  b = m;
  x = 1;
  u = 0;
  while (b > 0) {
    q = a / b;
    tmp = u;
    u = x - q * u;
    x = tmp;
    tmp = b;
    b = a - q * b;
    a = tmp;
  }

  return (x < 0) ? abs_m + x : x;
}

i64 fact[202020];
i64 inv_fact[202020];
using mint = ModInt<MOD>;

mint nCr(int n, int r) {
  return (fact[n] * inv_fact[r] % MOD * inv_fact[n - r] % MOD) % MOD;
}

// 重複組み合わせ
i64 nHr(int n, int r) { return (nCr(n + r - 1, r)) % MOD; }

int main() {
  fact[0] = 1;
  for (int i = 1; i < 202020; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  inv_fact[202020 - 1] = inv_mod(fact[202020 - 1]);
  for (int i = 202020 - 2; i >= 0; i--) {
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
  }

  mint result = 0;

  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i <= k; i++) {
    int j = k - i;
    mint l = 1, r = 1;
    if (i != 0)
      l = nCr(n + 2 * i, i) - nCr(n + 2 * i, i - 1);
    if (j != 0)
      r = nCr(m + 2 * j, j) - nCr(m + 2 * j, j - 1);
    result = result + l * r * nCr(n + m + 2 * k, n + 2 * i);
  }
  cout << result << endl;
}