prithivMLmods/Coder-Stat huggingface dataset:
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int MOD = 1e9 + 7;
ll d[N];
ll qpow(ll x, ll k) {
  ll r = 1;
  while (k) {
    if (k & 1)
      r = r * x % MOD;
    k >>= 1;
    x = x * x % MOD;
  }
  return r;
}
ll C(int i, int j) {
  return d[i] * qpow(d[i - j], MOD - 2) % MOD * qpow(d[j], MOD - 2) % MOD;
}
ll Cal(int i, int j) {
  ll res = 0;
  // C(i+j, j)
  for (int x = 0; x <= j; x++) {
    res += C(x + i - j + 1, x + 1);
    res %= MOD;
  }
  return res;
}
int main() {
  d[0] = 1;
  for (int i = 1; i < N; i++) {
    d[i] = d[i - 1] * i % MOD;
  }
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = Cal(r2 + c2, c2) - Cal(r1 - 1 + c2, c2) - Cal(r2 + c1 - 1, c1 - 1) +
           Cal(r1 - 1 + c1 - 1, c1 - 1);
  ans %= MOD;
  ans += MOD;
  ans %= MOD;
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6 + 10;
const int MOD = 1e9 + 7;
ll d[N];
ll qpow(ll x, ll k) {
  ll r = 1;
  while (k) {
    if (k & 1)
      r = r * x % MOD;
    k >>= 1;
    x = x * x % MOD;
  }
  return r;
}
ll C(int i, int j) {
  return d[i] * qpow(d[i - j], MOD - 2) % MOD * qpow(d[j], MOD - 2) % MOD;
}
ll Cal(int i, int j) {
  ll res = 0;
  // C(i+j, j)
  for (int x = 0; x <= j; x++) {
    res += C(x + i - j + 1, x + 1);
    res %= MOD;
  }
  return res;
}
int main() {
  d[0] = 1;
  for (int i = 1; i < N; i++) {
    d[i] = d[i - 1] * i % MOD;
  }
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = Cal(r2 + c2, c2) - Cal(r1 - 1 + c2, c2) - Cal(r2 + c1 - 1, c1 - 1) +
           Cal(r1 - 1 + c1 - 1, c1 - 1);
  ans %= MOD;
  ans += MOD;
  ans %= MOD;
  cout << ans << '\n';
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

const int MAX = 510000;
const int mod = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  rep(i, MAX) {
    if (i > 1) {
      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;
}

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  init();
  ll ans = com(C + D + 2, C + 1) + com(A + B, B);
  ans %= mod;
  (ans += mod - com(A + D + 1, A)) %= mod;
  (ans += mod - com(B + C + 1, B)) %= mod;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

const int MAX = 5000000;
const int mod = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  rep(i, MAX) {
    if (i > 1) {
      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;
}

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  init();
  ll ans = com(C + D + 2, C + 1) + com(A + B, B);
  ans %= mod;
  (ans += mod - com(A + D + 1, A)) %= mod;
  (ans += mod - com(B + C + 1, B)) %= mod;
  cout << ans << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

const int MAX = 1000000;
const int MOD = 1000000007;

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

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  ll sum1 = 0;
  for (ll i = c1; i <= c2; i++) {
    // cout << COM(r2+i+1,i+1)<<endl;
    sum1 += COM(r2 + i + 1, i + 1);
    sum1 %= inf;
  }
  ll sum2 = 0;
  for (ll i = c1; i <= c2; i++) {
    // cout << COM(r1+i,i+1)<<endl;
    sum2 += COM(r1 + i, i + 1);
    sum2 %= inf;
  }
  if (sum1 - sum2 < 0) {
    sum1 += inf;
  }
  cout << sum1 - sum2;
  // your code goes here
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

const int MAX = 5100000;
const int MOD = 1000000007;

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

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  ll sum1 = 0;
  for (ll i = c1; i <= c2; i++) {
    // cout << COM(r2+i+1,i+1)<<endl;
    sum1 += COM(r2 + i + 1, i + 1);
    sum1 %= inf;
  }
  ll sum2 = 0;
  for (ll i = c1; i <= c2; i++) {
    // cout << COM(r1+i,i+1)<<endl;
    sum2 += COM(r1 + i, i + 1);
    sum2 %= inf;
  }
  if (sum1 - sum2 < 0) {
    sum1 += inf;
  }
  cout << sum1 - sum2;
  // your code goes here
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 1000 * 1000 + 20, mod = 1e9 + 7;
int fact[N], factRev[N];

int sum(int a, int b) { return ((1LL * a + b) % mod + mod) % mod; }

int mul(int a, int b) { return ((1LL * a * b) % mod + mod) % mod; }

int pw(int a, int b) {
  int res = 1, tmp = a;
  while (b) {
    if (b & 1)
      res = mul(res, tmp);
    tmp = mul(tmp, tmp);
    b >>= 1;
  }
  return res;
}

int c(int a, int b) { return mul(fact[a], mul(factRev[b], factRev[a - b])); }

int main() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  factRev[N - 1] = pw(fact[N - 1], mod - 2);
  for (int i = N - 2; ~i; i--)
    factRev[i] = mul(factRev[i + 1], i + 1);
  int r1, r2, c1, c2, ans = 0;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = r1; i <= r2; i++)
    ans = sum(ans, sum(c(c2 + i + 1, i + 1), -c(c1 + i, i + 1)));
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 2000 * 1000 + 20, mod = 1e9 + 7;
int fact[N], factRev[N];

int sum(int a, int b) { return ((1LL * a + b) % mod + mod) % mod; }

int mul(int a, int b) { return ((1LL * a * b) % mod + mod) % mod; }

int pw(int a, int b) {
  int res = 1, tmp = a;
  while (b) {
    if (b & 1)
      res = mul(res, tmp);
    tmp = mul(tmp, tmp);
    b >>= 1;
  }
  return res;
}

int c(int a, int b) { return mul(fact[a], mul(factRev[b], factRev[a - b])); }

int main() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  factRev[N - 1] = pw(fact[N - 1], mod - 2);
  for (int i = N - 2; ~i; i--)
    factRev[i] = mul(factRev[i + 1], i + 1);
  int r1, r2, c1, c2, ans = 0;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = r1; i <= r2; i++)
    ans = sum(ans, sum(c(c2 + i + 1, i + 1), -c(c1 + i, i + 1)));
  cout << ans << "\n";
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;

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

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

ll g(int x, int y) { return COM(x + y + 2, x + 1) - 1; }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  ll ans = g(c2, r2);
  ans -= g(c2, r1);
  ans -= g(c1, r2);
  ans += g(c1, r1);
  cout << (ans + 2 * mod) % mod << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int MAX = 6666666;
const int MOD = 1000000007;

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

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

ll g(int x, int y) { return COM(x + y + 2, x + 1) - 1; }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;
  ll ans = g(c2, r2);
  ans -= g(c2, r1);
  ans -= g(c1, r2);
  ans += g(c1, r1);
  cout << (ans + 2 * mod) % mod << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

const int MAX = 510000;
const int MOD = 1000000007;

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 COMS(int x, int y) { return COM(x + y + 2, x + 1); }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

const int MAX = 2010000;
const int MOD = 1000000007;

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 COMS(int x, int y) { return COM(x + y + 2, x + 1); }

int main() {
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = (COMS(r2, c2) - COMS(r2, c1 - 1) - COMS(r1 - 1, c2) +
            COMS(r1 - 1, c1 - 1)) %
           mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

const int MAX = 510000;
const int MOD = 1000000007;

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 cal(int r, int c) { return COM(r + c + 2, r + 1); }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

const int MAX = 2100000;
const int MOD = 1000000007;

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 cal(int r, int c) { return COM(r + c + 2, r + 1); }

int main() {
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans =
      cal(r2, c2) - cal(r2, c1 - 1) - cal(r1 - 1, c2) + cal(r1 - 1, c1 - 1);
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

vector<long long int> inverse;
vector<long long int> f, f_inverse;

void factorial_Initialize(int max) {
  vector<long long int> a(max + 1, 1);
  inverse = a;
  f = a;
  f_inverse = a;

  for (int i = 2; i <= max; i++) {
    inverse[i] = MOD - inverse[MOD % i] * (MOD / i) % MOD;
    f[i] = f[i - 1] * i % MOD;
    f_inverse[i] = f_inverse[i - 1] * inverse[i] % MOD;
  }
}

long long int nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  if (n >= f.size()) {
    factorial_Initialize(n);
  }
  return f[n] * f_inverse[r] % MOD * f_inverse[n - r] % MOD;
}

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

  long long int f11 = 0, f12 = 0, f21 = 0, f22 = 0;
  for (int i = 0; i < r1; i++) {
    f12 += nCr(i + c2 + 1, c2);
    f12 %= MOD;
    f11 += nCr(i + c1, c1 - 1);
    f11 %= MOD;
  }
  for (int i = 0; i <= r2; i++) {
    f22 += nCr(i + c2 + 1, c2);
    f22 %= MOD;
    f21 += nCr(i + c1, c1 - 1);
    f21 %= MOD;
  }
  // cout << f11 << " " << f12 << " " << f21 << " " << f22 << endl;
  cout << (((f22 - f21 - f12 + f11) % MOD + MOD) % MOD) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;

vector<long long int> inverse;
vector<long long int> f, f_inverse;

void factorial_Initialize(int max) {
  vector<long long int> a(max + 1, 1);
  inverse = a;
  f = a;
  f_inverse = a;

  for (int i = 2; i <= max; i++) {
    inverse[i] = MOD - inverse[MOD % i] * (MOD / i) % MOD;
    f[i] = f[i - 1] * i % MOD;
    f_inverse[i] = f_inverse[i - 1] * inverse[i] % MOD;
  }
}

long long int nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  if (n >= f.size()) {
    factorial_Initialize(n);
  }
  return f[n] * f_inverse[r] % MOD * f_inverse[n - r] % MOD;
}

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

  long long int f11 = 0, f12 = 0, f21 = 0, f22 = 0;
  for (int i = 0; i < r1; i++) {
    f12 += nCr(i + c2 + 1, c2);
    f12 %= MOD;
    f11 += nCr(i + c1, c1 - 1);
    f11 %= MOD;
  }
  for (int i = 0; i <= r2; i++) {
    f22 += nCr(i + c2 + 1, c2);
    f22 %= MOD;
    f21 += nCr(i + c1, c1 - 1);
    f21 %= MOD;
  }
  // cout << f11 << " " << f12 << " " << f21 << " " << f22 << endl;
  cout << (((f22 - f21 - f12 + f11) % MOD + MOD) % MOD) << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 404404;
int fac[N], ifac[N];
int p = 1e9 + 7;
ll ans = 0;
ll power(int a, int b) {
  int sum = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      sum = 1ll * sum * a % p;
    a = 1ll * a * a % p;
  }
  return sum;
}
ll C(int n, int m) { return 1ll * fac[n] * ifac[m] % p * ifac[n - m] % p; }
ll calc(int n, int m) {
  if (n < 0 || m < 0)
    return 0;
  ll sum = 0;
  for (int i = 1; i <= n + 1; i++)
    sum = (sum + C(i + m, i)) % p;
  return sum % p;
}

int main() {
  int n1, m1, n2, m2;
  scanf("%d%d%d%d", &n1, &m1, &n2, &m2);
  int n = n2 + m2 + 1;
  ifac[0] = fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % p;
  ifac[n] = power(fac[n], p - 2);
  for (int i = n - 1; i >= 1; i--)
    ifac[i] = 1ll * ifac[i + 1] * (i + 1) % p;
  ll ans =
      calc(n2, m2) - calc(n2, m1 - 1) - calc(n1 - 1, m2) + calc(n1 - 1, m1 - 1);
  printf("%lld\n", (ans % p + p) % p);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 2022202;
int fac[N], ifac[N];
int p = 1e9 + 7;
ll ans = 0;
ll power(int a, int b) {
  int sum = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      sum = 1ll * sum * a % p;
    a = 1ll * a * a % p;
  }
  return sum;
}
ll C(int n, int m) { return 1ll * fac[n] * ifac[m] % p * ifac[n - m] % p; }
ll calc(int n, int m) {
  if (n < 0 || m < 0)
    return 0;
  ll sum = 0;
  for (int i = 1; i <= n + 1; i++)
    sum = (sum + C(i + m, i)) % p;
  return sum % p;
}

int main() {
  int n1, m1, n2, m2;
  scanf("%d%d%d%d", &n1, &m1, &n2, &m2);
  int n = n2 + m2 + 1;
  ifac[0] = fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % p;
  ifac[n] = power(fac[n], p - 2);
  for (int i = n - 1; i >= 1; i--)
    ifac[i] = 1ll * ifac[i + 1] * (i + 1) % p;
  ll ans =
      calc(n2, m2) - calc(n2, m1 - 1) - calc(n1 - 1, m2) + calc(n1 - 1, m1 - 1);
  printf("%lld\n", (ans % p + p) % p);
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
const int Max_Num = 1e5 + 10;
const LL mod = 1e9 + 7;
LL F[Max_Num], Finv[Max_Num];

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n) {
    if (n & 1)
      r = r * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return r;
}

LL modinv(LL x) {
  LL m = mod, u = 1, v = 0;
  while (m) {
    LL t = x / m;
    x -= t * m;
    swap(x, m);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

void fact() {
  F[0] = 1;
  rep(i, Max_Num - 1) {
    F[i + 1] = F[i] * (i + 1);
    F[i + 1] %= mod;
  }
  rep(i, Max_Num) Finv[i] = modinv(F[i]);
}

LL calc(LL r, LL c) {
  return F[r + c + 2] * Finv[c + 1] % mod * Finv[r + 1] % mod;
}

int main() {
  fact();
  LL r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  LL ans =
      calc(r2, c2) - calc(r1 - 1, c2) - calc(r2, c1 - 1) + calc(r1 - 1, c1 - 1);
  ans %= mod;
  while (ans < 0)
    ans += mod;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
const int Max_Num = 2e6 + 10;
const LL mod = 1e9 + 7;
LL F[Max_Num], Finv[Max_Num];

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n) {
    if (n & 1)
      r = r * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return r;
}

LL modinv(LL x) {
  LL m = mod, u = 1, v = 0;
  while (m) {
    LL t = x / m;
    x -= t * m;
    swap(x, m);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

void fact() {
  F[0] = 1;
  rep(i, Max_Num - 1) {
    F[i + 1] = F[i] * (i + 1);
    F[i + 1] %= mod;
  }
  rep(i, Max_Num) Finv[i] = modinv(F[i]);
}

LL calc(LL r, LL c) {
  return F[r + c + 2] * Finv[c + 1] % mod * Finv[r + 1] % mod;
}

int main() {
  fact();
  LL r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  LL ans =
      calc(r2, c2) - calc(r1 - 1, c2) - calc(r2, c1 - 1) + calc(r1 - 1, c1 - 1);
  ans %= mod;
  while (ans < 0)
    ans += mod;
  cout << ans << endl;

  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int nmax = 2000100;
const ll mod = 1e9 + 7;
ll fac[nmax], finv[nmax], inv[nmax];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < nmax; 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 || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
int main() {
  COMinit();
  int r1, c1, r, c;
  cin >> r1 >> c1 >> r >> c;
  vector<vector<ll>> sum(r + 1, vector<ll>(c + 1));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      sum[i][j] = com(i + j, i);
    }
  }
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j < c; j++) {
      sum[i][j + 1] += sum[i][j];
      sum[i][j + 1] %= mod;
    }
  }
  for (int i = 0; i <= c; i++) {
    for (int j = 0; j < r; j++) {
      sum[j + 1][i] += sum[j][i];
      sum[j + 1][i] %= mod;
    }
  }
  cout << (sum[r][c] - sum[r1 - 1][c] - sum[r][c1 - 1] + sum[r1 - 1][c1 - 1] +
           mod * 2) %
              mod
       << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int nmax = 2000100;
const ll mod = 1e9 + 7;
ll fac[nmax], finv[nmax], inv[nmax];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < nmax; 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 || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
int main() {
  COMinit();
  int r1, c1, r, c;
  cin >> r1 >> c1 >> r >> c;
  cout << (com(r + 1 + c + 1, r + 1) - com(r + 1 + c1, c1) -
           com(r1 + c + 1, r1) + com(r1 + c1, r1) + 2 * mod) %
              mod
       << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mypow(long a, long long b) {
  long long c;
  if (b == 0)
    return 1;
  else {
    c = mypow(a, b / 2) * mypow(a, b / 2) % mod;
    if (b % 2 == 1)
      c = c * a % mod;
    return c;
  }
}
vector<long long> fac(200001);
vector<long long> minv(200001);
void myfact(long n) {
  long i;
  fac.at(0) = 1;
  minv.at(0) = 1;
  for (i = 1; i <= n; i++) {
    fac.at(i) = i * fac.at(i - 1) % mod;
    minv.at(i) = mypow(fac.at(i), mod - 2) % mod;
  }
}
long long mycomb(long n, long r) {
  long long c;
  c = fac.at(n) * minv.at(r) % mod * minv.at(n - r) % mod;
  return c;
}
int main() {
  long r1, c1, r2, c2;
  long long a;
  cin >> r1 >> c1 >> r2 >> c2;
  myfact(r2 + c2 + 2);
  a = (mycomb(r2 + c2 + 2, r2 + 1) - mycomb(r2 + c1 + 1, r2 + 1) -
       mycomb(r1 + c2 + 1, r1) + mycomb(r1 + c1, r1)) %
      mod;
  if (a < 0)
    a += mod;
  cout << a << endl;
}
// 1≤r1≤r2≤10^6
// 1≤c1≤c2≤10^6

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mypow(long a, long long b) {
  long long c;
  if (b == 0)
    return 1;
  else {
    c = mypow(a, b / 2) * mypow(a, b / 2) % mod;
    if (b % 2 == 1)
      c = c * a % mod;
    return c;
  }
}
vector<long long> fac(2000003);
vector<long long> minv(2000003);
void myfact(long n) {
  long i;
  fac.at(0) = 1;
  minv.at(0) = 1;
  for (i = 1; i <= n; i++) {
    fac.at(i) = i * fac.at(i - 1) % mod;
    minv.at(i) = mypow(fac.at(i), mod - 2) % mod;
  }
}
long long mycomb(long n, long r) {
  long long c;
  c = fac.at(n) * minv.at(r) % mod * minv.at(n - r) % mod;
  return c;
}
int main() {
  long r1, c1, r2, c2;
  long long a;
  cin >> r1 >> c1 >> r2 >> c2;
  myfact(r2 + c2 + 2);
  a = (mycomb(r2 + c2 + 2, r2 + 1) - mycomb(r2 + c1 + 1, r2 + 1) -
       mycomb(r1 + c2 + 1, r1) + mycomb(r1 + c1, r1)) %
      mod;
  if (a < 0)
    a += mod;
  cout << a << endl;
}
// 1≤r1≤r2≤10^6
// 1≤c1≤c2≤10^6
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 1000005

ll r1, c1, r2, c2;
ll fact[SIZE], inv_fact[SIZE];

ll mod_pow(ll x, ll count, ll mod) {

  if (count == 0)
    return 1;
  ll ret = mod_pow((x * x) % mod, count / 2, mod);
  if (count % 2 == 1) {

    ret = (ret * x) % mod;
  }
  return ret;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll nCk(ll n, ll k) {

  ll ret = fact[n] * inv_fact[k];
  ret %= MOD;
  ret *= inv_fact[n - k];

  return ret % MOD;
}

int main() {

  fact[0] = 1;
  for (ll i = 1; i < SIZE; i++) {
    fact[i] = i * fact[i - 1];
    fact[i] %= MOD;
  }
  inv_fact[SIZE - 1] = mod_inverse(fact[SIZE - 1], MOD);
  for (ll i = SIZE - 1; i >= 1; i--) {

    inv_fact[i - 1] = inv_fact[i] * i;
    inv_fact[i - 1] %= MOD;
  }

  scanf("%lld %lld %lld %lld", &r1, &c1, &r2, &c2);

  ll ans = 0;
  for (ll i = c1; i <= c2; i++) {

    ll tmp = nCk((r2 + i + 1), i + 1);

    tmp -= nCk((r1 + i), i + 1);

    if (tmp < 0) {

      tmp += MOD;
    }
    ans += tmp;
    ans %= MOD;
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 2000005

ll r1, c1, r2, c2;
ll fact[SIZE], inv_fact[SIZE];

ll mod_pow(ll x, ll count, ll mod) {

  if (count == 0)
    return 1;
  ll ret = mod_pow((x * x) % mod, count / 2, mod);
  if (count % 2 == 1) {

    ret = (ret * x) % mod;
  }
  return ret;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll nCk(ll n, ll k) {

  ll ret = fact[n] * inv_fact[k];
  ret %= MOD;
  ret *= inv_fact[n - k];

  return ret % MOD;
}

int main() {

  fact[0] = 1;
  for (ll i = 1; i < SIZE; i++) {
    fact[i] = i * fact[i - 1];
    fact[i] %= MOD;
  }
  inv_fact[SIZE - 1] = mod_inverse(fact[SIZE - 1], MOD);
  for (ll i = SIZE - 1; i >= 1; i--) {

    inv_fact[i - 1] = inv_fact[i] * i;
    inv_fact[i - 1] %= MOD;
  }

  scanf("%lld %lld %lld %lld", &r1, &c1, &r2, &c2);

  ll ans = 0;
  for (ll i = c1; i <= c2; i++) {

    ll tmp = nCk((r2 + i + 1), i + 1);

    tmp -= nCk((r1 + i), i + 1);

    if (tmp < 0) {

      tmp += MOD;
    }
    ans += tmp;
    ans %= MOD;
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = (1e6) + 10, maxm = (1 << 18);
const double PI = acos(-1);
ll fat[maxn], inv[maxn];

ll fexp(ll a, ll b) {
  ll ans = 1;

  while (b) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b = (b >> 1);
  }
  return ans;
}

void init() {
  fat[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fat[i] = (fat[i - 1] * i) % mod;
  }
  inv[maxn - 1] = fexp(fat[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
  }
}

ll C(ll n, ll a) {
  if (n == a || a == 0)
    return 1;
  if (a > n)
    return 0;
  return ((fat[n] * inv[a]) % mod * inv[n - a]) % mod;
}

int main() {
  init();

  ll xi, xf, yi, yf;
  cin >> xi >> yi >> xf >> yf;

  ll ans = C(xf + yf + 2, xf + 1);
  ans = (ans - C(yi + xf + 1, yi) + mod) % mod;
  ans = (ans - C(xi + yf + 1, xi) + mod) % mod;
  ans = (ans + C(xi + yi, xi) + mod) % mod;

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define DEBUG

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = (2 * 1e6) + 10, maxm = (1 << 18);
const double PI = acos(-1);
ll fat[maxn], inv[maxn];

ll fexp(ll a, ll b) {
  ll ans = 1;

  while (b) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b = (b >> 1);
  }
  return ans;
}

void init() {
  fat[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fat[i] = (fat[i - 1] * i) % mod;
  }
  inv[maxn - 1] = fexp(fat[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = (inv[i + 1] * (i + 1)) % mod;
  }
}

ll C(ll n, ll a) {
  if (n == a || a == 0)
    return 1;
  if (a > n)
    return 0;
  return ((fat[n] * inv[a]) % mod * inv[n - a]) % mod;
}

int main() {
  init();

  ll xi, xf, yi, yf;
  cin >> xi >> yi >> xf >> yf;

  ll ans = C(xf + yf + 2, xf + 1);
  ans = (ans - C(yi + xf + 1, yi) + mod) % mod;
  ans = (ans - C(xi + yf + 1, xi) + mod) % mod;
  ans = (ans + C(xi + yi, xi) + mod) % mod;

  cout << ans << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int mod = 1e9 + 7;

map<pair<long long, long long>, long long> mp;
long long modpow(long long x, long long y) {
  if (mp[{x, y}])
    return mp[{x, y}];
  if (y == 0)
    return 1;
  if (y % 2) {
    long long res = x * modpow(x, y - 1) % mod;
    mp[{x, y}] = res;
    return res;
  }
  long long res = modpow(x, y / 2) * modpow(x, y / 2) % mod;
  mp[{x, y}] = res;
  return res;
}

int main() {
  long long ans = 0;
  vector<long long> fac(1e7, 1);
  for (int i = 2; i < 1e7; i++)
    fac[i] = fac[i - 1] * i % mod;
  vector<long long> inv(1e7, 1);
  for (int i = 2; i < 1e7; i++)
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  for (int r = r1; r <= r2; r++) {
    ans += (mod +
            (fac[r + c2 + 1] * modpow(fac[r + 1], mod - 2) % mod) *
                modpow(fac[c2], mod - 2) % mod -
            (fac[r + c1] * modpow(fac[r + 1], mod - 2) % mod) *
                modpow(fac[c1 - 1], mod - 2) % mod) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int mod = 1e9 + 7;

map<pair<long long, long long>, long long> mp;
long long modpow(long long x, long long y) {
  if (mp[{x, y}])
    return mp[{x, y}];
  if (y == 0)
    return 1;
  if (y % 2) {
    long long res = x * modpow(x, y - 1) % mod;
    mp[{x, y}] = res;
    return res;
  }
  long long res = modpow(x, y / 2) * modpow(x, y / 2) % mod;
  mp[{x, y}] = res;
  return res;
}

int main() {
  long long ans = 0;
  vector<long long> fac(1e7, 1);
  for (int i = 2; i < 1e7; i++)
    fac[i] = fac[i - 1] * i % mod;
  vector<long long> inv(1e7, 1);
  for (int i = 2; i < 1e7; i++)
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long a = (fac[r1 + c2 + 1] * modpow(fac[r1 + 1], mod - 2) % mod) *
                modpow(fac[c2], mod - 2) % mod;
  long long b = (fac[r1 + c1] * modpow(fac[r1 + 1], mod - 2) % mod) *
                modpow(fac[c1 - 1], mod - 2) % mod;
  ans += (mod + a - b) % mod;
  ans %= mod;
  for (int r = r1 + 1; r <= r2; r++) {
    a *= r + c2 + 1;
    a %= mod;
    a *= inv[r + 1];
    a %= mod;
    b *= r + c1;
    b %= mod;
    b *= inv[r + 1];
    b %= mod;
    ans += (mod + a - b) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

const ll MAX = 1010010;
const ll mod = 1000000007;
vector<ll> fac(MAX), finv(MAX), inv(MAX);
bool init_called = false;
//=============modinv============================
void COMinit() {
  init_called = true;
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (ll i = 2; i < MAX; i++) {
    fac.at(i) = fac.at(i - 1) * i % mod;
    inv.at(i) = mod - inv.at(mod % i) * (mod / i) % mod;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % mod;
  }
}

ll COM(ll n, ll k) {
  if (!init_called) {
    COMinit();
  }
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * (finv.at(k) * finv.at(n - k) % mod) % mod;
}
//=================================================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if ((r1 == r2) && (c1 == c2)) {
    cout << COM(r1 + c1, r1) << "\n";
    return 0;
  }
  ll ans = (COM(r1 + c1, r1) + COM(r2 + c2, r2)) % mod;
  if ((r1 != r2) && (c1 != c2)) {
    ans = (ans + COM(r2 + c2, r2)) % mod;
  }
  for (ll i = r1 + 1; i < r2; ++i) {
    ans = (ans + COM(i + c2, c2)) % mod;
  }
  for (ll i = c1 + 1; i < c2; ++i) {
    ans = (ans + COM(r2 + i, r2)) % mod;
  }
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

const ll MAX = 2010010;
const ll mod = 1000000007;
vector<ll> fac(MAX), finv(MAX), inv(MAX);
bool init_called = false;
//=============modinv============================
void COMinit() {
  init_called = true;
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (ll i = 2; i < MAX; i++) {
    fac.at(i) = fac.at(i - 1) * i % mod;
    inv.at(i) = mod - inv.at(mod % i) * (mod / i) % mod;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % mod;
  }
}

ll COM(ll n, ll k) {
  if (!init_called) {
    COMinit();
  }
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * (finv.at(k) * finv.at(n - k) % mod) % mod;
}
//=================================================

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if ((r1 == r2) && (c1 == c2)) {
    cout << COM(r1 + c1, r1) << "\n";
    return 0;
  }
  ll ans = (COM(r1 + c1, r1) + COM(r2 + c2, r2)) % mod;
  if ((r1 != r2) && (c1 != c2)) {
    ans = (ans + COM(r2 + c2, r2)) % mod;
  }
  for (ll i = r1 + 1; i < r2; ++i) {
    ans = (ans + COM(i + c2, c2)) % mod;
  }
  for (ll i = c1 + 1; i < c2; ++i) {
    ans = (ans + COM(r2 + i, r2)) % mod;
  }
  cout << ans << "\n";
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll ANS;
const ll MAX = 1000005;
const ll MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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(ll i, ll j) {
  if (i < j)
    return 0;
  if (i < 0 || j < 0)
    return 0;
  return fac[i] * (finv[j] * finv[i - j] % MOD) % MOD;
}
int main(void) {
  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  for (ll i = 1; i <= r2 + 1; i++) {
    ANS = (ANS + COM(i + c2, i)) % MOD;
  }
  for (int i = 1; i <= r1; i++) {
    ANS = (ANS + COM(i + c1 - 1, i)) % MOD;
  }
  for (int i = 1; i <= r1; i++) {
    ANS = (ANS - COM(i + c2, i)) % MOD;
  }
  for (int i = 1; i <= r2 + 1; i++) {
    ANS = (ANS - COM(i + c1 - 1, i)) % MOD;
  }
  if (ANS < 0)
    ANS += MOD;
  cout << ANS << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll ANS;
const ll MAX = 2000005;
const ll MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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(ll i, ll j) {
  if (i < j)
    return 0;
  if (i < 0 || j < 0)
    return 0;
  return fac[i] * (finv[j] * finv[i - j] % MOD) % MOD;
}
int main(void) {
  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  for (ll i = 1; i <= r2 + 1; i++) {
    ANS = (ANS + COM(i + c2, i)) % MOD;
  }
  for (int i = 1; i <= r1; i++) {
    ANS = (ANS + COM(i + c1 - 1, i)) % MOD;
  }
  for (int i = 1; i <= r1; i++) {
    ANS = (ANS - COM(i + c2, i)) % MOD;
  }
  for (int i = 1; i <= r2 + 1; i++) {
    ANS = (ANS - COM(i + c1 - 1, i)) % MOD;
  }
  if (ANS < 0)
    ANS += MOD;
  cout << ANS << endl;
}
Problem p02782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
ll d[2000010];
ll go(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y == 1)
    return x;
  else if (y == 2)
    return x * x % mod;
  else if (y & 1) {
    return (x * go(x, y - 1)) % mod;
  } else {
    ll tmp = go(x, y / 2) % mod;
    return (tmp * tmp) % mod;
  }
}
ll go2(ll x, ll y) {
  if (x == 0 && y == 0)
    return 0;
  ll cnt = d[x];
  ll tmp = d[y] * (d[x - y]) % mod;
  tmp = go(tmp, mod - 2);
  cnt *= tmp;
  cnt %= mod;
  return cnt;
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  ll r, c, r1, c1;
  cin >> r >> c >> r1 >> c1;
  ll ans = 0;
  d[0] = 1;
  d[1] = 1;
  for (ll i = 2; i <= 2000002; i++) {
    d[i] = d[i - 1] * i;
    d[i] %= mod;
  }

  for (int i = r; i <= r1; i++) {
    ll x = i + 1, y = c1;
    ll cnt = go2(x + y, x);
    x = i + 1;
    y = c - 1;
    ll tmp = go2(x + y, x);
    tmp = (cnt - tmp);
    while (tmp < 0)
      tmp += mod;
    ans += tmp;
    ans %= mod;
  }

  cout << ans << '\n';
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
ll d[2000010];
ll go(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y == 1)
    return x;
  else if (y == 2)
    return x * x % mod;
  else if (y & 1) {
    return (x * go(x, y - 1)) % mod;
  } else {
    ll tmp = go(x, y / 2) % mod;
    return (tmp * tmp) % mod;
  }
}
ll go2(ll x, ll y) {
  if (x == 0 && y == 0)
    return 0;
  ll cnt = d[x];
  ll tmp = d[y] * (d[x - y]) % mod;
  tmp = go(tmp, mod - 2);
  cnt *= tmp;
  cnt %= mod;
  return cnt;
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  ll r, c, r1, c1;
  cin >> r >> c >> r1 >> c1;
  ll ans = 0;
  d[0] = 1;
  d[1] = 1;
  for (ll i = 2; i <= 2000002; i++) {
    d[i] = d[i - 1] * i;
    d[i] %= mod;
  }

  ans = go2(r1 + c1 + 2, r1 + 1);
  ll tmp = go2(c1 + r + 1, r);
  ans -= tmp;
  while (ans < 0)
    ans += mod;
  ans %= mod;
  tmp = go2(r1 + c + 1, r1 + 1);
  tmp -= go2(r + c, r);
  while (tmp < 0)
    tmp += mod;
  ans -= tmp;
  while (ans < 0)
    ans += mod;

  cout << ans << '\n';
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;

const int N = 1000000 + 10;
const int MOD = 1000000007;

int f[N], fr[N], p[N];

int exp(int x, int n) {
  int tmp;
  if (n == 0)
    return 1;
  tmp = exp(x, n / 2);
  tmp = 1LL * tmp * tmp % MOD;
  if (n % 2)
    tmp = 1LL * tmp * x % MOD;
  return tmp;
}

int calc(int n, int m) { return 1LL * f[n] * fr[m] % MOD * fr[n - m] % MOD; }

int main() {
  int res, r1, c1, r2, c2, n, m, a, b, i, j;
  cin >> r1 >> c1 >> r2 >> c2;
  n = r2 + c2 + 2;
  f[0] = fr[0] = p[0] = 1;
  for (i = 1; i <= n; i++) {
    f[i] = 1LL * f[i - 1] * i % MOD;
    fr[i] = exp(f[i], MOD - 2);
    p[i] = p[i - 1] * 2LL % MOD;
  }
  a = (calc(r2 + c2 + 2, r2 + 1) + calc(r1 + c1, r1)) % MOD;
  b = (calc(r1 + c2 + 1, r1) + calc(r2 + c1 + 1, c1)) % MOD;
  res = ((a - b) % MOD + MOD) % MOD;
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;

const int N = 2000000 + 10;
const int MOD = 1000000007;

int f[N], fr[N], p[N];

int exp(int x, int n) {
  int tmp;
  if (n == 0)
    return 1;
  tmp = exp(x, n / 2);
  tmp = 1LL * tmp * tmp % MOD;
  if (n % 2)
    tmp = 1LL * tmp * x % MOD;
  return tmp;
}

int calc(int n, int m) { return 1LL * f[n] * fr[m] % MOD * fr[n - m] % MOD; }

int main() {
  int res, r1, c1, r2, c2, n, m, a, b, i, j;
  cin >> r1 >> c1 >> r2 >> c2;
  n = r2 + c2 + 2;
  f[0] = fr[0] = p[0] = 1;
  for (i = 1; i <= n; i++) {
    f[i] = 1LL * f[i - 1] * i % MOD;
    fr[i] = exp(f[i], MOD - 2);
    p[i] = p[i - 1] * 2LL % MOD;
  }
  a = (calc(r2 + c2 + 2, r2 + 1) + calc(r1 + c1, r1)) % MOD;
  b = (calc(r1 + c2 + 1, r1) + calc(r2 + c1 + 1, c1)) % MOD;
  res = ((a - b) % MOD + MOD) % MOD;
  cout << res << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define It set<node>::iterator
#define endl '\n'
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int r1, c1, r2, c2, ans;
int val[maxn], inv[maxn];

inline int qpow(int a, int x) {
  int res = 1;
  while (x) {
    if (x & 1)
      res = a * res % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return res;
}

inline int Inv(int dat) { return qpow(dat, mod - 2); }

inline int calc(int a, int b) {
  if (b == 0)
    return 1;
  return (val[a] * inv[b] % mod) * inv[a - b] % mod;
}

signed main() {
  cin >> r1 >> c1 >> r2 >> c2;
  val[1] = 1;
  for (int i = 2; i <= r2 + c2 + 5; ++i)
    val[i] = val[i - 1] * i % mod;
  for (int i = 1; i <= r2 + c2 + 5; ++i)
    inv[i] = Inv(val[i]);
  //	for(int i=1;i<=r2+c2+5;++i) cout<<inv[i]<<" ";
  //	cout<<endl;
  for (int i = r1 + 1; i <= r2 + 1; ++i) {
    ans = (ans + calc(i + c2, c2) % mod) % mod;
    //		cout<<i+c2<<" "<<c2<<" "<<calc(i+c2,c2)<<endl;
  }
  for (int i = r1 + 1; i <= r2 + 1; ++i) {
    ans = (ans - calc(i + c1 - 1, c1 - 1) % mod + mod) % mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define It set<node>::iterator
#define endl '\n'
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 3e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int r1, c1, r2, c2, ans;
int val[maxn], inv[maxn];

inline int qpow(int a, int x) {
  int res = 1;
  while (x) {
    if (x & 1)
      res = a * res % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return res;
}

inline int Inv(int dat) { return qpow(dat, mod - 2); }

inline int calc(int a, int b) {
  if (b == 0)
    return 1;
  return (val[a] * inv[b] % mod) * inv[a - b] % mod;
}

signed main() {
  cin >> r1 >> c1 >> r2 >> c2;
  val[1] = 1;
  for (int i = 2; i <= r2 + c2 + 5; ++i)
    val[i] = val[i - 1] * i % mod;
  for (int i = 1; i <= r2 + c2 + 5; ++i)
    inv[i] = Inv(val[i]);
  //	for(int i=1;i<=r2+c2+5;++i) cout<<inv[i]<<" ";
  //	cout<<endl;
  for (int i = r1 + 1; i <= r2 + 1; ++i) {
    ans = (ans + calc(i + c2, c2) % mod) % mod;
    //		cout<<i+c2<<" "<<c2<<" "<<calc(i+c2,c2)<<endl;
  }
  for (int i = r1 + 1; i <= r2 + 1; ++i) {
    ans = (ans - calc(i + c1 - 1, c1 - 1) % mod + mod) % mod;
  }
  cout << ans << endl;
  return 0;
}