Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fac[2000005];
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 2000005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
signed main() {

  senky_bansal IIIT_ALLAHABAD

      ll t;
  // cin>>t;
  t = 1;
  pre();
  while (t--) {
    ll x, y;
    cin >> x >> y;
    ll mx = max(x, y);
    ll mn = min(x, y);
    if (y > x)
      swap(x, y);
    if (x >= y) {
      ll diff = x - y;
      ll f = 0;
      if ((diff + x) % 3 != 0)
        f = 1;
      if ((2 * diff + y) % 3 != 0)
        f = 1;
      ll ans1 = (diff + x) / 3;
      ll ans2 = (2 * diff + y) / 3;
      ans2 -= diff;
      ll ans = 1;
      // cout<<ans1<<" "<<ans2<<endl;
      ans = ncr((ans1 + ans2), ans1);
      if (f)
        cout << "0" << endl;
      else
        cout << (ans + mod) % mod << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fac[2000005];
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 2000005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
signed main() {

  senky_bansal IIIT_ALLAHABAD

      ll t;
  // cin>>t;
  t = 1;
  pre();
  while (t--) {
    ll x, y;
    cin >> x >> y;
    ll mx = max(x, y);
    ll mn = min(x, y);
    if (y > x)
      swap(x, y);
    if (x >= y) {
      ll diff = x - y;
      ll f = 0;
      if ((diff + x) % 3 != 0)
        f = 1;
      if ((2 * diff + y) % 3 != 0)
        f = 1;
      ll ans1 = (diff + x) / 3;
      ll ans2 = (2 * diff + y) / 3;
      ans2 -= diff;
      if (ans2 < 0)
        f = 1;
      ll ans = 1;
      // cout<<ans1<<" "<<ans2<<endl;
      ans = ncr((ans1 + ans2), ans1);
      if (f)
        cout << "0" << endl;
      else
        cout << (ans + mod) % mod << endl;
    }
  }
}