Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 1e6 + 5;

template <int MOD = 1000000007> struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) {
    value = v % MOD;
    if (value < 0)
      value += MOD;
  }
  Modular(long long a, long long b) : value(0) {
    *this += a;
    *this /= b;
  }

  Modular &operator+=(Modular const &b) {
    value += b.value;
    if (value >= MOD)
      value -= MOD;
    return *this;
  }
  Modular &operator-=(Modular const &b) {
    value -= b.value;
    if (value < 0)
      value += MOD;
    return *this;
  }
  Modular &operator*=(Modular const &b) {
    value = (long long)value * b.value % MOD;
    return *this;
  }

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1;
    while (e) {
      if (e & 1)
        res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular &operator/=(Modular const &b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream &operator<<(std::ostream &os, Modular const &a) {
    return os << a.value;
  }
  friend bool operator==(Modular const &a, Modular const &b) {
    return a.value == b.value;
  }
  friend bool operator!=(Modular const &a, Modular const &b) {
    return a.value != b.value;
  }
};
Modular<M> ans = 0, fact[N];

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

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

  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = fact[i - 1] * i;
  }

  for (int i = c1; i <= c2; ++i) {
    Modular<M> l = fact[i + r1] / (fact[i + 1] * fact[r1 - 1]);
    Modular<M> r = fact[r2 + i + 1] / (fact[i + 1] * fact[r2]);
    ans += r - l;
  }

  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 3e6 + 5;

template <int MOD = 1000000007> struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) {
    value = v % MOD;
    if (value < 0)
      value += MOD;
  }
  Modular(long long a, long long b) : value(0) {
    *this += a;
    *this /= b;
  }

  Modular &operator+=(Modular const &b) {
    value += b.value;
    if (value >= MOD)
      value -= MOD;
    return *this;
  }
  Modular &operator-=(Modular const &b) {
    value -= b.value;
    if (value < 0)
      value += MOD;
    return *this;
  }
  Modular &operator*=(Modular const &b) {
    value = (long long)value * b.value % MOD;
    return *this;
  }

  friend Modular mexp(Modular a, long long e) {
    Modular res = 1;
    while (e) {
      if (e & 1)
        res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular &operator/=(Modular const &b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream &operator<<(std::ostream &os, Modular const &a) {
    return os << a.value;
  }
  friend bool operator==(Modular const &a, Modular const &b) {
    return a.value == b.value;
  }
  friend bool operator!=(Modular const &a, Modular const &b) {
    return a.value != b.value;
  }
};
Modular<M> ans = 0, fact[N];

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

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

  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = fact[i - 1] * i;
  }

  for (int i = c1; i <= c2; ++i) {
    Modular<M> l = fact[i + r1] / (fact[i + 1] * fact[r1 - 1]);
    Modular<M> r = fact[r2 + i + 1] / (fact[i + 1] * fact[r2]);
    ans += r - l;
  }

  cout << ans << '\n';
}