Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod_) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod_;
    a = a * a % mod_;
    n >>= 1;
  }
  return res;
}

// const ll mod = 998244353;
const ll mod = 1e9 + 7;
const ll MAX_SIZE = 1000005;
array<ll, MAX_SIZE> fac, inv, finv;
ll fac_init() {
  // combination init
  fac[0] = 1;
  for (ll i = 1; i < MAX_SIZE; i++)
    fac[i] = fac[i - 1] * i % mod;
  finv[0] = modpow(fac[MAX_SIZE - 1], mod - 2, mod);
  repe(i, 1, MAX_SIZE) finv[i] = finv[i - 1] * (MAX_SIZE - i) % mod;
  reverse(all(finv));

  // inv init
  repe(i, 1, MAX_SIZE) inv[i] = modpow(i, mod - 2, mod);
}

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % mod; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % mod * finv[a - b] % mod; }

ll f(ll a, ll b) {
  return (a != 1) ? comb(a + b + 2, b + 1) - (a + b + 2)
                  : b * (b + 3) / 2 % mod;
}
int main() {
  init();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  fac_init();

  ll ans = (f(r2, c2) - f(r2, c1 - 1) - f(r1 - 1, c2) + f(r1 - 1, c1 - 1) +
            5 * mod) %
           mod;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod_) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod_;
    a = a * a % mod_;
    n >>= 1;
  }
  return res;
}

// const ll mod = 998244353;
const ll mod = 1e9 + 7;
const ll MAX_SIZE = 2000005;
array<ll, MAX_SIZE> fac, inv, finv;
ll fac_init() {
  // combination init
  fac[0] = 1;
  for (ll i = 1; i < MAX_SIZE; i++)
    fac[i] = fac[i - 1] * i % mod;
  finv[0] = modpow(fac[MAX_SIZE - 1], mod - 2, mod);
  repe(i, 1, MAX_SIZE) finv[i] = finv[i - 1] * (MAX_SIZE - i) % mod;
  reverse(all(finv));

  // inv init
  repe(i, 1, MAX_SIZE) inv[i] = modpow(i, mod - 2, mod);
}

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % mod; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % mod * finv[a - b] % mod; }

ll f(ll a, ll b) {
  return (a != 1) ? comb(a + b + 2, b + 1) - (a + b + 2)
                  : b * (b + 3) / 2 % mod;
}
int main() {
  init();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  fac_init();

  ll ans = (f(r2, c2) - f(r2, c1 - 1) - f(r1 - 1, c2) + f(r1 - 1, c1 - 1) +
            5 * mod) %
           mod;
  cout << ans << endl;
}