Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define MAX_P 510000
using namespace std;

vector<ll> factn(MAX_P + 1);
// n! = factn[i]
void fact(ll n) {
  factn[1] = 1;
  for (ll i = 2; i <= n; i++) {
    factn[i] = factn[i - 1] * i % MOD;
  }
  return;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
ll mod_inverse(ll a, ll m) {
  ll x, y;
  x = 1;
  y = 0;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// n! mod p
ll mod_fact(ll n, ll p, ll &e) {
  e = 0;
  if (n == 0)
    return 1;

  ll res = mod_fact(n / p, p, e);
  e += n / p;

  if (n / p % 2 != 0)
    return res * (p - factn[n % p]) % p;
  return res * factn[n % p] % p;
}

// nCk %mod p
ll mod_comb(ll n, ll k, ll p) {
  if (n < 0 || k < 0 || n < k)
    return 0;

  ll e1, e2, e3;
  e1 = e2 = e3 = 0;
  ll a1 = mod_fact(n, p, e1);
  ll a2 = mod_fact(k, p, e2);
  ll a3 = mod_fact((n - k), p, e3);

  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

// X = 2a + 1b
// Y = a +2b

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll xx, yy;
  cin >> xx >> yy;
  ll mm = 0;
  ll nn = 0;
  bool flag = true;
  for (ll b = 0; 2 * b <= xx; b++) {
    ll a = xx - (2 * b);
    if (yy == (2 * a + b)) {
      nn = a;
      mm = b;
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << 0 << endl;
    return 0;
  }
  fact(MAX_P);
  ll ans = 0;
  ans = mod_comb(nn + mm, nn, MOD);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define MAX_P 1100000
using namespace std;

vector<ll> factn(MAX_P + 1);
// n! = factn[i]
void fact(ll n) {
  factn[1] = 1;
  for (ll i = 2; i <= n; i++) {
    factn[i] = factn[i - 1] * i % MOD;
  }
  return;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
ll mod_inverse(ll a, ll m) {
  ll x, y;
  x = 1;
  y = 0;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// n! mod p
ll mod_fact(ll n, ll p, ll &e) {
  e = 0;
  if (n == 0)
    return 1;

  ll res = mod_fact(n / p, p, e);
  e += n / p;

  if (n / p % 2 != 0)
    return res * (p - factn[n % p]) % p;
  return res * factn[n % p] % p;
}

// nCk %mod p
ll mod_comb(ll n, ll k, ll p) {
  if (n < 0 || k < 0 || n < k)
    return 0;

  ll e1, e2, e3;
  e1 = e2 = e3 = 0;
  ll a1 = mod_fact(n, p, e1);
  ll a2 = mod_fact(k, p, e2);
  ll a3 = mod_fact((n - k), p, e3);

  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

// X = 2a + 1b
// Y = a +2b

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll xx, yy;
  cin >> xx >> yy;
  ll mm = 0;
  ll nn = 0;
  bool flag = true;
  for (ll b = 0; 2 * b <= xx; b++) {
    ll a = xx - (2 * b);
    if (yy == (2 * a + b)) {
      nn = a;
      mm = b;
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << 0 << endl;
    return 0;
  }
  fact(MAX_P);
  ll ans = 0;
  ans = mod_comb(nn + mm, nn, MOD);
  cout << ans << endl;

  return 0;
}