Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

const int mod = 1e9 + 7;
template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<mod>;

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

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  //  r1++;c1++;r2++;c2++;

  vector<modint> fact(r2 + c2 + 4);
  vector<modint> inv(r2 + c2 + 4);
  fact[0] = 1;
  rep1(i, r2 + c2 + 4) fact[i] = fact[i - 1] * ((modint)i);
  rep(i, r2 + c2 + 5) {
    inv[i] = 1;
    inv[i] /= fact[i];
  }

  modint ans = 0;

  ans = (fact[r2 + c2 + 2] * inv[r2 + 1] * inv[c2 + 1] +
         fact[r1 + c1] * inv[r1] * inv[c1] -
         fact[r2 + c1 + 1] * inv[r2 + 1] * inv[c1] -
         fact[r1 + c2 + 1] * inv[r1] * inv[c2 + 1]);

  cout << ans << endl;

  //  cout << endl << endl;
  //  cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

const int mod = 1e9 + 7;
template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<mod>;

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

  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  //  r1++;c1++;r2++;c2++;

  vector<modint> fact(r2 + c2 + 4);
  vector<modint> inv(r2 + c2 + 4);
  fact[0] = 1;
  rep1(i, r2 + c2 + 3) fact[i] = fact[i - 1] * ((modint)i);
  rep(i, r2 + c2 + 4) {
    inv[i] = 1;
    inv[i] /= fact[i];
  }

  modint ans = 0;

  ans = (fact[r2 + c2 + 2] * inv[r2 + 1] * inv[c2 + 1] +
         fact[r1 + c1] * inv[r1] * inv[c1] -
         fact[r2 + c1 + 1] * inv[r2 + 1] * inv[c1] -
         fact[r1 + c2 + 1] * inv[r1] * inv[c2 + 1]);

  cout << ans << endl;

  //  cout << endl << endl;
  //  cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
}