Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// shan61916
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define vll vector<ll>
const ll MOD = (ll)(1e9 + 7);
vll fact(3000000, 0);
ll fast_exp(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return res % MOD;
}
ll modadd(ll x, ll y) { return (x % MOD + y % MOD) % MOD; }
ll modmul(ll x, ll y) { return (x % MOD * y % MOD) % MOD; }
void f() {
  fact[0] = 1;
  fact[1] = 1;
  for (ll i = 2; i < 3000000; i++)
    fact[i] = modmul(i, fact[i - 1]);
}
ll ncr(ll x, ll y) {
  ll ans = fact[x];
  ll denom = modmul(fact[y], fact[x - y]);
  denom = fast_exp(denom, MOD - 2);
  ans = modmul(ans, denom);
  return ans;
}

int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  ll x, y;
  cin >> x >> y;
  ll tot = x + y;
  if (tot % 3) {
    cout << 0 << endl;
    return 0;
  }
  f();
  tot /= 3;
  ll firch = x - tot;
  if (firch == 0) {
    cout << 1;
    return 0;
  }
  if (firch < 0) {
    cout << 0;
    return 0;
  }
  cout << ncr(tot, firch);
  return 0;
} // good night.

Fixed submission:
// shan61916
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define vll vector<ll>
const ll MOD = (ll)(1e9 + 7);
vll fact(3000000, 0);
ll fast_exp(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return res % MOD;
}
ll modadd(ll x, ll y) { return (x % MOD + y % MOD) % MOD; }
ll modmul(ll x, ll y) { return (x % MOD * y % MOD) % MOD; }
void f() {
  fact[0] = 1;
  fact[1] = 1;
  for (ll i = 2; i < 3000000; i++)
    fact[i] = modmul(i, fact[i - 1]);
}
ll ncr(ll x, ll y) {
  if (y > x)
    return 0;
  ll ans = fact[x];
  ll denom = modmul(fact[y], fact[x - y]);
  denom = fast_exp(denom, MOD - 2);
  ans = modmul(ans, denom);
  return ans;
}

int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  ll x, y;
  cin >> x >> y;
  ll tot = x + y;
  if (tot % 3) {
    cout << 0 << endl;
    return 0;
  }
  f();
  tot /= 3;
  ll firch = x - tot;
  if (firch == 0) {
    cout << 1;
    return 0;
  }
  if (firch < 0) {
    cout << 0;
    return 0;
  }
  cout << ncr(tot, firch);
  return 0;
} // good night.