Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
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;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mo 1000000007
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

ll comb(ll N_, ll C_) {
  const int NUM_ = 400001;
  static ll fact[400002], factr[400002], inv[400002];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[mo % i] * (mo - mo / i) % mo;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
ll modpow(ll a, ll n = mo - 2) {
  ll r = 1;
  while (n)
    r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
  return r;
}

int g(int a, int b) { return comb(a + b + 2, a + 1) - 1; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int A, B, C, D;
  cin >> A >> B >> C >> D;
  A--;
  B--;

  cout << (g(C, D) - g(A, D) - g(C, B) + g(A, B) + mod) % mod << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
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;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mo 1000000007
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

ll comb(ll N_, ll C_) {
  const int NUM_ = 4000001;
  static ll fact[4000002], factr[4000002], inv[4000002];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[mo % i] * (mo - mo / i) % mo;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
ll modpow(ll a, ll n = mo - 2) {
  ll r = 1;
  while (n)
    r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
  return r;
}

int g(int a, int b) { return comb(a + b + 2, a + 1) - 1; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int A, B, C, D;
  cin >> A >> B >> C >> D;
  A--;
  B--;

  cout << (g(C, D) - g(A, D) - g(C, B) + g(A, B) + mod) % mod << endl;

  return 0;
}