Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

const int MAX = 1000000;

vector<long long> fac(MAX + 1), inverseFac(MAX + 1);

long long modPow(long long x, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

long long comb(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = inverseFac[a - b] * inverseFac[b] % MOD;
  return tmp * fac[a] % MOD;
}

long long perm(long long a, long long b) {
  if (a < b)
    return 0;
  long long tmp = inverseFac[a - b] % MOD;
  return tmp * fac[a] % MOD;
}

long long dupc(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < 0 || b == 0)
    return 0;
  long long tmp = inverseFac[a - 1] * inverseFac[b] % MOD;
  return tmp * fac[a + b - 1] % MOD;
}

void init() {
  fac[0] = 1;
  inverseFac[0] = 1;
  for (long long i = 0; i < MAX; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    inverseFac[i + 1] = inverseFac[i] * modPow(i + 1, MOD - 2) % MOD;
  }
}

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }

  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

using mint = Fp<MOD>;

int main() {
  long long X, Y;
  cin >> X >> Y;
  if ((2 * X - Y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    init();
    long long y = (2 * X - Y) / 3;
    long long x = X - 2 * y;
    mint ans = comb(x + y, x);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

const int MAX = 1000000;

vector<long long> fac(MAX + 1), inverseFac(MAX + 1);

long long modPow(long long x, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

long long comb(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  long long tmp = inverseFac[a - b] * inverseFac[b] % MOD;
  return tmp * fac[a] % MOD;
}

long long perm(long long a, long long b) {
  if (a < b)
    return 0;
  long long tmp = inverseFac[a - b] % MOD;
  return tmp * fac[a] % MOD;
}

long long dupc(long long a, long long b) {
  if (a == 0 && b == 0)
    return 1;
  if (a < 0 || b == 0)
    return 0;
  long long tmp = inverseFac[a - 1] * inverseFac[b] % MOD;
  return tmp * fac[a + b - 1] % MOD;
}

void init() {
  fac[0] = 1;
  inverseFac[0] = 1;
  for (long long i = 0; i < MAX; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;
    inverseFac[i + 1] = inverseFac[i] * modPow(i + 1, MOD - 2) % MOD;
  }
}

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }

  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

using mint = Fp<MOD>;

int main() {
  long long X, Y;
  cin >> X >> Y;
  if ((2 * X - Y) % 3 != 0) {
    cout << 0 << endl;
  } else {
    init();
    long long y = (2 * X - Y) / 3;
    long long x = X - 2 * y;

    if (x < 0 || y < 0) {
      cout << 0 << endl;
      return 0;
    }

    mint ans = comb(x + y, x);
    cout << ans << endl;
  }
  return 0;
}