Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define mcout(m)                                                               \
  rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;

const int MAX = 1e6 + 5; // 適宜

ll 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;
  }
}

// 二項係数計算
ll 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 PER(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * finv[n - k] % MOD;
}

// 逆元 a^{-1} mod m
// aがmの倍数でないことが前提。O(log m)
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

void mainsolve() {

  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  COMinit();
  /*
  vector<ll> ff(MAX),cum(MAX+1);
  ff[0] = ff[1] = 1;
  rep(i,MAX){
      if(i!=0) ff[i] = (ff[i-1] * i) % MOD;
      cum[i+1] = (cum[i] + ff[i]) % MOD;
  }
*/
  auto g = [&](ll r, ll c) {
    ll res = 0;
    if (r == 0 || c == 0)
      return res;
    if (r == 1) {
      rep(i, c) res = (res + COM(2 + i, 1 + i)) % MOD;
      return res;
    } else {
      rep(i, c) res = (res + COM(2 + r + i, 2 + i)) % MOD;
      return (res - c + MOD) % MOD;
    }
  };

  ll res = (g(r2, c2) + g(r1 - 1, c1 - 1)) % MOD;
  res = (res - g(r1 - 1, c2) + MOD) % MOD;
  res = (res - g(r2, c1 - 1) + MOD) % MOD;
  cout << res << endl;
  return;
}

int main() {

  // ll TC;cin>>TC;rep(I,TC){cout<<"case:"<<I+1<<"---"<<endl; // comment out!
  mainsolve();
  //} // comment out!
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define vcout(v)                                                               \
  rep(i, v.size()) cout << v[i] << " ";                                        \
  cout << endl
#define mcout(m)                                                               \
  rep(j, m.size()) { vcout(m[j]); }
const ll MOD = 1e9 + 7;

const int MAX = 3 * 1e6; // 適宜

ll 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;
  }
}

// 二項係数計算
ll 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 PER(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * finv[n - k] % MOD;
}

// 逆元 a^{-1} mod m
// aがmの倍数でないことが前提。O(log m)
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

void mainsolve() {

  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;

  COMinit();
  /*
  vector<ll> ff(MAX),cum(MAX+1);
  ff[0] = ff[1] = 1;
  rep(i,MAX){
      if(i!=0) ff[i] = (ff[i-1] * i) % MOD;
      cum[i+1] = (cum[i] + ff[i]) % MOD;
  }
*/
  auto g = [&](ll r, ll c) {
    ll res = 0;
    if (r == 0 || c == 0)
      return res;
    if (r == 1) {
      rep(i, c) res = (res + COM(2 + i, 1 + i)) % MOD;
      return res;
    } else {
      rep(i, c) res = (res + COM(2 + r + i, 2 + i)) % MOD;
      return (res - c + MOD) % MOD;
    }
  };

  ll res = (g(r2, c2) + g(r1 - 1, c1 - 1)) % MOD;
  res = (res - g(r1 - 1, c2) + MOD) % MOD;
  res = (res - g(r2, c1 - 1) + MOD) % MOD;
  cout << res << endl;
  return;
}

int main() {

  // ll TC;cin>>TC;rep(I,TC){cout<<"case:"<<I+1<<"---"<<endl; // comment out!
  mainsolve();
  //} // comment out!
  return 0;
}