Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

const long long MOD = 1000000007LL;
const long long INF = 1e18;

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp, long long mod = MOD) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % mod;
    base = base * base % mod;
  }
  return tot;
}
long long slow_mult(long long base, long long exp, long long mod = MOD) {
  long long tot = 0;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = (tot + base) % mod;
    base = base * 2 % mod;
  }
  return tot;
}

struct chash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x8e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf54876d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<uint64_t, uint64_t> x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x.first + FIXED_RANDOM) ^
           (splitmix64(x.second + FIXED_RANDOM) >> 1);
  }
};

long long fac[3000006];

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int x, y;
  cin >> x >> y;

  fac[1] = fac[0] = 1;
  for (int i = 2; i <= 3000003; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  long long tot = 0;
  for (int j, i = 0; i <= x; ++i) {
    j = x - 2 * i;
    if (i + j * 2 == y) {
      // choose(n + k, k)
      tot += fac[i + j] * fast_exp(fac[j], MOD - 2) % MOD *
             fast_exp(fac[i], MOD - 2) % MOD;
    }
  }

  cout << tot;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

const long long MOD = 1000000007LL;
const long long INF = 1e18;

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp, long long mod = MOD) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % mod;
    base = base * base % mod;
  }
  return tot;
}
long long slow_mult(long long base, long long exp, long long mod = MOD) {
  long long tot = 0;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = (tot + base) % mod;
    base = base * 2 % mod;
  }
  return tot;
}

struct chash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x8e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf54876d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<uint64_t, uint64_t> x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x.first + FIXED_RANDOM) ^
           (splitmix64(x.second + FIXED_RANDOM) >> 1);
  }
};

long long fac[3000006];

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int x, y;
  cin >> x >> y;

  fac[1] = fac[0] = 1;
  for (int i = 2; i <= 3000003; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  long long tot = 0;
  for (int j, i = 0; i <= x; ++i) {
    j = x - 2 * i;
    if (i + j * 2 == y && j >= 0 && i >= 0) {
      // choose(n + k, k)
      tot += fac[i + j] * fast_exp(fac[j], MOD - 2) % MOD *
             fast_exp(fac[i], MOD - 2) % MOD;
    }
  }

  cout << tot;

  return 0;
}