Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define rrep(i, a, n) for (ll i = n; i >= a; i--)
#define index_check(i, n)                                                      \
  if (i > n - 1 || i < 0)                                                      \
    continue;
#define LINF 1e18
#define INF 1e9
using namespace std;

#define fs first
#define sc second

using P = pair<ll, ll>;
using Pll = pair<P, ll>;
using PQ = priority_queue<P, vector<P>, greater<P>>;

const ll MOD = 1e9 + 7;

template <ll MOD> struct modint {
  ll x;
  modint() : x(0) {}
  modint(ll a) : x((a % MOD + MOD) % MOD) {}

  bool operator!() const { return !x; }

  bool operator<(const modint &a) const {
    modint res(*this);
    return res.x < a.x;
  }

  bool operator>(const modint &a) const {
    modint res(*this);
    return res.x > a.x;
  }

  modint &operator+=(const modint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  modint &operator-=(const modint &a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  modint &operator*=(const modint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }

  modint &operator/=(const modint &a) {
    *this *= a.inv();
    return *this;
  }

  modint &operator%=(const modint &a) {
    x %= a.x;
    return *this;
  }

  modint operator-() const { return modint(-x); }

  modint operator+(const modint &a) const { return modint(*this) += a; }
  modint operator-(const modint &a) const { return modint(*this) -= a; }
  modint operator*(const modint &a) const { return modint(*this) *= a; }
  modint operator/(const modint &a) const { return modint(*this) /= a; }
  modint operator%(const modint &a) const { return modint(*this) %= a; }
  bool operator==(const modint &a) const { return x == a.x; }
  bool operator!=(const modint &a) const { return x != a.x; }

  modint pow(ll n) const {
    modint res(1), a(x);
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n /= 2;
    }
    return res;
  }

  modint inv() const { return this->pow(MOD - 2); }

  friend ostream &operator<<(ostream &os, const modint &m) { return os << m.x; }

  friend istream &operator>>(istream &is, modint &m) {
    ll t;
    is >> t;
    m = modint<MOD>(t);
    return (is);
  }

  static ll get_mod() { return MOD; }
};
using mint = modint<MOD>;

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    cout << 0 << endl;
  else {
    ll a = (x + y) / 3;
    ll b = (2 * x - y) / 3;
    mint fact[max(a, b) + 1];
    fact[0] = 1;
    fact[1] = 1;
    ll temp = 2;
    while (temp <= max(a, b)) {
      fact[temp] = fact[temp - 1] * temp;
      temp++;
    }
    if (a - b >= 0)
      cout << fact[a] * fact[b].inv() * fact[a - b].inv() << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define rrep(i, a, n) for (ll i = n; i >= a; i--)
#define index_check(i, n)                                                      \
  if (i > n - 1 || i < 0)                                                      \
    continue;
#define LINF 1e18
#define INF 1e9
using namespace std;

#define fs first
#define sc second

using P = pair<ll, ll>;
using Pll = pair<P, ll>;
using PQ = priority_queue<P, vector<P>, greater<P>>;

const ll MOD = 1e9 + 7;

template <ll MOD> struct modint {
  ll x;
  modint() : x(0) {}
  modint(ll a) : x((a % MOD + MOD) % MOD) {}

  bool operator!() const { return !x; }

  bool operator<(const modint &a) const {
    modint res(*this);
    return res.x < a.x;
  }

  bool operator>(const modint &a) const {
    modint res(*this);
    return res.x > a.x;
  }

  modint &operator+=(const modint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  modint &operator-=(const modint &a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  modint &operator*=(const modint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }

  modint &operator/=(const modint &a) {
    *this *= a.inv();
    return *this;
  }

  modint &operator%=(const modint &a) {
    x %= a.x;
    return *this;
  }

  modint operator-() const { return modint(-x); }

  modint operator+(const modint &a) const { return modint(*this) += a; }
  modint operator-(const modint &a) const { return modint(*this) -= a; }
  modint operator*(const modint &a) const { return modint(*this) *= a; }
  modint operator/(const modint &a) const { return modint(*this) /= a; }
  modint operator%(const modint &a) const { return modint(*this) %= a; }
  bool operator==(const modint &a) const { return x == a.x; }
  bool operator!=(const modint &a) const { return x != a.x; }

  modint pow(ll n) const {
    modint res(1), a(x);
    while (n > 0) {
      if (n & 1)
        res *= a;
      a *= a;
      n /= 2;
    }
    return res;
  }

  modint inv() const { return this->pow(MOD - 2); }

  friend ostream &operator<<(ostream &os, const modint &m) { return os << m.x; }

  friend istream &operator>>(istream &is, modint &m) {
    ll t;
    is >> t;
    m = modint<MOD>(t);
    return (is);
  }

  static ll get_mod() { return MOD; }
};
using mint = modint<MOD>;

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    cout << 0 << endl;
  else {
    ll a = (x + y) / 3;
    ll b = max((2 * x - y) / 3, (2 * y - x) / 3);
    mint fact[max(a, b) + 1];
    fact[0] = 1;
    fact[1] = 1;
    ll temp = 2;
    while (temp <= max(a, b)) {
      fact[temp] = fact[temp - 1] * temp;
      temp++;
    }
    if (a - b >= 0)
      cout << fact[a] * fact[b].inv() * fact[a - b].inv() << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}