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 + 7, M = 1e9 + 7;

LL fac[N], invfac[N];

LL power(LL a, LL p) {
  if (p == 0)
    return 1;
  LL ans = power(a, p / 2);
  ans = (ans * ans) % M;
  if (p % 2)
    ans = (ans * a) % M;
  return ans;
}

void pre() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  invfac[N - 1] = power(fac[N - 1], M - 2);
  for (int i = N - 2; i >= 0; i--)
    invfac[i] = (invfac[i + 1] * (i + 1)) % M;
}

LL C(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  LL num = fac[n];
  LL denom = (invfac[r] * invfac[n - r]) % M;
  return (num * denom) % M;
}

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

  pre();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;

  LL ans = C(r2 + c2 + 2, r2 + 1) - C(r1 + c2 + 2, r1 + 1) -
           C(r2 + c1 + 2, r2 + 1) + C(r1 + c1 + 2, r1 + 1);
  ans = (ans % M + M) % M;
  cout << ans << endl;
}


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

typedef long long LL;
const int N = 2e6 + 7, M = 1e9 + 7;

LL fac[N], invfac[N];

LL power(LL a, LL p) {
  if (p == 0)
    return 1;
  LL ans = power(a, p / 2);
  ans = (ans * ans) % M;
  if (p % 2)
    ans = (ans * a) % M;
  return ans;
}

void pre() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  invfac[N - 1] = power(fac[N - 1], M - 2);
  for (int i = N - 2; i >= 0; i--)
    invfac[i] = (invfac[i + 1] * (i + 1)) % M;
}

LL C(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  LL num = fac[n];
  LL denom = (invfac[r] * invfac[n - r]) % M;
  return (num * denom) % M;
}

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

  pre();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;

  LL ans = C(r2 + c2 + 2, r2 + 1) - C(r1 + c2 + 2, r1 + 1) -
           C(r2 + c1 + 2, r2 + 1) + C(r1 + c1 + 2, r1 + 1);
  ans = (ans % M + M) % M;
  cout << ans << endl;
}
Problem p02782 (C++)

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

const long long MOD = 1000000007;

typedef long long LL;
LL n, k;
const LL mof = 1e9 + 7;
LL p = mof;
const LL maxn = 1e6;
inline LL pow_mod(LL a, LL k, LL mo) {
  LL ans = 1;
  while (k) {
    if (k % 2)
      ans = ans * a % mo;
    a = a * a % mo;
    k >>= 1;
  }
  return ans % mo;
}
LL jc[maxn + 7];
inline LL inv(LL x) { return pow_mod(x, mof - 2, mof); }
LL Inv[maxn + 7];
LL C(LL N, LL k) {
  if (k > N || k < 0)
    return 0;
  if (k == 0 || k == N)
    return 1;
  LL ans = 1;
  ans = ans * jc[N] * Inv[k] % mof * Inv[N - k] % mof;
  return ans;
}
void init() {
  jc[1] = jc[0] = 1;
  for (LL i = 2; i <= maxn; ++i)
    jc[i] = jc[i - 1] * i % mof;
  for (int i = 0; i <= maxn; ++i)
    Inv[i] = inv(jc[i]);
}
void solve(int r1, int r2, int c1, int c2) {
  auto ret = C(c2 + r2 + 2, c2 + 1) - C(c2 + r1 + 1, c2 + 1) -
             C(c1 + r2 + 1, c1) + C(c1 + r1, c1);
  ret = (ret % MOD + MOD) % MOD;
  cout << ret << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  init();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  solve(r1, r2, c1, c2);
  return 0;
}


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

const long long MOD = 1000000007;

typedef long long LL;
LL n, k;
const LL mof = 1e9 + 7;
const LL maxn = 2e6 + 5;
inline LL pow_mod(LL a, LL k, LL mo) {
  LL ans = 1;
  while (k) {
    if (k % 2)
      ans = ans * a % mo;
    a = a * a % mo;
    k >>= 1;
  }
  return ans % mo;
}
LL jc[maxn + 7];
inline LL inv(LL x) { return pow_mod(x, mof - 2, mof); }
LL Inv[maxn + 7];
LL C(LL N, LL k) {
  if (k > N || k < 0)
    return 0;
  if (k == 0 || k == N)
    return 1;
  LL ans = 1;
  ans = ans * jc[N] * Inv[k] % mof * Inv[N - k] % mof;
  return ans;
}
void init() {
  jc[1] = jc[0] = 1;
  for (LL i = 2; i <= maxn; ++i)
    jc[i] = jc[i - 1] * i % mof;
  for (int i = 0; i <= maxn; ++i)
    Inv[i] = inv(jc[i]);
}
void solve(int r1, int r2, int c1, int c2) {
  auto ret = C(c2 + r2 + 2, c2 + 1) - C(c2 + r1 + 1, c2 + 1) -
             C(c1 + r2 + 1, c1) + C(c1 + r1, c1);
  ret = (ret % MOD + MOD) % MOD;
  cout << ret << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  init();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  solve(r1, r2, c1, c2);
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const ll inf = 1e9 + 7;
const ll INF = 1e18;
const ll mod = 998244353;

const int MAX = 1000010;

vll fac(MAX);
vll finv(MAX);
vll inv(MAX);

void combinit(ll m) {
  fac[0] = 1;
  fac[1] = 1;
  finv[0] = 1;
  finv[1] = 1;
  inv[1] = 1;
  rep2(i, 2, MAX) {
    fac[i] = fac[i - 1] * i % m;
    inv[i] = m - inv[m % i] * (m / i) % m;
    finv[i] = finv[i - 1] * inv[i] % m;
  }
}

ll combi(ll n, ll k, ll m = inf) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % m) % m;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  combinit(inf);
  ll ans = (ll)0, tmp;
  for (ll j = c1; j <= c2; j++) {
    tmp = combi(r2 + j + (ll)1, r2) - combi(r1 + j, r1 - (ll)1);
    if (tmp < 0)
      tmp += inf;
    ans += tmp;
  }
  cout << ans % inf << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const ll inf = 1e9 + 7;
const ll INF = 1e18;
const ll mod = 998244353;

const int MAX = 2000010;

vll fac(MAX);
vll finv(MAX);
vll inv(MAX);

void combinit(ll m) {
  fac[0] = 1;
  fac[1] = 1;
  finv[0] = 1;
  finv[1] = 1;
  inv[1] = 1;
  rep2(i, 2, MAX) {
    fac[i] = fac[i - 1] * i % m;
    inv[i] = m - inv[m % i] * (m / i) % m;
    finv[i] = finv[i - 1] * inv[i] % m;
  }
}

ll combi(ll n, ll k, ll m = inf) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % m) % m;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  combinit(inf);
  ll ans = (ll)0, tmp;
  for (ll j = c1; j <= c2; j++) {
    tmp = combi(r2 + j + (ll)1, r2) - combi(r1 + j, r1 - (ll)1);
    if (tmp < 0)
      tmp += inf;
    ans += tmp;
  }
  cout << ans % inf << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
const int M = 1e9 + 7;
int n, m;

int sum[maxn];
int inv[maxn];
int sinv[maxn];

int solve(int x, int y) {
  int ans = 0;
  for (int i = 0; i <= x; i++) {
    ans = (ans + (sum[i + y + 1] * sinv[i + 1]) % M) % M;
  }
  return ans * sinv[y] % M;
}

signed main() {
  sum[0] = sum[1] = 1;
  inv[0] = inv[1] = 1;
  sinv[0] = sinv[1] = 1;
  for (int i = 2; i < maxn; i++) {
    sum[i] = sum[i - 1] * i % M;
    inv[i] = (M - M / i) * inv[M % i] % M;
    sinv[i] = (inv[i] * sinv[i - 1]) % M;
  }
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ans = solve(r2, c2);
  int a = 0, b = 0, c = 0;
  if (r1)
    a = solve(r1 - 1, c2);
  if (c1)
    b = solve(r2, c1 - 1);
  if (r1 && c1)
    c = solve(r1 - 1, c1 - 1);
  // cout<<ans<<' '<<a<<' '<<b<<' '<<c<<endl;
  ans = ((ans - a - b + c) % M + M) % M;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define pii pair<int, int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 2001110;
const int M = 1e9 + 7;
int n, m;

int sum[maxn];
int inv[maxn];
int sinv[maxn];

int solve(int x, int y) {
  int ans = 0;
  for (int i = 0; i <= x; i++) {
    ans = (ans + (sum[i + y + 1] * sinv[i + 1]) % M) % M;
  }
  return ans * sinv[y] % M;
}

signed main() {
  sum[0] = sum[1] = 1;
  inv[0] = inv[1] = 1;
  sinv[0] = sinv[1] = 1;
  for (int i = 2; i < maxn; i++) {
    sum[i] = sum[i - 1] * i % M;
    inv[i] = (M - M / i) * inv[M % i] % M;
    sinv[i] = (inv[i] * sinv[i - 1]) % M;
  }
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ans = solve(r2, c2);
  int a = 0, b = 0, c = 0;
  if (r1)
    a = solve(r1 - 1, c2);
  if (c1)
    b = solve(r2, c1 - 1);
  if (r1 && c1)
    c = solve(r1 - 1, c1 - 1);
  // cout<<ans<<' '<<a<<' '<<b<<' '<<c<<endl;
  ans = ((ans - a - b + c) % M + M) % M;
  cout << ans << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
// ABEY SAALE
// QUESTION KHUD KR SOLUTION KYU DEKH RHA H

// MAAF KIJIYEGA ME GUSSE ME IDHAR UDHAR NIKAL JATA HU
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;

ll fac[2000005];
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 2000005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE

signed main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  senky_bansal IIIT_ALLAHABAD

  pre();
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    ll ans = 0;
    for (int i = c1; i <= c2; i++) {
      ll p1 = (ncr(r2 + i + 1, i + 1) - ncr(r1 + i, i + 1) + mod) % mod;
      p1 += mod;
      p1 %= mod;
      ans += p1;
      ans %= mod;
    }
    cout << ans % mod;
  }
}


Fixed submission:
// ABEY SAALE
// QUESTION KHUD KR SOLUTION KYU DEKH RHA H

// MAAF KIJIYEGA ME GUSSE ME IDHAR UDHAR NIKAL JATA HU
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;

ll fac[2000005];
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 2000005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE

signed main() {

  senky_bansal IIIT_ALLAHABAD

  pre();
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    ll r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    ll ans = 0;
    for (int i = c1; i <= c2; i++) {
      ll p1 = (ncr(r2 + i + 1, i + 1) - ncr(r1 + i, i + 1) + mod) % mod;
      p1 += mod;
      p1 %= mod;
      ans += p1;
      ans %= mod;
    }
    cout << ans % mod;
  }
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

const int mod = 1'000'000'007;
const int size_max = 1'000'005;

long long fac[size_max], finv[size_max];

long long mpow(int a, int b);
void com_init();
long long com(int n, int k);

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;

  com_init();

  long long ans = com(r2 + c2 + 2, r2 + 1) - 1;
  ans -= com(r1 + c2 + 2, r1 + 1) - 1;
  ans -= com(r2 + c1 + 2, r2 + 1) - 1;
  ans += com(r1 + c1 + 2, r1 + 1) - 1;
  ans += mod;
  ans %= mod;

  cout << ans << endl;

  return 0;
}

long long mpow(int a, int b) {
  long long t = a;
  long long ans = 1;
  for (int i = 0; i < 31; i++) {
    if ((b & 1) == 1) {
      ans *= t;
      ans %= mod;
    }
    t *= t;
    t %= mod;
    b >>= 1;
  }

  return ans;
}

void com_init() {
  fac[0] = fac[1] = 1;

  for (int i = 2; i < size_max; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }

  finv[size_max - 1] = mpow(fac[size_max - 1], mod - 2);

  for (int i = size_max - 2; i >= 2; i--) {
    finv[i] = finv[i + 1] * (i + 1) % mod;
  }
  finv[0] = finv[1] = 1;
}

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

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

const int mod = 1'000'000'007;
const int size_max = 2'000'005;

long long fac[size_max], finv[size_max];

long long mpow(int a, int b);
void com_init();
long long com(int n, int k);

int main() {
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  c1--;

  com_init();

  long long ans = com(r2 + c2 + 2, r2 + 1) - 1;
  ans -= com(r1 + c2 + 2, r1 + 1) - 1;
  ans -= com(r2 + c1 + 2, r2 + 1) - 1;
  ans += com(r1 + c1 + 2, r1 + 1) - 1;
  ans += mod;
  ans %= mod;

  cout << ans << endl;

  return 0;
}

long long mpow(int a, int b) {
  long long t = a;
  long long ans = 1;
  for (int i = 0; i < 31; i++) {
    if ((b & 1) == 1) {
      ans *= t;
      ans %= mod;
    }
    t *= t;
    t %= mod;
    b >>= 1;
  }

  return ans;
}

void com_init() {
  fac[0] = fac[1] = 1;

  for (int i = 2; i < size_max; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }

  finv[size_max - 1] = mpow(fac[size_max - 1], mod - 2);

  for (int i = size_max - 2; i >= 2; i--) {
    finv[i] = finv[i + 1] * (i + 1) % mod;
  }
  finv[0] = finv[1] = 1;
}

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;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;

// return a^b O(log b)
template <typename T> T mypow(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = mypow(a, b / 2);
  if (b % 2)
    return (((tmp * tmp) % MOD) * a) % MOD;
  else
    return (tmp * tmp) % MOD;
}
// return nCk 前処理 O(N) + query あたり O(1)
static bool init = false;
ll cb_res = 1;
const ll M = 1e6 + 10;
vll frc(M + 10, 1), inv(M + 10, 1);
long long comb(int n, int k) {
  if (!init) {
    init = true;
    REP(i, M) {
      frc[i + 1] = (frc[i] * (i + 1)) % MOD;
      inv[i + 1] = mypow(frc[i + 1], MOD - 2);
    }
  }
  if (k < 0 || n < k)
    return 0;
  cb_res = (((frc[n] * inv[k]) % MOD) * inv[n - k]) % MOD;
  return cb_res;
}

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

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

  // (0, 0) -> (r, c) : (r + c)Cr
  // sum_{c = 0, ..., c2}rCc = (r + 1)Cc2
  ll res = 0;
  for (ll r = r1; r <= r2; r++) {
    res += MOD + comb(r + 1 + c2, c2) - comb(r + 1 + c1 - 1, c1 - 1);
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;

// return a^b O(log b)
template <typename T> T mypow(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = mypow(a, b / 2);
  if (b % 2)
    return (((tmp * tmp) % MOD) * a) % MOD;
  else
    return (tmp * tmp) % MOD;
}
// return nCk 前処理 O(N) + query あたり O(1)
static bool init = false;
ll cb_res = 1;
const ll M = 2e6 + 10;
vll frc(M + 1, 1), inv(M + 1, 1);
long long comb(int n, int k) {
  if (!init) {
    init = true;
    REP(i, M) {
      frc[i + 1] = (frc[i] * (i + 1)) % MOD;
      inv[i + 1] = mypow(frc[i + 1], MOD - 2);
    }
  }
  if (k < 0 || n < k)
    return 0;
  cb_res = (((frc[n] * inv[k]) % MOD) * inv[n - k]) % MOD;
  return cb_res;
}

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

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

  // (0, 0) -> (r, c) : (r + c)Cr
  // sum_{c = 0, ..., c2}rCc = (r + 1)Cc2
  ll res = 0;
  for (ll r = r1; r <= r2; r++) {
    res += MOD + comb(r + 1 + c2, c2) - comb(r + 1 + c1 - 1, c1 - 1);
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
Problem p02782 (Python)

Buggy submission (status: Runtime Error):
r1, c1, r2, c2 = map(int, input().split())

MAX = 10**6 + 1
MOD = 10**9 + 7

# Factorial
fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1

# Inverse
inv = [0] * (MAX + 1)
inv[1] = 1

# Inverse factorial
finv = [0] * (MAX + 1)
finv[0] = 1
finv[1] = 1

for i in range(2, MAX + 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


def f(r, c):
    return fac[r + c] * finv[r] * finv[c] % MOD


ans = 0
for i in range(c1, c2 + 1):
    ans += f(r2, i + 1) - f(r1 - 1, i + 1)
    ans %= MOD
print(ans)


Fixed submission:
r1, c1, r2, c2 = map(int, input().split())

MAX = 2 * 10**6 + 1
MOD = 10**9 + 7

# Factorial
fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1

# Inverse
inv = [0] * (MAX + 1)
inv[1] = 1

# Inverse factorial
finv = [0] * (MAX + 1)
finv[0] = 1
finv[1] = 1

for i in range(2, MAX + 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


def f(r, c):
    return fac[r + c] * finv[r] * finv[c] % MOD


ans = 0
for i in range(c1, c2 + 1):
    ans += f(r2, i + 1) - f(r1 - 1, i + 1)
    ans %= MOD
print(ans)
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

constexpr int MAX = 1000001;
constexpr int MOD = 1000000007;

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

void COMinit() {
  static bool init = false;
  if (init) {
    return;
  }
  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;
  }
  init = true;
}

long long COM(int n, int k) {
  COMinit();
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int f(int r, int c) { return COM(r + c, r); }

long long g(int r, int c) {
  long long sum = 0;
  for (int j = 0; j <= r; ++j) {
    sum = (sum + f(j + 1, c)) % MOD;
  }
  return sum;
}

int main() {
  int r1, c1, r2, c2;
  std::cin >> r1 >> c1 >> r2 >> c2;
  long long result =
      (g(r2, c2) - g(r2, c1 - 1) - g(r1 - 1, c2) + g(r1 - 1, c1 - 1)) % MOD;
  if (result < 0) {
    result += MOD;
  }
  std::cout << result << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>

constexpr int MAX = 2000002;
constexpr int MOD = 1000000007;

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

void COMinit() {
  static bool init = false;
  if (init) {
    return;
  }
  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;
  }
  init = true;
}

long long COM(int n, int k) {
  COMinit();
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int f(int r, int c) { return COM(r + c, r); }

long long g(int r, int c) {
  long long sum = 0;
  for (int j = 0; j <= r; ++j) {
    sum = (sum + f(j + 1, c)) % MOD;
  }
  return sum;
}

int main() {
  int r1, c1, r2, c2;
  std::cin >> r1 >> c1 >> r2 >> c2;
  long long result =
      (g(r2, c2) - g(r2, c1 - 1) - g(r1 - 1, c2) + g(r1 - 1, c1 - 1)) % MOD;
  if (result < 0) {
    result += MOD;
  }
  std::cout << result << std::endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  int res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

const int N = 1e6 + 5;

int fac[N], inv[N];

void init(int n) {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = invert(fac[i]);
  }
}

int comb(int n, int k) {
  if (k > n)
    return 0;
  return mul(fac[n], mul(inv[k], inv[n - k]));
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  init(N);
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if (r1 > c1) {
    swap(r1, c1);
    swap(r2, c2);
  }
  int ans = 0;
  rep(r, r1, r2 + 1) {
    gadd(ans, add(comb(r + c2 + 1, r + 1), MOD - comb(r + c1, r + 1)));
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  int res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

const int N = 2e6 + 10;

int fac[N], inv[N];

void init(int n) {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = invert(fac[i]);
  }
}

int comb(int n, int k) {
  if (k > n)
    return 0;
  return mul(fac[n], mul(inv[k], inv[n - k]));
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  init(N);
  int r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  if (r1 > c1) {
    swap(r1, c1);
    swap(r2, c2);
  }
  int ans = 0;
  rep(r, r1, r2 + 1) {
    gadd(ans, add(comb(r + c2 + 1, r + 1), MOD - comb(r + c1, r + 1)));
  }
  cout << ans << '\n';
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;

#define SIZE 1000010
ll kaijo[SIZE];
void init() {
  kaijo[0] = 1;
  for (ll i = 1; i < SIZE; i++)
    kaijo[i] = (kaijo[i - 1] * i) % N;
}

ll inv(ll x) {
  ll res = 1;
  ll k = N - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % N;
    y = (y % N * y % N) % N;
    k /= 2;
  }
  return res;
}

ll Comb(ll n, ll k) {
  if (n < 0 || k < 0 || (n - k) < 0)
    return 0;
  ll b = kaijo[n];
  ll c = kaijo[n - k];
  ll d = kaijo[k];
  ll cd = (c * d) % N;
  return ((b % N) * (inv(cd)) % N) % N;
}

int main(void) {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  init();
  ll ans = 0;
  for (ll i = 0; i <= r2; i++) {
    ans = ans + Comb(i + c2 + 1, c2);
    ans = ans % N;
  }
  for (ll i = 0; i < r1; i++) {
    ans = ans - Comb(i + c2 + 1, c2);
    ans = (ans + N) % N;
  }
  for (ll i = 0; i <= r2; i++) {
    ans = ans - Comb(i + c1, c1 - 1);
    ans = (ans + N) % N;
  }
  for (ll i = 0; i < r1; i++) {
    ans = ans + Comb(i + c1, c1 - 1);
    ans = ans % N;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;

#define SIZE 2000010
ll kaijo[SIZE];
void init() {
  kaijo[0] = 1;
  for (ll i = 1; i < SIZE; i++)
    kaijo[i] = (kaijo[i - 1] * i) % N;
}

ll inv(ll x) {
  ll res = 1;
  ll k = N - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % N;
    y = (y % N * y % N) % N;
    k /= 2;
  }
  return res;
}

ll Comb(ll n, ll k) {
  if (n < 0 || k < 0 || (n - k) < 0)
    return 0;
  ll b = kaijo[n];
  ll c = kaijo[n - k];
  ll d = kaijo[k];
  ll cd = (c * d) % N;
  return ((b % N) * (inv(cd)) % N) % N;
}

int main(void) {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  init();
  ll ans = 0;
  for (ll i = 0; i <= r2; i++) {
    ans = ans + Comb(i + c2 + 1, c2);
    ans = ans % N;
  }
  for (ll i = 0; i < r1; i++) {
    ans = ans - Comb(i + c2 + 1, c2);
    ans = (ans + N) % N;
  }
  for (ll i = 0; i <= r2; i++) {
    ans = ans - Comb(i + c1, c1 - 1);
    ans = (ans + N) % N;
  }
  for (ll i = 0; i < r1; i++) {
    ans = ans + Comb(i + c1, c1 - 1);
    ans = ans % N;
  }
  cout << ans << endl;
  return 0;
}
Problem p02782 (C++)

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

const int MAX = 1000001;
const int MOD = 1000000007;

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

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  rep(i, MAX) {
    if (i == 0 || i == 1)
      continue;
    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 r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  ll res = 0;
  rep(i, r2 - r1 + 1) {
    res += COM(r1 + c2 + 1 + i, r1 + 1 + i);
    res -= COM(r1 + c1 + i, r1 + 1 + i);
    if (res < 0)
      res += MOD;
    res %= MOD;
  }
  cout << res << endl;
}

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

const int MAX = 2020202;
const int MOD = 1000000007;

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

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  rep(i, MAX) {
    if (i == 0 || i == 1)
      continue;
    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 r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  COMinit();
  ll res = 0;
  rep(i, r2 - r1 + 1) {
    res += COM(r1 + c2 + 1 + i, r1 + 1 + i);
    res -= COM(r1 + c1 + i, r1 + 1 + i);
    if (res < 0)
      res += MOD;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02782 (C++)

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

#define ll long long
#define ld long double
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define int long long

using namespace std;
const int INF = 2e18;
const int MOD = 1e9 + 7;
const int MB = 20;
const int maxN = 1e6 + 1;

int fact[maxN], rev[maxN];

int mod_pow(int a, int b) {
  if (!b)
    return 1;
  if (b % 2)
    return a * mod_pow(a, b - 1) % MOD;
  int q = mod_pow(a, b / 2);
  return q * q % MOD;
}

int C(int n, int k) { return (fact[n] * rev[k] % MOD) * rev[n - k] % MOD; }

int f(int r, int c) {
  int ans = 0;
  for (int i = r + 1; i > 1; i--) {
    ans = (ans + C(i + c, c)) % MOD;
  }
  return ans;
}

void solve() {
  fact[0] = 1;
  for (int i = 1; i < maxN; i++)
    fact[i] = fact[i - 1] * i % MOD;
  for (int i = 0; i < maxN; i++)
    rev[i] = mod_pow(fact[i], MOD - 2);
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  cout << (f(r2, c2) - f(r2, c1 - 1) - f(r1 - 1, c2) + f(r1 - 1, c1 - 1) +
           5 * MOD) %
              MOD;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout.precision(12);
  solve();
}

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

#define ll long long
#define ld long double
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define int long long

using namespace std;
const int INF = 2e18;
const int MOD = 1e9 + 7;
const int MB = 20;
const int maxN = 4e6 + 1;

int fact[maxN], rev[maxN];

int mod_pow(int a, int b) {
  if (!b)
    return 1;
  if (b % 2)
    return a * mod_pow(a, b - 1) % MOD;
  int q = mod_pow(a, b / 2);
  return q * q % MOD;
}

int C(int n, int k) { return (fact[n] * rev[k] % MOD) * rev[n - k] % MOD; }

int f(int r, int c) {
  int ans = 0;
  for (int i = r + 1; i > 1; i--) {
    ans = (ans + C(i + c, c)) % MOD;
  }
  return ans;
}

void solve() {
  fact[0] = 1;
  for (int i = 1; i < maxN; i++)
    fact[i] = fact[i - 1] * i % MOD;
  for (int i = 0; i < maxN; i++)
    rev[i] = mod_pow(fact[i], MOD - 2);
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  cout << (f(r2, c2) - f(r2, c1 - 1) - f(r1 - 1, c2) + f(r1 - 1, c1 - 1) +
           5 * MOD) %
              MOD;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout.precision(12);
  solve();
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

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() {
  // 前処理
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long ans = 0;
  ans += COM(r1 + c1, r1);
  ans %= MOD;
  for (int i = r2 + c2; i > r1 + c2; --i) {
    ans += COM(i, c2) % MOD;
    ans %= MOD;
  }
  for (int i = r2 + c2; i > c1 + r2; --i) {
    ans += COM(i, r2) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

const int MAX = 2000005;
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() {
  // 前処理
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long ans = 0;
  ans += COM(r1 + c1, r1);
  ans %= MOD;
  for (int i = r2 + c2; i > r1 + c2; --i) {
    ans += COM(i, c2) % MOD;
    ans %= MOD;
  }
  for (int i = r2 + c2; i > c1 + r2; --i) {
    ans += COM(i, r2) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02782 (C++)

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

using namespace std;

const int mod = 1e9 + 7;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mul(int a, int b) { return int(1LL * a * b % mod); }
int fexp(int b, int e = mod - 2) {
  int ans = 1;
  while (e) {
    if (e & 1)
      ans = mul(ans, b);
    b = mul(b, b);
    e /= 2;
  }
  return ans;
}

const int N = 1000006;

int fat[N], ifat[N];
int C(int n, int k) {
  if (n < k)
    return 0;
  return mul(mul(fat[n], ifat[k]), ifat[n - k]);
}

int solve(int n, int m) {
  if (n < 0 or m < 0)
    return 0;
  int ans = 0;
  for (int j = 0; j <= m; j++) {
    ans = add(ans, C(j + n + 1, j + 1));
  }
  return ans;
}

int main() {
  fat[0] = ifat[0] = 1;
  for (int i = 1; i < N; i++) {
    fat[i] = mul(i, fat[i - 1]);
    ifat[i] = fexp(fat[i]);
  }
  int r1, c1, r2, c2;
  scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
  int ans = 0;
  ans = add(ans, solve(r2, c2));
  ans = add(ans, mod - solve(r1 - 1, c2));
  ans = add(ans, mod - solve(r2, c1 - 1));
  ans = add(ans, solve(r1 - 1, c1 - 1));
  printf("%d\n", ans);
}


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

using namespace std;

const int mod = 1e9 + 7;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mul(int a, int b) { return int(1LL * a * b % mod); }
int fexp(int b, int e = mod - 2) {
  int ans = 1;
  while (e) {
    if (e & 1)
      ans = mul(ans, b);
    b = mul(b, b);
    e /= 2;
  }
  return ans;
}

const int N = 2000006;

int fat[N], ifat[N];
int C(int n, int k) {
  if (n < k)
    return 0;
  return mul(mul(fat[n], ifat[k]), ifat[n - k]);
}

int solve(int n, int m) {
  if (n < 0 or m < 0)
    return 0;
  int ans = 0;
  for (int j = 0; j <= m; j++) {
    ans = add(ans, C(j + n + 1, j + 1));
  }
  return ans;
}

int main() {
  fat[0] = ifat[0] = 1;
  for (int i = 1; i < N; i++) {
    fat[i] = mul(i, fat[i - 1]);
    ifat[i] = fexp(fat[i]);
  }
  int r1, c1, r2, c2;
  scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
  int ans = 0;
  ans = add(ans, solve(r2, c2));
  ans = add(ans, mod - solve(r1 - 1, c2));
  ans = add(ans, mod - solve(r2, c1 - 1));
  ans = add(ans, solve(r1 - 1, c1 - 1));
  printf("%d\n", ans);
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (ll i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
ll ans = 0;
const ll MAX = 1200000;
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 n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void Main() {
  COMinit();
  ll r1;
  cin >> r1;
  r1--;
  ll c1;
  cin >> c1;
  c1--;
  ll r2;
  cin >> r2;
  ll c2;
  cin >> c2;
  ans += COM(r2 + c2 + 2, r2 + 1);
  ans %= MOD;
  ans -= COM(r1 + c2 + 2, r1 + 1);
  ans = (ans + MOD) % MOD;
  ans += COM(r1 + c1 + 2, r1 + 1);
  ans %= MOD;
  ans -= COM(r2 + c1 + 2, r2 + 1);
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
}
int main() {
  Main();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (ll i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
ll ans = 0;
const ll MAX = 2400000;
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 n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void Main() {
  COMinit();
  ll r1;
  cin >> r1;
  r1--;
  ll c1;
  cin >> c1;
  c1--;
  ll r2;
  cin >> r2;
  ll c2;
  cin >> c2;
  ans += COM(r2 + c2 + 2, r2 + 1);
  ans %= MOD;
  ans -= COM(r1 + c2 + 2, r1 + 1);
  ans = (ans + MOD) % MOD;
  ans += COM(r1 + c1 + 2, r1 + 1);
  ans %= MOD;
  ans -= COM(r2 + c1 + 2, r2 + 1);
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
}
int main() {
  Main();
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 500010;

ll fac[N];

ll power(ll x, ll n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return x;
  }
  if (n % 2 == 0) {
    return power((x * x) % M, n / 2);
  }
  if (n % 2 == 1) {
    return (power((x * x) % M, n / 2) * x) % M;
  }
}

ll ncr(ll n, ll r) {
  ll num = fac[n];
  ll denum = (fac[r] * fac[n - r]) % M;
  ll ans = (num * power(denum, M - 2)) % M;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  fac[0] = 1;
  for (ll i = 1; i < N; ++i) {
    fac[i] = (i * fac[i - 1]) % M;
  }

  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ++r1;
  ++c1;
  ++r2;
  ++c2;
  ll ans = 0;
  for (ll i = r1; i <= r2; ++i) {
    ll ch = (ncr(i + c2 - 1, i) - ncr(i + c1 - 2, i) + M) % M;
    ans = (ans + ch) % M;
  }
  cout << ans << endl;

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
using namespace std;
const ll N = 5000010;

ll fac[N];

ll power(ll x, ll n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return x;
  }
  if (n % 2 == 0) {
    return power((x * x) % M, n / 2);
  }
  if (n % 2 == 1) {
    return (power((x * x) % M, n / 2) * x) % M;
  }
}

ll ncr(ll n, ll r) {
  ll num = fac[n];
  ll denum = (fac[r] * fac[n - r]) % M;
  ll ans = (num * power(denum, M - 2)) % M;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  fac[0] = 1;
  for (ll i = 1; i < N; ++i) {
    fac[i] = (i * fac[i - 1]) % M;
  }

  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ++r1;
  ++c1;
  ++r2;
  ++c2;
  ll ans = 0;
  for (ll i = r1; i <= r2; ++i) {
    ll ch = (ncr(i + c2 - 1, i) - ncr(i + c1 - 2, i) + M) % M;
    ans = (ans + ch) % M;
  }
  cout << ans << endl;

  return (0);
}
Problem p02782 (C++)

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

const ll MAX = 1e6 + 5;
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 (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(ll n, ll 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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  COMinit();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll a, b, c, d;
  a = COM(r2 + 1 + c2 + 1, c2 + 1);
  b = COM(r1 + c2 + 1, c2 + 1);
  c = COM(r2 + 1 + c1, c1);
  d = COM(r1 + c1, c1);
  ll ans = (a - b - c + d) % MOD;
  if (ans < 0)
    ans += MOD;
  // cout<<a<<b<<c<<d<<"\n";

  cout << ans << "\n";

  return 0;
}


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

const ll MAX = 1e6 * 2 + 5;
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 (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(ll n, ll 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() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  COMinit();
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll a, b, c, d;
  a = COM(r2 + 1 + c2 + 1, c2 + 1);
  b = COM(r1 + c2 + 1, c2 + 1);
  c = COM(r2 + 1 + c1, c1);
  d = COM(r1 + c1, c1);
  ll ans = (a - b - c + d) % MOD;
  if (ans < 0)
    ans += MOD;
  // cout<<a<<b<<c<<d<<"\n";

  cout << ans << "\n";

  return 0;
}
Problem p02782 (C++)

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

// mod逆元を使ったnCkの計算(速)
const int64_t MAX = 510000;
const int64_t MOD = 1000000007;

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

// 二項係数計算
int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t calc(int64_t x, int64_t y1, int64_t y2) {
  int64_t A = COM(x + y2 + 1, y2); // 0からc2まで
  int64_t B = COM(x + y1, y1 - 1); // 0からc1-1まで
  A -= B;
  if (A < 0) {
    A += MOD;
  }
  return A % MOD;
}

int main() {
  int64_t r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int64_t sum = 0;
  COMinit();
  for (int i = r1; i <= r2; i++) {
    sum += calc(i, c1, c2);
    sum %= MOD;
  }
  cout << sum << endl;
}

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

// mod逆元を使ったnCkの計算(速)
const int64_t MAX = 3000000;
const int64_t MOD = 1000000007;

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

// 二項係数計算
int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int64_t calc(int64_t x, int64_t y1, int64_t y2) {
  int64_t A = COM(x + y2 + 1, y2); // 0からc2まで
  int64_t B = COM(x + y1, y1 - 1); // 0からc1-1まで
  A -= B;
  if (A < 0) {
    A += MOD;
  }
  return A % MOD;
}

int main() {
  int64_t r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int64_t sum = 0;
  COMinit();
  for (int i = r1; i <= r2; i++) {
    sum += calc(i, c1, c2);
    sum %= MOD;
  }
  cout << sum << endl;
}
Problem p02782 (C++)

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

#define mod 1000000007
#define MAX_N 200010
long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void GetInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void GetFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long combination(int n, int r) {
  long long ret = factorial[n] * inv_factorial[r];
  ret %= mod;
  ret *= inv_factorial[n - r];
  return ret % mod;
}

long long rect(int r, int c) { return combination(c + r + 2, r + 1) - 1; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  GetInv();
  GetFactorial();
  long long r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long ans =
      rect(r1 - 1, c1 - 1) + rect(r2, c2) - rect(r1 - 1, c2) - rect(r2, c1 - 1);
  cout << (ans % mod + mod) % mod << "\n";
  return 0;
}

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

#define mod 1000000007
#define MAX_N 2000100
long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void GetInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void GetFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long combination(int n, int r) {
  long long ret = factorial[n] * inv_factorial[r];
  ret %= mod;
  ret *= inv_factorial[n - r];
  return ret % mod;
}

long long rect(int r, int c) { return combination(c + r + 2, r + 1) - 1; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  GetInv();
  GetFactorial();
  long long r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long ans =
      rect(r1 - 1, c1 - 1) + rect(r2, c2) - rect(r1 - 1, c2) - rect(r2, c1 - 1);
  cout << (ans % mod + mod) % mod << "\n";
  return 0;
}