Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
typedef pair<pair<lint, int>, pair<int, int>> ppp;
typedef complex<double> xy_t;
typedef vector<lint> poly;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const lint mod = 1e9 + 7;
const lint INF = mod;
const int MAX = 100010;

lint powmod(lint x, lint n) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x, ret %= mod, n -= 1;
    else
      x *= x, x %= mod, n >>= 1;
  }
  return ret;
}

vector<lint> fact;
vector<lint> revfact;

void setfact(int n) {
  fact.resize(n + 1);
  revfact.resize(n + 1);
  fact[0] = 1;
  rep(i, n) fact[i + 1] = fact[i] * (i + 1) % mod;

  revfact[n] = powmod(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    revfact[i] = revfact[i + 1] * (i + 1) % mod;
}

lint getC(int n, int r) {
  return fact[n] * revfact[r] % mod * revfact[n - r] % mod;
}

int main() {
  int x, y;
  scanf("%d%d", &x, &y);
  int n = -x + 2 * y, m = 2 * x - y;
  if (n < 0 || y < 0 || n % 3 || m % 3) {
    printf("%d\n", 0);
    return 0;
  }
  n /= 3;
  m /= 3;
  setfact(2000010);
  printf("%lld\n", getC(n + m, n));
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
typedef pair<pair<lint, int>, pair<int, int>> ppp;
typedef complex<double> xy_t;
typedef vector<lint> poly;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const lint mod = 1e9 + 7;
const lint INF = mod;
const int MAX = 100010;

lint powmod(lint x, lint n) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x, ret %= mod, n -= 1;
    else
      x *= x, x %= mod, n >>= 1;
  }
  return ret;
}

vector<lint> fact;
vector<lint> revfact;

void setfact(int n) {
  fact.resize(n + 1);
  revfact.resize(n + 1);
  fact[0] = 1;
  rep(i, n) fact[i + 1] = fact[i] * (i + 1) % mod;

  revfact[n] = powmod(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    revfact[i] = revfact[i + 1] * (i + 1) % mod;
}

lint getC(int n, int r) {
  return fact[n] * revfact[r] % mod * revfact[n - r] % mod;
}

int main() {
  int x, y;
  scanf("%d%d", &x, &y);
  int n = -x + 2 * y, m = 2 * x - y;
  if (n < 0 || m < 0 || n % 3 || m % 3) {
    printf("%d\n", 0);
    return 0;
  }
  n /= 3;
  m /= 3;
  setfact(2000010);
  printf("%lld\n", getC(n + m, n));
}