Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/11/20 21:59:34
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, T MOD> class modular {
private:
  T val_;

public:
  constexpr modular() : val_(0) {}
  constexpr modular(T v) : val_(v % MOD) {}
  constexpr modular &operator=(T v) {
    val_ = v % MOD;
    return *this;
  }

  constexpr T val() const { return val_; }
  constexpr modular operator+(modular x) const {
    return (val() + x.val()) % MOD;
  }
  constexpr modular operator+=(modular x) { return *this = *this + x; }
  constexpr modular operator++() { return *this += 1; }
  constexpr modular operator++(int) {
    modular c = *this;
    ++(*this);
    return c;
  }
  constexpr modular operator-(modular x) const {
    return (val() + MOD - x.val()) % MOD;
  }
  constexpr modular operator-=(modular x) { return *this = *this - x; }
  constexpr modular operator--() { return *this -= 1; }
  constexpr modular operator--(int) {
    modular c = *this;
    --(*this);
    return c;
  }
  constexpr modular operator*(modular x) const {
    return (val() * x.val()) % MOD;
  }
  constexpr modular operator*=(modular x) { return *this = *this * x; }
  friend constexpr modular pow(modular x, T p) {
    modular t = 1;
    modular u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modular operator/(modular x) const {
    return *this * pow(modular{x}, MOD - 2);
  }
  constexpr modular operator/=(modular x) { return *this = *this / x; }

  constexpr bool operator==(modular x) const { return val() == x.val(); }
  constexpr bool operator!=(modular x) const { return val() != x.val(); }
  constexpr bool operator<(modular x) const { return val() < x.val(); };
  constexpr bool operator<=(modular x) const { return val() <= x.val(); };
  constexpr bool operator>(modular x) const { return val() > x.val(); };
  constexpr bool operator>=(modular x) const { return val() >= x.val(); };

  friend std::ostream &operator<<(std::ostream &os, const modular &x) {
    return os << x.val();
  }
  friend std::istream &operator>>(std::istream &is, modular &x) {
    return is >> x.val_;
  }
};

constexpr unsigned long long MOD = 1e9 + 7;
using mint = modular<unsigned long long, MOD>;
constexpr int MAX = 7e5;

mint fact_[MAX + 1];
mint ifact_[MAX + 1];
void init() {
  fact_[0] = 1;
  for (size_t i = 1; i <= MAX; ++i) {
    fact_[i] = fact_[i - 1] * i;
  }
  ifact_[MAX] = mint(1) / fact_[MAX];
  for (int i = MAX - 1; i >= 0; i--) {
    ifact_[i] = ifact_[i + 1] * (i + 1);
  }
}

mint cmb(int p, int q) { return fact_[p] * ifact_[q] * ifact_[p - q]; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int x, y;
  cin >> x >> y;
  mint ans;
  if ((x + y) % 3 != 0 or (2 * x - y) % 3 != 0 or (2 * y - x) % 3 != 0) {
    ans = 0;
  } else {
    init();
    ans = cmb((x + y) / 3, (2 * x - y) / 3);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/11/20 21:59:34
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, T MOD> class modular {
private:
  T val_;

public:
  constexpr modular() : val_(0) {}
  constexpr modular(T v) : val_(v % MOD) {}
  constexpr modular &operator=(T v) {
    val_ = v % MOD;
    return *this;
  }

  constexpr T val() const { return val_; }
  constexpr modular operator+(modular x) const {
    return (val() + x.val()) % MOD;
  }
  constexpr modular operator+=(modular x) { return *this = *this + x; }
  constexpr modular operator++() { return *this += 1; }
  constexpr modular operator++(int) {
    modular c = *this;
    ++(*this);
    return c;
  }
  constexpr modular operator-(modular x) const {
    return (val() + MOD - x.val()) % MOD;
  }
  constexpr modular operator-=(modular x) { return *this = *this - x; }
  constexpr modular operator--() { return *this -= 1; }
  constexpr modular operator--(int) {
    modular c = *this;
    --(*this);
    return c;
  }
  constexpr modular operator*(modular x) const {
    return (val() * x.val()) % MOD;
  }
  constexpr modular operator*=(modular x) { return *this = *this * x; }
  friend constexpr modular pow(modular x, T p) {
    modular t = 1;
    modular u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modular operator/(modular x) const {
    return *this * pow(modular{x}, MOD - 2);
  }
  constexpr modular operator/=(modular x) { return *this = *this / x; }

  constexpr bool operator==(modular x) const { return val() == x.val(); }
  constexpr bool operator!=(modular x) const { return val() != x.val(); }
  constexpr bool operator<(modular x) const { return val() < x.val(); };
  constexpr bool operator<=(modular x) const { return val() <= x.val(); };
  constexpr bool operator>(modular x) const { return val() > x.val(); };
  constexpr bool operator>=(modular x) const { return val() >= x.val(); };

  friend std::ostream &operator<<(std::ostream &os, const modular &x) {
    return os << x.val();
  }
  friend std::istream &operator>>(std::istream &is, modular &x) {
    return is >> x.val_;
  }
};

constexpr unsigned long long MOD = 1e9 + 7;
using mint = modular<unsigned long long, MOD>;
constexpr int MAX = 7e5;

mint fact_[MAX + 1];
mint ifact_[MAX + 1];
void init() {
  fact_[0] = 1;
  for (size_t i = 1; i <= MAX; ++i) {
    fact_[i] = fact_[i - 1] * i;
  }
  ifact_[MAX] = mint(1) / fact_[MAX];
  for (int i = MAX - 1; i >= 0; i--) {
    ifact_[i] = ifact_[i + 1] * (i + 1);
  }
}

mint cmb(int p, int q) { return fact_[p] * ifact_[q] * ifact_[p - q]; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int x, y;
  cin >> x >> y;
  mint ans;
  if ((x + y) % 3 != 0 or 2 * x < y or 2 * y < x or (2 * x - y) % 3 != 0 or
      (2 * y - x) % 3 != 0) {
    ans = 0;
  } else {
    init();
    ans = cmb((x + y) / 3, (2 * x - y) / 3);
  }
  cout << ans << endl;

  return 0;
}