Problem p02862 (C++)

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

#define rep(i, s, n) for (ll i = (s); i < (n); i++)
#define rep0(i, n) rep(i, 0, n)
#define rep1(i, n) rep(i, 1, n + 1)
#define repR(i, s, n) for (ll i = (n - 1); i >= (s); i--)
#define repR0(i, n) repR(i, 0, n)
#define repR1(i, n) repR(i, 1, n + 1)

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;
#define fcout cout << fixed << setprecision(18)

const ll MAX = 2e6 + 1;
ll MOD = 1e9 + 7;
ll INV[MAX] = {}, FACTORIAL[MAX] = {}, FACTORIAL_INV[MAX] = {};

void calc_init() {
  INV[1] = 1;
  FACTORIAL[0] = FACTORIAL[1] = 1;
  FACTORIAL_INV[0] = FACTORIAL_INV[1] = 1;

  rep(i, 2, MAX + 1) {
    ll q = MOD / i, r = MOD % i;
    INV[i] = (-INV[r] * q) % MOD;
    INV[i] = (INV[i] + MOD) % MOD;
    FACTORIAL[i] = (i * FACTORIAL[i - 1]) % MOD;
    FACTORIAL_INV[i] = (INV[i] * FACTORIAL_INV[i - 1]) % MOD;
  }
}

ll nCk(ll n, ll k) {
  ll ans = FACTORIAL[n];
  ans = (ans * FACTORIAL_INV[k]) % MOD;
  ans = (ans * FACTORIAL_INV[n - k]) % MOD;
  return ans;
}

int main() {
  ll X, Y;
  cin >> X >> Y;

  if ((X + Y) % 3 != 0) {
    cout << 0 << BR;
    return 0;
  }
  calc_init();

  ll n = (X + Y) / 3;
  ll k = X - n;
  if (k < 0) {
    cout << 0 << BR;
    return 0;
  }

  cout << nCk(n, k) << BR;
  return 0;
}

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

#define rep(i, s, n) for (ll i = (s); i < (n); i++)
#define rep0(i, n) rep(i, 0, n)
#define rep1(i, n) rep(i, 1, n + 1)
#define repR(i, s, n) for (ll i = (n - 1); i >= (s); i--)
#define repR0(i, n) repR(i, 0, n)
#define repR1(i, n) repR(i, 1, n + 1)

#define BR "\n"
#define SP " "
#define SHOW(x)                                                                \
  for (int i = 0; i < x.size(); i++) {                                         \
    cout << x[i] << SP;                                                        \
  }                                                                            \
  cout << BR;
#define SHOW2(x)                                                               \
  for (int j = 0; j < x.size(); j++) {                                         \
    SHOW(x[j]);                                                                \
  }                                                                            \
  cout << BR;
#define fcout cout << fixed << setprecision(18)

const ll MAX = 2e6 + 1;
ll MOD = 1e9 + 7;
ll INV[MAX] = {}, FACTORIAL[MAX] = {}, FACTORIAL_INV[MAX] = {};

void calc_init() {
  INV[1] = 1;
  FACTORIAL[0] = FACTORIAL[1] = 1;
  FACTORIAL_INV[0] = FACTORIAL_INV[1] = 1;

  rep(i, 2, MAX + 1) {
    ll q = MOD / i, r = MOD % i;
    INV[i] = (-INV[r] * q) % MOD;
    INV[i] = (INV[i] + MOD) % MOD;
    FACTORIAL[i] = (i * FACTORIAL[i - 1]) % MOD;
    FACTORIAL_INV[i] = (INV[i] * FACTORIAL_INV[i - 1]) % MOD;
  }
}

ll nCk(ll n, ll k) {
  ll ans = FACTORIAL[n];
  ans = (ans * FACTORIAL_INV[k]) % MOD;
  ans = (ans * FACTORIAL_INV[n - k]) % MOD;
  return ans;
}

int main() {
  ll X, Y;
  cin >> X >> Y;

  if ((X + Y) % 3 != 0) {
    cout << 0 << BR;
    return 0;
  }
  calc_init();

  ll n = (X + Y) / 3;
  ll k = X - n;
  if (X < n || Y < n) {
    cout << 0 << BR;
    return 0;
  }

  cout << nCk(n, k) << BR;
  return 0;
}