Problem p02862 (C++)

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

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x)                                                               \
  do {                                                                         \
    cerr << #x << ": " << x << "\n";                                           \
  } while (0)
#define debugv(x)                                                              \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto &e : (x))                                                        \
      cerr << e << " ";                                                        \
    cerr << "\n";                                                              \
  } while (0)
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define forn(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n); i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << x << '\n';                                                         \
    return 0;                                                                  \
  } while (0)
#define FIRSTWIN                                                               \
  do {                                                                         \
    cout << "Monocarp\n";                                                      \
    return 0;                                                                  \
  } while (0)
#define SECONDWIN                                                              \
  do {                                                                         \
    cout << "Bicarp\n";                                                        \
    return 0;                                                                  \
  } while (0)

const int MAXN = 2e6 + 4;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;

int x, y;
int fact[MAXN], inv[MAXN], ifact[MAXN];

template <typename T, typename T1> T mod(T x, T1 p) {
  x %= p;
  if (x < 0)
    x += p;
  return x;
}

template <typename T> T inverse(T x, T p) {
  x = mod(x, p);
  if (x == 1)
    return x;
  return mod((1LL * (-p / x) * (inv[p % x] % p)), p);
  // Since inverse of p % x is already calculated.
}

int NcR(int n, int r) {
  int ret = (1LL * ifact[n - r] * ifact[r]) % MOD;
  ret = (1LL * ret * fact[n]) % MOD;
  return ret;
}

int main() {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = 1LL * fact[i - 1] * i % MOD;
  }

  ifact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    inv[i] = inverse(i, MOD);
    ifact[i] = 1LL * ifact[i - 1] * inv[i] % MOD;
  }

  cin >> x >> y;
  if ((2 * x - y) % 3)
    RETURN(0);
  if ((2 * y - x) % 3)
    RETURN(0);

  int a = (2 * x - y) / 3;
  int b = (2 * y - x) / 3;

  cout << NcR(a + b, a) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x)                                                               \
  do {                                                                         \
    cerr << #x << ": " << x << "\n";                                           \
  } while (0)
#define debugv(x)                                                              \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto &e : (x))                                                        \
      cerr << e << " ";                                                        \
    cerr << "\n";                                                              \
  } while (0)
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define forn(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n); i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << x << '\n';                                                         \
    return 0;                                                                  \
  } while (0)
#define FIRSTWIN                                                               \
  do {                                                                         \
    cout << "Monocarp\n";                                                      \
    return 0;                                                                  \
  } while (0)
#define SECONDWIN                                                              \
  do {                                                                         \
    cout << "Bicarp\n";                                                        \
    return 0;                                                                  \
  } while (0)

const int MAXN = 2e6 + 4;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;

int x, y;
int fact[MAXN], inv[MAXN], ifact[MAXN];

template <typename T, typename T1> T mod(T x, T1 p) {
  x %= p;
  if (x < 0)
    x += p;
  return x;
}

template <typename T> T inverse(T x, T p) {
  x = mod(x, p);
  if (x == 1)
    return x;
  return mod((1LL * (-p / x) * (inv[p % x] % p)), p);
  // Since inverse of p % x is already calculated.
}

int NcR(int n, int r) {
  int ret = (1LL * ifact[n - r] * ifact[r]) % MOD;
  ret = (1LL * ret * fact[n]) % MOD;
  return ret;
}

int main() {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = 1LL * fact[i - 1] * i % MOD;
  }

  ifact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    inv[i] = inverse(i, MOD);
    ifact[i] = 1LL * ifact[i - 1] * inv[i] % MOD;
  }

  cin >> x >> y;
  if ((2 * x - y) % 3)
    RETURN(0);
  if ((2 * y - x) % 3)
    RETURN(0);

  int a = (2 * x - y) / 3;
  int b = (2 * y - x) / 3;

  if (a < 0 || b < 0)
    RETURN(0);
  cout << NcR(a + b, a) << endl;
  return 0;
}