Problem p02862 (C++)

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

struct mint {
  ll v;
  mint() : v(0) {}
  mint(ll v) : v((v % MOD + MOD) % MOD) {}
  operator ll() const { return v; }
  mint pow(ll t) const;
  mint inv() const;
};
mint &operator+=(mint &a, mint b) { return a = a.v + b.v; }
mint &operator-=(mint &a, mint b) { return a = a.v - b.v; }
mint &operator*=(mint &a, mint b) { return a = a.v * b.v; }
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return a *= b; }
mint operator-(mint a) { return (mint)0 - a; }
mint &operator/=(mint &a, mint b) { return a *= b.inv(); }
mint operator/(mint a, mint b) { return a /= b; }
mint mint::pow(ll t) const {
  if (!t)
    return 1;
  mint a = pow(t >> 1);
  a *= a;
  if (t & 1)
    a *= *this;
  return a;
}
mint mint::inv() const { return pow(MOD - 2); }
bool operator==(mint a, mint b) { return a.v == b.v; }
bool operator!=(mint a, mint b) { return a.v != b.v; }
istream &operator>>(istream &os, mint &a) { return (os >> a.v); }
ostream &operator<<(ostream &os, const mint &a) { return (os << a.v); }

mint mfact(ll n) { // n! (mod MOD)
  // mint fact = 1;
  // for (ll i = 2; i <= n; i++) fact = fact * (mint)i;
  // return fact;
  static vector<mint> fact(1, 1);
  int pn = fact.size();
  if (pn < n + 1) {
    fact.resize(n + 1);
    for (int i = pn; i <= n; i++)
      fact[i] = fact[i - 1] * (mint)i;
  }
  return fact[n];
}
mint nPk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(n - k).inv();
}
mint nCk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(k).inv() * mfact(n - k).inv();
}

void solve() {
  ll x, y;
  cin >> x >> y;
  ll a, b;
  if ((x * 2 - y) % 3 != 0 || (y * 2 - x) % 3 != 0) {
    cout << 0 << endl;
    return;
  }
  a = (x * 2 - y) / 3;
  b = (y * 2 - x) / 3;
  if (a == 0 || b == 0) {
    cout << 1 << endl;
    return;
  }
  mint ans = mfact(a + b) * mfact(a).inv() * mfact(b).inv();
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

struct mint {
  ll v;
  mint() : v(0) {}
  mint(ll v) : v((v % MOD + MOD) % MOD) {}
  operator ll() const { return v; }
  mint pow(ll t) const;
  mint inv() const;
};
mint &operator+=(mint &a, mint b) { return a = a.v + b.v; }
mint &operator-=(mint &a, mint b) { return a = a.v - b.v; }
mint &operator*=(mint &a, mint b) { return a = a.v * b.v; }
mint operator+(mint a, mint b) { return a += b; }
mint operator-(mint a, mint b) { return a -= b; }
mint operator*(mint a, mint b) { return a *= b; }
mint operator-(mint a) { return (mint)0 - a; }
mint &operator/=(mint &a, mint b) { return a *= b.inv(); }
mint operator/(mint a, mint b) { return a /= b; }
mint mint::pow(ll t) const {
  if (!t)
    return 1;
  mint a = pow(t >> 1);
  a *= a;
  if (t & 1)
    a *= *this;
  return a;
}
mint mint::inv() const { return pow(MOD - 2); }
bool operator==(mint a, mint b) { return a.v == b.v; }
bool operator!=(mint a, mint b) { return a.v != b.v; }
istream &operator>>(istream &os, mint &a) { return (os >> a.v); }
ostream &operator<<(ostream &os, const mint &a) { return (os << a.v); }

mint mfact(ll n) { // n! (mod MOD)
  // mint fact = 1;
  // for (ll i = 2; i <= n; i++) fact = fact * (mint)i;
  // return fact;
  static vector<mint> fact(1, 1);
  int pn = fact.size();
  if (pn < n + 1) {
    fact.resize(n + 1);
    for (int i = pn; i <= n; i++)
      fact[i] = fact[i - 1] * (mint)i;
  }
  return fact[n];
}
mint nPk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(n - k).inv();
}
mint nCk(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (k == 0)
    return 1;
  return mfact(n) * mfact(k).inv() * mfact(n - k).inv();
}

void solve() {
  ll x, y;
  cin >> x >> y;
  ll a, b;
  if ((x * 2 - y) % 3 != 0 || (y * 2 - x) % 3 != 0) {
    cout << 0 << endl;
    return;
  }
  a = (x * 2 - y) / 3;
  b = (y * 2 - x) / 3;
  if (a < 0 || b < 0) {
    cout << 0 << endl;
    return;
  }
  if (a == 0 || b == 0) {
    cout << 1 << endl;
    return;
  }
  mint ans = mfact(a + b) * mfact(a).inv() * mfact(b).inv();
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}