Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using std::bitset;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reps(i, s, e) for (int i = s; i < (int)e; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template <class T> using Graph = vector<vector<T>>;

void printFixedDouble(double d, int fix) {
  cout << std::fixed;
  cout << std::setprecision(fix) << d << endl;
}

// #include <iostream>

// modint: mod 計算を int を扱うように扱える構造体
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;
      std::swap(a, b);
      u -= t * v;
      std::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 std::ostream &operator<<(std::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;
  }
};

const int MOD = 1000000007;

int main() {
  int X, Y;
  cin >> X >> Y;

  if ((2 * Y - X) % 3 != 0 || (2 * X - Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int W = (2 * Y - X) / 3;
  int H = (2 * X - Y) / 3;
  vector<Fp<MOD>> factorial;
  factorial.push_back(Fp<MOD>(1));
  reps(i, 1, W + H + 1) { factorial.push_back(factorial[i - 1] * Fp<MOD>(i)); }

  cout << factorial[W + H] / factorial[W] / factorial[H] << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using std::bitset;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reps(i, s, e) for (int i = s; i < (int)e; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template <class T> using Graph = vector<vector<T>>;

void printFixedDouble(double d, int fix) {
  cout << std::fixed;
  cout << std::setprecision(fix) << d << endl;
}

// #include <iostream>

// modint: mod 計算を int を扱うように扱える構造体
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;
      std::swap(a, b);
      u -= t * v;
      std::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 std::ostream &operator<<(std::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;
  }
};

const int MOD = 1000000007;

int main() {
  int X, Y;
  cin >> X >> Y;

  if ((2 * Y - X) % 3 != 0 || (2 * X - Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  int W = (2 * Y - X) / 3;
  int H = (2 * X - Y) / 3;
  if (W < 0 || H < 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<Fp<MOD>> factorial;
  factorial.push_back(Fp<MOD>(1));
  reps(i, 1, W + H + 1) { factorial.push_back(factorial[i - 1] * Fp<MOD>(i)); }

  cout << factorial[W + H] / factorial[W] / factorial[H] << endl;
}