Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pll pair<ll, ll>
#define pint pll
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

const int MAX = 1010000;
const int MOD = 1e9 + 7;

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

void COMinit() {
  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 COM(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 ans(ll r, ll c) {
  ll ret = 0;
  if (r == 1) {
    ret = c * (c + 3);
    ret %= mod;
    ret *= inv[2];
    ret %= mod;
    return ret;
  }
  ret = COM(c + r + 2, r + 1);
  ll t = r + c + 2;
  t %= mod;
  t *= -1;
  t += mod;
  t %= mod;
  ret += t;
  ret %= mod;
  return ret;
}

int main() {
  COMinit();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll x = ans(r2, c2), y = ans(r1 - 1, c1 - 1), z = mod - ans(r1 - 1, c2),
     w = mod - ans(r2, c1 - 1);
  z += w;
  z %= mod;
  x += z;
  x %= mod;
  x += y;
  x %= mod;
  cout << x << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pll pair<ll, ll>
#define pint pll
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

const int MAX = 3010000;
const int MOD = 1e9 + 7;

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

void COMinit() {
  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 COM(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 ans(ll r, ll c) {
  ll ret = 0;
  if (r == 1) {
    ret = c * (c + 3);
    ret %= mod;
    ret *= inv[2];
    ret %= mod;
    return ret;
  }
  ret = COM(c + r + 2, r + 1);
  ll t = r + c + 2;
  t %= mod;
  t *= -1;
  t += mod;
  t %= mod;
  ret += t;
  ret %= mod;
  return ret;
}

int main() {
  COMinit();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll x = ans(r2, c2), y = ans(r1 - 1, c1 - 1), z = mod - ans(r1 - 1, c2),
     w = mod - ans(r2, c1 - 1);
  z += w;
  z %= mod;
  x += z;
  x %= mod;
  x += y;
  x %= mod;
  cout << x << endl;
  return 0;
}