Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

#define int long long

#if defined(__GNUC__)
#define __int32 __int32_t
#endif // __GNUC__

#define itn __int32
#define REP(i, n) for (itn(i) = 0; (i) < (n); (i)++)
#define REP2(i, x, n) for (itn(i) = x; (i) < (n); (i)++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SORT(x) sort(ALL(x))
#define RSORT(x, T) sort(ALL(x), greater<(T)>())
#define REVERCE(x) reverce(ALL(x))

#define INT_POW(x, y)                                                          \
  [](int a, int b) -> int {                                                    \
    int c = a;                                                                 \
    REP(i, b - 1) { c *= a; }                                                  \
    return c;                                                                  \
  }(x, y)
#define INT_POWM(x, y, m)                                                      \
  [](int a, int b, int m) -> int {                                             \
    int c = a;                                                                 \
    REP(i, b - 1) {                                                            \
      c *= a;                                                                  \
      c %= m;                                                                  \
    }                                                                          \
    return c;                                                                  \
  }(x, y, m)

const int kMax = 510000;
static int fac[kMax], finv[kMax], inv[kMax];
static int d = 1'000'000'007;

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  REP2(i, 2, kMax) {
    fac[i] = fac[i - 1] * i % d;
    inv[i] = d - inv[d % i] * (d / i) % d;
    finv[i] = finv[i - 1] * inv[i] % d;
  }
}

int COM(int n, int k) {
  if (n < k) {
    return 0;
  }
  if (n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % d) % d;
}

itn main() {
  ios::sync_with_stdio(false);

  int x, y;

  cin >> x >> y;

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

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

  int n, m;

  n = (-x + 2 * y) / 3;
  m = (2 * x - y) / 3;

  COMinit();

  cout << COM(n + m, n);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

#define int long long

#if defined(__GNUC__)
#define __int32 __int32_t
#endif // __GNUC__

#define itn __int32
#define REP(i, n) for (itn(i) = 0; (i) < (n); (i)++)
#define REP2(i, x, n) for (itn(i) = x; (i) < (n); (i)++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SORT(x) sort(ALL(x))
#define RSORT(x, T) sort(ALL(x), greater<(T)>())
#define REVERCE(x) reverce(ALL(x))

#define INT_POW(x, y)                                                          \
  [](int a, int b) -> int {                                                    \
    int c = a;                                                                 \
    REP(i, b - 1) { c *= a; }                                                  \
    return c;                                                                  \
  }(x, y)
#define INT_POWM(x, y, m)                                                      \
  [](int a, int b, int m) -> int {                                             \
    int c = a;                                                                 \
    REP(i, b - 1) {                                                            \
      c *= a;                                                                  \
      c %= m;                                                                  \
    }                                                                          \
    return c;                                                                  \
  }(x, y, m)

const int kMax = 680000;
int fac[kMax], finv[kMax], inv[kMax];
int d = 1'000'000'007;

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  REP2(i, 2, kMax) {
    fac[i] = fac[i - 1] * i % d;
    inv[i] = d - inv[d % i] * (d / i) % d;
    finv[i] = finv[i - 1] * inv[i] % d;
  }
}

int COM(int n, int k) {
  if (n < k) {
    return 0;
  }
  if (n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % d) % d;
}

itn main() {
  ios::sync_with_stdio(false);

  int x, y;

  cin >> x >> y;

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

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

  int n, m;

  n = (-x + 2 * y) / 3;
  m = (2 * x - y) / 3;

  COMinit();

  cout << COM(n + m, n);

  return 0;
}