Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
const int MAX = 1010101;

long long fac[MAX], finv[MAX], inv[MAX];

void binom_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long binom(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll r1, c1, r2, c2;

ll g(ll r, ll c) {
  ll res = 0;
  for (ll i = 0; i <= r; i++) {
    res += binom(i + c + 1, c);
    res %= MOD;
  }
  return res;
}

int main() {
  binom_init();
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = 0;
  ans = g(r2, c2);
  ans = (ans + MOD - g(r2, c1 - 1)) % MOD;
  ans = (ans + MOD - g(r1 - 1, c2)) % MOD;
  ans += g(r1 - 1, c1 - 1);
  ans %= MOD;
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
const int MAX = 2010101;

long long fac[MAX], finv[MAX], inv[MAX];

void binom_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long binom(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll r1, c1, r2, c2;

ll g(ll r, ll c) {
  ll res = 0;
  for (ll i = 0; i <= r; i++) {
    res += binom(i + c + 1, c);
    res %= MOD;
  }
  return res;
}

int main() {
  binom_init();
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = 0;
  ans = g(r2, c2);
  ans = (ans + MOD - g(r2, c1 - 1)) % MOD;
  ans = (ans + MOD - g(r1 - 1, c2)) % MOD;
  ans += g(r1 - 1, c1 - 1);
  ans %= MOD;
  cout << ans << endl;
}