Problem p02862 (C++)

Buggy submission (status: Runtime Error):
/*author  ADITYA BANSAL
         //~ MNNIT ALLAHABAD     */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll power(ll, ll);
ll ncr(ll, ll);
ll add(ll a, ll b);
void fact();
ll mul(ll a, ll b);
#define watch(x) cerr << (#x) << "is" << x << nl;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi(a, b) for (ll i = a; i < b; i++)
#define fk(a, b) for (ll k = a; k < b; k++)
#define fri(a, b) for (ll i = a; i >= b; i--)
#define fj(a, b) for (ll j = a; j < b; j++)
#define ffi(a, b) for (ll i = a; i * i <= b; i++)
#define mp make_pair
#define ff first
#define tc                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define all(v) (v).begin(), (v).end()
#define ss second
#define pb push_back
#define mem(v, a) memset((v), a, sizeof(v));
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define sll set<ll>
#define vll vector<ll>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define nl endl
int mod = 1e9 + 7;
const ll N = 1000032LL;
ll fac[N + 2];
ll inv[N + 2];

signed main() {
  fast fact();
  //~ tc
  {
    ll x, y;
    cin >> x >> y;

    if ((x + y) % 3 != 0)
      return cout << 0, 0;

    ll p = (x + y) / 3;
    ll q = min(x, y);

    ll ans = ncr(p, q - p);
    cout << ans << nl;
  }
  return 0;
}

/******************************************************************************/

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
  return a;
}

ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = mul(res, a);
    n >>= 1;
    a = mul(a, a);
  }
  return res;
}
void fact() {
  inv[0] = fac[0] = 1;
  fi(1, N) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = power(fac[i], mod - 2);
  }
}

ll ncr(ll n, ll r) {

  if (r > n)
    return 0;
  ll e = fac[n];
  e = mul(e, inv[r]);
  e = mul(e, inv[n - r]);
  return e;
}


Fixed submission:
/*author  ADITYA BANSAL
         //~ MNNIT ALLAHABAD     */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll power(ll, ll);
ll ncr(ll, ll);
ll add(ll a, ll b);
void fact();
ll mul(ll a, ll b);
#define watch(x) cerr << (#x) << "is" << x << nl;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi(a, b) for (ll i = a; i < b; i++)
#define fk(a, b) for (ll k = a; k < b; k++)
#define fri(a, b) for (ll i = a; i >= b; i--)
#define fj(a, b) for (ll j = a; j < b; j++)
#define ffi(a, b) for (ll i = a; i * i <= b; i++)
#define mp make_pair
#define ff first
#define tc                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define all(v) (v).begin(), (v).end()
#define ss second
#define pb push_back
#define mem(v, a) memset((v), a, sizeof(v));
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define sll set<ll>
#define vll vector<ll>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define nl endl
int mod = 1e9 + 7;
const ll N = 1000032LL;
ll fac[N + 2];
ll inv[N + 2];

signed main() {
  fast fact();
  //~ tc
  {
    ll x, y;
    cin >> x >> y;

    if ((x + y) % 3 != 0)
      return cout << 0, 0;

    ll p = (x + y) / 3;
    ll q = min(x, y);
    if (q - p < 0)
      return cout << 0, 0;
    ll ans = ncr(p, q - p);
    cout << ans << nl;
  }
  return 0;
}

/******************************************************************************/

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
  return a;
}

ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll power(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = mul(res, a);
    n >>= 1;
    a = mul(a, a);
  }
  return res;
}
void fact() {
  inv[0] = fac[0] = 1;
  fi(1, N) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = power(fac[i], mod - 2);
  }
}

ll ncr(ll n, ll r) {

  if (r > n)
    return 0;
  ll e = fac[n];
  e = mul(e, inv[r]);
  e = mul(e, inv[n - r]);
  return e;
}