Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod = 1e9 + 7;
int fac[2000005];

int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    // q is quotient
    int q = a / m;
    int t = m;

    // m is remainder now, process same as
    // Euclid's algo
    m = a % m, a = t;
    t = y;

    // Update y and x
    y = x - q * y;
    x = t;
  }

  // Make x positive
  if (x < 0)
    x += m0;

  return x;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  int k1, k2;
  cin >> k1 >> k2;
  fac[0] = 1;
  for (int i = 1; i <= 2000004; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }

  if ((2 * k1 - k2) % 3 == 0 && (2 * k2 - k1) % 3 == 0) {
    int z1 = (2 * k1 - k2) / 3;
    int z2 = (2 * k2 - k1) / 3;
    int ans = (fac[z1 + z2]);
    ans = (ans * modInverse(fac[z1], mod)) % mod;
    ans = (ans * modInverse(fac[z2], mod)) % mod;
    cout << ans;
  } else {
    cout << 0;
  }

  // -------------------------------------Code ends
  // here------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}

Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod = 1e9 + 7;
int fac[2000005];

int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    // q is quotient
    int q = a / m;
    int t = m;

    // m is remainder now, process same as
    // Euclid's algo
    m = a % m, a = t;
    t = y;

    // Update y and x
    y = x - q * y;
    x = t;
  }

  // Make x positive
  if (x < 0)
    x += m0;

  return x;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  int k1, k2;
  cin >> k1 >> k2;
  fac[0] = 1;
  for (int i = 1; i <= 2000004; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }

  if (2 * k1 < k2 || 2 * k2 < k1) {
    cout << 0;
    return 0;
  }
  if ((2 * k1 - k2) % 3 == 0 && (2 * k2 - k1) % 3 == 0) {
    int z1 = (2 * k1 - k2) / 3;
    int z2 = (2 * k2 - k1) / 3;
    int ans = (fac[z1 + z2]);
    ans = (ans * modInverse(fac[z1], mod)) % mod;
    ans = (ans * modInverse(fac[z2], mod)) % mod;
    cout << ans;
  } else {
    cout << 0;
  }

  // -------------------------------------Code ends
  // here------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}