Problem p02862 (C++)

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

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \
  vec var(n);                                                                  \
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \
  vecll var(n);                                                                \
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \
  vecst var(n);                                                                \
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)                                                              \
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); }
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//------------------------------------------------------------------
template <unsigned int mod_> struct ModInt {
  using uint = unsigned int;
  using ll = long long;
  using ull = unsigned long long;

  constexpr static uint mod = mod_;

  uint v;
  ModInt() : v(0) {}
  ModInt(ll _v) : v(normS(_v % mod + mod)) {}
  explicit operator bool() const { return v != 0; }
  static uint normS(const uint &x) {
    return (x < mod) ? x : x - mod;
  } // [0 , 2*mod-1] -> [0 , mod-1]
  static ModInt make(const uint &x) {
    ModInt m;
    m.v = x;
    return m;
  }
  ModInt operator+(const ModInt &b) const { return make(normS(v + b.v)); }
  ModInt operator-(const ModInt &b) const { return make(normS(v + mod - b.v)); }
  ModInt operator-() const { return make(normS(mod - v)); }
  ModInt operator*(const ModInt &b) const { return make((ull)v * b.v % mod); }
  ModInt operator/(const ModInt &b) const { return *this * b.inv(); }
  ModInt &operator+=(const ModInt &b) { return *this = *this + b; }
  ModInt &operator-=(const ModInt &b) { return *this = *this - b; }
  ModInt &operator*=(const ModInt &b) { return *this = *this * b; }
  ModInt &operator/=(const ModInt &b) { return *this = *this / b; }
  ModInt &operator++(int) { return *this = *this + 1; }
  ModInt &operator--(int) { return *this = *this - 1; }
  ll extgcd(ll a, ll b, ll &x, ll &y) const {
    ll p[] = {a, 1, 0}, q[] = {b, 0, 1};
    while (*q) {
      ll t = *p / *q;
      rep(i, 3) swap(p[i] -= t * q[i], q[i]);
    }
    if (p[0] < 0)
      rep(i, 3) p[i] = -p[i];
    x = p[1], y = p[2];
    return p[0];
  }
  ModInt inv() const {
    ll x, y;
    extgcd(v, mod, x, y);
    return make(normS(x + mod));
  }
  ModInt pow(ll p) const {
    if (p < 0)
      return inv().pow(-p);
    ModInt a = 1;
    ModInt x = *this;
    while (p) {
      if (p & 1)
        a *= x;
      x *= x;
      p >>= 1;
    }
    return a;
  }
  bool operator==(const ModInt &b) const { return v == b.v; }
  bool operator!=(const ModInt &b) const { return v != b.v; }
  friend istream &operator>>(istream &o, ModInt &x) {
    ll tmp;
    o >> tmp;
    x = ModInt(tmp);
    return o;
  }
  friend ostream &operator<<(ostream &o, const ModInt &x) { return o << x.v; }
};
using mint = ModInt<1000000007>;

const int MAX = 400000;
mint fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i;
    inv[i] = (mint)1 / i;
    finv[i] = finv[i - 1] * inv[i];
  }
}

mint COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * finv[k] * finv[n - k];
}

int main(void) {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    prt(0);
    return 0;
  }
  ll m = (2 * x - y) / 3;
  ll n = (2 * y - x) / 3;
  if (m < 0 || n < 0) {
    prt(0);
    return 0;
  }
  COMinit();
  prt(COM(m + n, n));
}


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

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \
  vec var(n);                                                                  \
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \
  vecll var(n);                                                                \
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \
  vecst var(n);                                                                \
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)                                                              \
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\n"); }
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//------------------------------------------------------------------
template <unsigned int mod_> struct ModInt {
  using uint = unsigned int;
  using ll = long long;
  using ull = unsigned long long;

  constexpr static uint mod = mod_;

  uint v;
  ModInt() : v(0) {}
  ModInt(ll _v) : v(normS(_v % mod + mod)) {}
  explicit operator bool() const { return v != 0; }
  static uint normS(const uint &x) {
    return (x < mod) ? x : x - mod;
  } // [0 , 2*mod-1] -> [0 , mod-1]
  static ModInt make(const uint &x) {
    ModInt m;
    m.v = x;
    return m;
  }
  ModInt operator+(const ModInt &b) const { return make(normS(v + b.v)); }
  ModInt operator-(const ModInt &b) const { return make(normS(v + mod - b.v)); }
  ModInt operator-() const { return make(normS(mod - v)); }
  ModInt operator*(const ModInt &b) const { return make((ull)v * b.v % mod); }
  ModInt operator/(const ModInt &b) const { return *this * b.inv(); }
  ModInt &operator+=(const ModInt &b) { return *this = *this + b; }
  ModInt &operator-=(const ModInt &b) { return *this = *this - b; }
  ModInt &operator*=(const ModInt &b) { return *this = *this * b; }
  ModInt &operator/=(const ModInt &b) { return *this = *this / b; }
  ModInt &operator++(int) { return *this = *this + 1; }
  ModInt &operator--(int) { return *this = *this - 1; }
  ll extgcd(ll a, ll b, ll &x, ll &y) const {
    ll p[] = {a, 1, 0}, q[] = {b, 0, 1};
    while (*q) {
      ll t = *p / *q;
      rep(i, 3) swap(p[i] -= t * q[i], q[i]);
    }
    if (p[0] < 0)
      rep(i, 3) p[i] = -p[i];
    x = p[1], y = p[2];
    return p[0];
  }
  ModInt inv() const {
    ll x, y;
    extgcd(v, mod, x, y);
    return make(normS(x + mod));
  }
  ModInt pow(ll p) const {
    if (p < 0)
      return inv().pow(-p);
    ModInt a = 1;
    ModInt x = *this;
    while (p) {
      if (p & 1)
        a *= x;
      x *= x;
      p >>= 1;
    }
    return a;
  }
  bool operator==(const ModInt &b) const { return v == b.v; }
  bool operator!=(const ModInt &b) const { return v != b.v; }
  friend istream &operator>>(istream &o, ModInt &x) {
    ll tmp;
    o >> tmp;
    x = ModInt(tmp);
    return o;
  }
  friend ostream &operator<<(ostream &o, const ModInt &x) { return o << x.v; }
};
using mint = ModInt<1000000007>;

const int MAX = 700000;
mint fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i;
    inv[i] = (mint)1 / i;
    finv[i] = finv[i - 1] * inv[i];
  }
}

mint COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * finv[k] * finv[n - k];
}

int main(void) {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0) {
    prt(0);
    return 0;
  }
  ll m = (2 * x - y) / 3;
  ll n = (2 * y - x) / 3;
  if (m < 0 || n < 0) {
    prt(0);
    return 0;
  }
  COMinit();
  prt(COM(m + n, n));
}