Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

const int max_comb = 220000;
vector<ll> fac(max_comb + 1);  // n! (mod M)
vector<ll> ifac(max_comb + 1); // k!^(-1) (mod M)

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

ll comb(int a, int b) { // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

ll perm(int a, int b) { // P(a,b) = a! * (a-b)!^(-1)
  if (b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] % MOD;
  return tmp * fac[a] % MOD;
}

void pre_comb() {
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 0; i < max_comb; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;       // n!(mod M)
    ifac[i + 1] = ifac[i] * minv(i + 1) % MOD; // k!^(-1) (mod M)
  }
  return;
}

void solve() {
  int r0, c0, r1, c1;
  scanf("%d%d%d%d", &r0, &c0, &r1, &c1);
  pre_comb();

  ll ans0 = comb(r1 + c1 + 2, r1 + 1);
  ll ans1 = comb(r1 + c0 + 1, c0);
  ll ans2 = comb(r0 + c1 + 1, r0);
  ll ans3 = comb(r0 + c0, r0);
  ll ans = (ans0 - ans1 - ans2 + ans3 + MOD + MOD) % MOD;
  printf("%lld\n", ans);

  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

const int max_comb = 2100000;
vector<ll> fac(max_comb + 1);  // n! (mod M)
vector<ll> ifac(max_comb + 1); // k!^(-1) (mod M)

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

ll comb(int a, int b) { // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

ll perm(int a, int b) { // P(a,b) = a! * (a-b)!^(-1)
  if (b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] % MOD;
  return tmp * fac[a] % MOD;
}

void pre_comb() {
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 0; i < max_comb; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD;       // n!(mod M)
    ifac[i + 1] = ifac[i] * minv(i + 1) % MOD; // k!^(-1) (mod M)
  }
  return;
}

void solve() {
  int r0, c0, r1, c1;
  scanf("%d%d%d%d", &r0, &c0, &r1, &c1);
  pre_comb();

  ll ans0 = comb(r1 + c1 + 2, r1 + 1);
  ll ans1 = comb(r1 + c0 + 1, c0);
  ll ans2 = comb(r0 + c1 + 1, r0);
  ll ans3 = comb(r0 + c0, r0);
  ll ans = (ans0 - ans1 - ans2 + ans3 + MOD + MOD) % MOD;
  printf("%lld\n", ans);

  return;
}

int main(int argc, char *argv[]) {
#if 1
  solve();
#else
  int T;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
#endif
  return 0;
}