Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

#include <cassert>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \
  })(c))
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _fromto2(i, n) for (int i = 0; i <= (n); ++i)
#define _fromto3(i, a, b) for (int i = (a); i <= (b); ++i)
#define fromto(...) _overload3(__VA_ARGS__, _fromto3, _fromto2, )(__VA_ARGS__)

#define rrep(i, n) for (int i = (n); i >= 0; --i)
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
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;
}

// debug
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\n"; }
template <class First> void debug(const First &first) { cerr << first << "\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
#else
#define debug(...) 42
#endif

const int MAX = 1e6 + 5;
long long fac[MAX], finv[MAX], inv[MAX];

void comb_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;
  }
}

int f(int r, int c) { return (fac[r + c] * finv[r] % MOD) * finv[c] % MOD; }

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  comb_init();

  // ll ans = 0;
  // fromto(i, r1, r2) {
  //   fromto(j, c1, c2) {
  //     ans += f(i, j);
  //     ans %= MOD;
  //   }
  // }
  // cout << ans << endl;
  // debug(f(r2, c2) * 2 % MOD);
  ll ans = f(r2 + 1, c2 + 1);
  ans = MOD + ans - f(r2 + 1, c1);
  ans %= MOD;
  ans = MOD + ans - f(r1, c2 + 1);
  ans %= MOD;
  ans += f(r1, c1);
  ans %= MOD;
  cout << ans << endl;
  // f(r, c) = (r+c)! * (1/(r!)) * (1/(c!))
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>

#include <cassert>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \
  })(c))
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _fromto2(i, n) for (int i = 0; i <= (n); ++i)
#define _fromto3(i, a, b) for (int i = (a); i <= (b); ++i)
#define fromto(...) _overload3(__VA_ARGS__, _fromto3, _fromto2, )(__VA_ARGS__)

#define rrep(i, n) for (int i = (n); i >= 0; --i)
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
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;
}

// debug
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\n"; }
template <class First> void debug(const First &first) { cerr << first << "\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
#else
#define debug(...) 42
#endif

const int MAX = 2e6 + 500;
long long fac[MAX], finv[MAX], inv[MAX];

void comb_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;
  }
}

int f(int r, int c) { return (fac[r + c] * finv[r] % MOD) * finv[c] % MOD; }

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  comb_init();

  // ll ans = 0;
  // fromto(i, r1, r2) {
  //   fromto(j, c1, c2) {
  //     ans += f(i, j);
  //     ans %= MOD;
  //   }
  // }
  // cout << ans << endl;
  // debug(f(r2, c2) * 2 % MOD);
  ll ans = f(r2 + 1, c2 + 1);
  ans = MOD + ans - f(r2 + 1, c1);
  ans %= MOD;
  ans = MOD + ans - f(r1, c2 + 1);
  ans %= MOD;
  ans += f(r1, c1);
  ans %= MOD;
  cout << ans << endl;
  // f(r, c) = (r+c)! * (1/(r!)) * (1/(c!))
  return 0;
}