Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll int64_t
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
#define Array vec<ll>
using namespace std;

bool compare_by_b(pair<string, ll> a, pair<string, ll> b) {
  if (a.second != b.second)
    return a.second < b.second;
  else
    return a.first < b.first;
}

bool my_compare(pair<string, ll> a, pair<string, ll> b) {

  if (a.first != b.first)
    return a.first < b.first;
  if (a.second != b.second)
    return a.second > b.second;
  else
    return true;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n)(x *= i) %= mod;
  return x;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// 累乗だってさ
ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll combup(ll n, ll r) {
  ll sum = 1;
  rrep(i, min(r, n - r))(sum *= (n - i + 1)) %= mod;
  return sum;
}
// 逆元だってさ
ll modinv(ll a, ll mod1) { return modpow(a, mod1 - 2, mod1); }

const ll MAX = 51000000;
const ll MOD = 1000000007;

ll 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;
  }
}

// 二項係数計算
ll 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;
}

void solve() {}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y;
  ll left1 = 0, right1 = 0;
  cin >> x >> y;
  ll a = min(x, y), b = max(x, y);
  while (a > 0 && b > 0) {
    if (a > b) {
      b -= 1;
      a -= 2;
      right1++;
    } else if (a <= b) {
      b -= 2;
      a -= 1;
      left1++;
    }

    if ((a == 0 && b != 0) || (a != 0 && b == 0)) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll n = (right1 + left1);
  COMinit();
  cout << COM(n, right1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll int64_t
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = 1; i <= n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
#define vec vector
#define dump(x) cerr << #x << "=" << x << endl
#define Array vec<ll>
using namespace std;

bool compare_by_b(pair<string, ll> a, pair<string, ll> b) {
  if (a.second != b.second)
    return a.second < b.second;
  else
    return a.first < b.first;
}

bool my_compare(pair<string, ll> a, pair<string, ll> b) {

  if (a.first != b.first)
    return a.first < b.first;
  if (a.second != b.second)
    return a.second > b.second;
  else
    return true;
}

ll factorial(ll n) {
  ll x = 1;
  rrep(i, n)(x *= i) %= mod;
  return x;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// 累乗だってさ
ll modpow(ll a, ll n, ll mod1) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod1;
    a = a * a % mod1;
    n >>= 1;
  }
  return res;
}

ll combup(ll n, ll r) {
  ll sum = 1;
  rrep(i, min(r, n - r))(sum *= (n - i + 1)) %= mod;
  return sum;
}
// 逆元だってさ
ll modinv(ll a, ll mod1) { return modpow(a, mod1 - 2, mod1); }

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll 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;
  }
}

// 二項係数計算
ll 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;
}

void solve() {}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll x, y;
  ll left1 = 0, right1 = 0;
  cin >> x >> y;
  ll a = min(x, y), b = max(x, y);
  while (a > 0 && b > 0) {
    if (a > b) {
      b -= 1;
      a -= 2;
      right1++;
    } else if (a <= b) {
      b -= 2;
      a -= 1;
      left1++;
    }

    if ((a == 0 && b != 0) || (a != 0 && b == 0)) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll n = (right1 + left1);
  COMinit();
  cout << COM(n, right1) << endl;
  return 0;
}