Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define itn int
#define co(ans) cout << ans << endl;
#define COYE cout << "YES" << endl;
#define COYe cout << "Yes" << endl;
#define COye cout << "yes" << endl;
#define CONO cout << "NO" << endl;
#define CONo cout << "No" << endl;
#define COno cout << "no" << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define INF ((1LL << 62) - (1LL << 31))
#define MOD 1000000007
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

const ll MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

  bool CAN = true;

  COMinit();

  if (Y > 2 * X || X > 2 * Y)
    CAN = false;
  if ((X + Y) % 3 != 0)
    CAN = false;

  if (CAN) {
    co(COM((X + Y) / 3, X - (X + Y) / 3) % MOD);
    return 0;
  }

  co(0);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define itn int
#define co(ans) cout << ans << endl;
#define COYE cout << "YES" << endl;
#define COYe cout << "Yes" << endl;
#define COye cout << "yes" << endl;
#define CONO cout << "NO" << endl;
#define CONo cout << "No" << endl;
#define COno cout << "no" << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define INF ((1LL << 62) - (1LL << 31))
#define MOD 1000000007
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

const ll MAX = 5100000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

  bool CAN = true;

  COMinit();

  if (Y > 2 * X || X > 2 * Y)
    CAN = false;
  if ((X + Y) % 3 != 0)
    CAN = false;

  if (CAN) {
    co(COM((X + Y) / 3, X - (X + Y) / 3) % MOD);
    return 0;
  }

  co(0);
  return 0;
}