Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initValue, args...))>(
      sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    out << e << delim[&e == &vec.back()];
  return out;
}
// }}} End Header

constexpr int MOD = int(1e9) + 7;

// ModInt {{{
template <int64_t MOD> class ModInt {
  int64_t value;

public:
  // constructor
  inline ModInt(int64_t val = 0) noexcept
      : value((val >= MOD) ? (val % MOD)
              : (val < 0)  ? ((val + MOD) % MOD)
                           : val) {}

  // プリミティブ整数型へのキャスト (型の明示:必要)
  template <class Int> explicit inline operator Int() const noexcept {
    return static_cast<Int>(value);
  }

  inline ModInt inv() const noexcept {
    return ModInt<MOD>::pow(value, MOD - 2);
  }

  inline ModInt &operator+=(ModInt x) noexcept {
    if ((value += x.value) >= MOD)
      value -= MOD;
    return *this;
  }
  inline ModInt &operator-=(ModInt x) noexcept {
    if ((value -= x.value) < 0)
      value += MOD;
    return *this;
  }
  inline ModInt &operator*=(ModInt x) noexcept {
    value = (value * x.value) % MOD;
    return *this;
  }
  inline ModInt &operator/=(ModInt x) noexcept {
    value = (value * x.inv().value) % MOD;
    return *this;
  }

  inline ModInt operator+(ModInt x) const noexcept {
    return ModInt(*this) += x;
  }
  inline ModInt operator-(ModInt x) const noexcept {
    return ModInt(*this) -= x;
  }
  inline ModInt operator*(ModInt x) const noexcept {
    return ModInt(*this) *= x;
  }
  inline ModInt operator/(ModInt x) const noexcept {
    return ModInt(*this) /= x;
  }
  inline bool operator==(ModInt x) const noexcept { return value == x.value; }
  inline bool operator!=(ModInt x) const noexcept {
    return !(this->operator==(x));
  }

  friend ostream &operator<<(ostream &os, ModInt x) noexcept {
    os << x.value;
    return os;
  }
  friend istream &operator>>(istream &is, ModInt &x) noexcept {
    is >> x.value;
    x.value %= MOD;
    return is;
  }

  static constexpr inline ModInt pow(int64_t n, int64_t p) noexcept {
    int64_t ret = 1;
    for (; p > 0; p >>= 1) {
      if (p & 1)
        ret = (ret * n) % MOD;
      n = (n * n) % MOD;
    }
    return ret;
  }
};
// }}}
using modint = ModInt<MOD>;

// Factorial, InvFact {{{
template <size_t N, int MOD> struct Factorial {
  uint_fast64_t f[N + 1];

  inline Factorial() : f{1} {
    for (int i = 1; i <= N; ++i)
      f[i] = (f[i - 1] * i) % MOD;
  }

  constexpr inline ModInt<MOD> operator[](size_t i) const { return f[i]; }
};

template <size_t N, int MOD> struct InvFact {
  uint_fast64_t inv[N + 1];
  uint_fast64_t f[N + 1];

  inline InvFact() : inv{0, 1}, f{1, 1} {
    for (int i = 2; i <= N; ++i) {
      inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
      f[i] = f[i - 1] * inv[i] % MOD;
    }
  }

  constexpr inline ModInt<MOD> operator[](size_t i) const { return f[i]; }
};
// }}}
Factorial<1000100, MOD> fact;
InvFact<1000100, MOD> invFact;
ModInt<MOD> nCr(int n, int r) {
  return (r < 0 || n < r) ? 0 : (fact[n] * (invFact[r] * invFact[n - r]));
}
ModInt<MOD> nPr(int n, int r) {
  return (r < 0 || n < r) ? 0 : (fact[n] * invFact[n - r]);
}
ModInt<MOD> nHr(int n, int r) { return nCr(n + r - 1, r); }

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  i64 r1, c1, r2, c2;
  read(r1, c1, r2, c2);

  modint ans = 0;
  reps(y, r1, r2) {
    const i64 h = y;
    const auto f = [&h](i64 w) -> modint {
      return (modint(w + 1) * fact[w + h + 1]) /
             (modint(h + 1) * fact[w + 1] * fact[h]);
    };
    ans += f(c2) - f(c1 - 1);
  }

  print(ans);

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initValue, args...))>(
      sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    out << e << delim[&e == &vec.back()];
  return out;
}
// }}} End Header

constexpr int MOD = int(1e9) + 7;

// ModInt {{{
template <int64_t MOD> class ModInt {
  int64_t value;

public:
  // constructor
  inline ModInt(int64_t val = 0) noexcept
      : value((val >= MOD) ? (val % MOD)
              : (val < 0)  ? ((val + MOD) % MOD)
                           : val) {}

  // プリミティブ整数型へのキャスト (型の明示:必要)
  template <class Int> explicit inline operator Int() const noexcept {
    return static_cast<Int>(value);
  }

  inline ModInt inv() const noexcept {
    return ModInt<MOD>::pow(value, MOD - 2);
  }

  inline ModInt &operator+=(ModInt x) noexcept {
    if ((value += x.value) >= MOD)
      value -= MOD;
    return *this;
  }
  inline ModInt &operator-=(ModInt x) noexcept {
    if ((value -= x.value) < 0)
      value += MOD;
    return *this;
  }
  inline ModInt &operator*=(ModInt x) noexcept {
    value = (value * x.value) % MOD;
    return *this;
  }
  inline ModInt &operator/=(ModInt x) noexcept {
    value = (value * x.inv().value) % MOD;
    return *this;
  }

  inline ModInt operator+(ModInt x) const noexcept {
    return ModInt(*this) += x;
  }
  inline ModInt operator-(ModInt x) const noexcept {
    return ModInt(*this) -= x;
  }
  inline ModInt operator*(ModInt x) const noexcept {
    return ModInt(*this) *= x;
  }
  inline ModInt operator/(ModInt x) const noexcept {
    return ModInt(*this) /= x;
  }
  inline bool operator==(ModInt x) const noexcept { return value == x.value; }
  inline bool operator!=(ModInt x) const noexcept {
    return !(this->operator==(x));
  }

  friend ostream &operator<<(ostream &os, ModInt x) noexcept {
    os << x.value;
    return os;
  }
  friend istream &operator>>(istream &is, ModInt &x) noexcept {
    is >> x.value;
    x.value %= MOD;
    return is;
  }

  static constexpr inline ModInt pow(int64_t n, int64_t p) noexcept {
    int64_t ret = 1;
    for (; p > 0; p >>= 1) {
      if (p & 1)
        ret = (ret * n) % MOD;
      n = (n * n) % MOD;
    }
    return ret;
  }
};
// }}}
using modint = ModInt<MOD>;

// Factorial, InvFact {{{
template <size_t N, int MOD> struct Factorial {
  uint_fast64_t f[N + 1];

  inline Factorial() : f{1} {
    for (int i = 1; i <= N; ++i)
      f[i] = (f[i - 1] * i) % MOD;
  }

  constexpr inline ModInt<MOD> operator[](size_t i) const { return f[i]; }
};

template <size_t N, int MOD> struct InvFact {
  uint_fast64_t inv[N + 1];
  uint_fast64_t f[N + 1];

  inline InvFact() : inv{0, 1}, f{1, 1} {
    for (int i = 2; i <= N; ++i) {
      inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
      f[i] = f[i - 1] * inv[i] % MOD;
    }
  }

  constexpr inline ModInt<MOD> operator[](size_t i) const { return f[i]; }
};
// }}}
Factorial<2000100, MOD> fact;
InvFact<2000100, MOD> invFact;
ModInt<MOD> nCr(int n, int r) {
  return (r < 0 || n < r) ? 0 : (fact[n] * (invFact[r] * invFact[n - r]));
}
ModInt<MOD> nPr(int n, int r) {
  return (r < 0 || n < r) ? 0 : (fact[n] * invFact[n - r]);
}
ModInt<MOD> nHr(int n, int r) { return nCr(n + r - 1, r); }

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  i64 r1, c1, r2, c2;
  read(r1, c1, r2, c2);

  modint ans = 0;
  reps(y, r1, r2) {
    const i64 h = y;
    const auto f = [&h](i64 w) -> modint {
      return (modint(w + 1) * fact[w + h + 1]) /
             (modint(h + 1) * fact[w + 1] * fact[h]);
    };
    ans += f(c2) - f(c1 - 1);
  }

  print(ans);

  return 0;
}