Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
//----------------------------------------------------------------

// int MOD = 998244353;
int MOD = 1000000007;

ll bitpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n % 2 == 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

// 高速nCk
#define N 2000010
int fac[N + 1];
int inv[N + 1];
ll COM(ll n, ll k) { return fac[n] * inv[k] % MOD * inv[n - k] % MOD; }

//----------------------------------------------------------------
signed main() {

  int x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  if ((2 * x > y && y > x) || (2 * y > x && x > y)) {
    cout << 0 << endl;
    return 0;
  }

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = bitpow(fac[i], MOD - 2) % MOD;
  }

  int n = (x + y) / 3;
  int X = x - n;
  int Y = n - X;

  cout << fac[X + Y] * inv[X] % MOD * inv[Y] % MOD << endl;
  // cout << n << " " <<  X << " " << Y << endl;

  return 0;
}

//----------------------------------------------------------------

// g++ -std=c++14 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl
// https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c
// rm -r -f test;oj dl
// https://nikkei2019-2-qual.contest.atcoder.jp/tasks/nikkei2019_2_qual_b rm -r
// -f test;oj dl https://abc145.contest.atcoder.jp/tasks/abc145_b

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(), a.end())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MP(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define INF LLONG_MAX / 2
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cerr << #v << ":";                                                           \
  rep(i, v.size()) cerr << " " << v[i];                                        \
  cerr << endl
//----------------------------------------------------------------

// int MOD = 998244353;
int MOD = 1000000007;

ll bitpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n % 2 == 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

// 高速nCk
#define N 2000010
int fac[N + 1];
int inv[N + 1];
ll COM(ll n, ll k) { return fac[n] * inv[k] % MOD * inv[n - k] % MOD; }

//----------------------------------------------------------------
signed main() {

  int x, y;
  cin >> x >> y;

  if ((x + y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  if (2 * x < y || 2 * y < x) {
    cout << 0 << endl;
    return 0;
  }

  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = bitpow(fac[i], MOD - 2) % MOD;
  }

  int n = (x + y) / 3;
  int X = x - n;
  int Y = n - X;

  cout << fac[X + Y] * inv[X] % MOD * inv[Y] % MOD << endl;
  // cout << n << " " <<  X << " " << Y << endl;

  return 0;
}

//----------------------------------------------------------------

// g++ -std=c++14 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl
// https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c
// rm -r -f test;oj dl
// https://nikkei2019-2-qual.contest.atcoder.jp/tasks/nikkei2019_2_qual_b rm -r
// -f test;oj dl https://abc145.contest.atcoder.jp/tasks/abc145_b