Problem p02782 (C++)

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

// ------------------
// Integer modulo MOD
// ------------------
const int64_t MOD = 1e9 + 7;
struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t n) const {
    if (n == 0)
      return 1;
    mint t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// ---------------------------------------
// Number of combinations (nCk) modulo MOD
// ---------------------------------------
vector<mint> fac;
vector<mint> fac_inv;
vector<mint> inv;
void init_comb(int mx) {
  fac = vector<mint>(mx + 1), fac_inv = vector<mint>(mx + 1),
  inv = vector<mint>(mx + 1);
  fac.at(0) = fac.at(1) = 1;
  fac_inv.at(0) = fac_inv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i <= mx; i++) {
    fac.at(i) = fac.at(i - 1) * i;
    inv.at(i) = -inv.at(MOD % i) * (MOD / i);
    fac_inv.at(i) = fac_inv.at(i - 1) * inv.at(i);
  }
}
mint comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * fac_inv.at(k) * fac_inv.at(n - k);
}

mint f(int r, int c) { return comb(r + c, c); }

mint g(int r, int c) { return f(r + 1, c + 1) - 1; }

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  init_comb(2e6 + 1);
  cout << g(r2, c2) - g(r1 - 1, c2) - g(r2, c1 - 1) + g(r1 - 1, c1 - 1) << endl;
  return 0;
}


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

// ------------------
// Integer modulo MOD
// ------------------
const int64_t MOD = 1e9 + 7;
struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t n) const {
    if (n == 0)
      return 1;
    mint t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// ---------------------------------------
// Number of combinations (nCk) modulo MOD
// ---------------------------------------
vector<mint> fac;
vector<mint> fac_inv;
vector<mint> inv;
void init_comb(int mx) {
  fac = vector<mint>(mx + 1), fac_inv = vector<mint>(mx + 1),
  inv = vector<mint>(mx + 1);
  fac.at(0) = fac.at(1) = 1;
  fac_inv.at(0) = fac_inv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i <= mx; i++) {
    fac.at(i) = fac.at(i - 1) * i;
    inv.at(i) = -inv.at(MOD % i) * (MOD / i);
    fac_inv.at(i) = fac_inv.at(i - 1) * inv.at(i);
  }
}
mint comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * fac_inv.at(k) * fac_inv.at(n - k);
}

mint f(int r, int c) { return comb(r + c, c); }

mint g(int r, int c) { return f(r + 1, c + 1) - 1; }

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  init_comb(2e6 + 2);
  cout << g(r2, c2) - g(r1 - 1, c2) - g(r2, c1 - 1) + g(r1 - 1, c1 - 1) << endl;
  return 0;
}