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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define inf 1e18

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //	ifstream cin("input.txt");
  //	ofstream cout("output.txt");
  ll k;
  string s;
  cin >> s >> k;
  int n = s.size();
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(n + 1, vector<ll>(2)));
  dp[0][0][1] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      for (int z = 0; z < 2; ++z) {
        for (int d = 0; d <= 9; ++d) {
          if (j + (d != 0) > k) {
            continue;
          }
          if (j > i || (d > s[i] - '0' && z == 1)) {
            continue;
          }
          dp[i + 1][j + (d != 0)][z & (d == s[i] - '0')] += dp[i][j][z];
        }
      }
    }
  }
  cout << dp[n][k][1] + dp[n][k][0];
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define inf 1e18

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //	ifstream cin("input.txt");
  //	ofstream cout("output.txt");
  ll k;
  string s;
  cin >> s >> k;
  int n = s.size();
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(k + 100, vector<ll>(2)));
  dp[0][0][1] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      for (int z = 0; z < 2; ++z) {
        for (int d = 0; d <= 9; ++d) {
          if (j + (d != 0) > k) {
            continue;
          }
          if (j > i || (d > s[i] - '0' && z == 1)) {
            continue;
          }
          dp[i + 1][j + (d != 0)][z & (d == s[i] - '0')] += dp[i][j][z];
        }
      }
    }
  }
  cout << dp[n][k][1] + dp[n][k][0];
}
Problem p02781 (C++)

Buggy submission (status: Memory Limit Exceeded):
//----------BHAVIK DIWANI(PICT_COMP)---------------
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define MAX 1000005
#define pb push_back
#define vi vector<int>
#define endl '\n'
using namespace std;
// cnt total no. of elements between range from a to b which contains digit d
// exactly k times
string b;
int k;
vi num;
int dp[10000000][12]
      [2]; // dp[pos][cnt][f]	f=if number has become less or not
int call(int pos, int cnt, int f) {
  if (cnt > k)
    return 0;
  if (num.size() == pos) {
    if (cnt == k)
      return 1;
    return 0;
  }
  int limit, res = 0;

  if (dp[pos][cnt][f] != -1)
    return dp[pos][cnt][f];
  if (f == 0)
    limit = num[pos];
  else
    limit = 9;

  for (int i = 0; i <= limit; i++) {
    int nf = f;
    int ncnt = cnt;
    if (i < limit)
      nf = 1;
    if (i != 0)
      ncnt++;
    if (cnt <= k)
      res += call(pos + 1, ncnt, nf);
  }
  return dp[pos][cnt][f] = res;
}

int solve(string b) {
  num.clear();
  for (int i = 0; i < b.length(); i++) {
    num.pb(b[i] - '0');
  }
  memset(dp, -1, sizeof(dp));
  int res = call(0, 0, 0);
  return res;
}
signed main() {
  fastio;
  cin >> b >> k;
  int res = solve(b);
  cout << res << endl;
  return 0;
}


Fixed submission:
//----------BHAVIK DIWANI(PICT_COMP)---------------
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define MAX 1000005
#define pb push_back
#define vi vector<int>
#define endl '\n'
using namespace std;
// cnt total no. of elements between range from a to b which contains digit d
// exactly k times
string b;
int k;
vi num;
int dp[128][4][2]; // dp[pos][cnt][f]	f=if number has become less or not
int call(int pos, int cnt, int f) {
  if (cnt > k)
    return 0;
  if (num.size() == pos) {
    if (cnt == k)
      return 1;
    return 0;
  }
  int limit, res = 0;

  if (dp[pos][cnt][f] != -1)
    return dp[pos][cnt][f];
  if (f == 0)
    limit = num[pos];
  else
    limit = 9;

  for (int i = 0; i <= limit; i++) {
    int nf = f;
    int ncnt = cnt;
    if (i < limit)
      nf = 1;
    if (i != 0)
      ncnt++;
    if (cnt <= k)
      res += call(pos + 1, ncnt, nf);
  }
  return dp[pos][cnt][f] = res;
}

int solve(string b) {
  num.clear();
  for (int i = 0; i < b.length(); i++) {
    num.pb(b[i] - '0');
  }
  memset(dp, -1, sizeof(dp));
  int res = call(0, 0, 0);
  return res;
}
signed main() {
  fastio;
  cin >> b >> k;
  int res = solve(b);
  cout << res << endl;
  return 0;
}
Problem p02781 (C++)

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

long long comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  long long ans = 1;
  rep(i, k) {
    ans *= n;
    n--;
  }
  rep(i, k) { ans /= i + 1; }
  return ans;
}

long long calc(string S, int N, int K) {
  if (K > N)
    return 0;
  if (K == 0)
    return 1;
  if (S[0] - '0' == 0)
    return calc(S.substr(1), N, K);
  long long ans = 0;
  ans += comb(N - 1, K) * pow(9, K);

  if (S[0] - '0' > 1) {
    ans += (S[0] - '0' - 1) * comb(N - 1, K - 1) * pow(9, K - 1);
  }
  ans += calc(S.substr(1), N - 1, K - 1);

  return ans;
}

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  int N = S.size();

  cout << calc(S, N, K) << endl;
}

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

long long comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  long long ans = 1;
  rep(i, k) {
    ans *= n;
    n--;
  }
  rep(i, k) { ans /= i + 1; }
  return ans;
}

long long calc(string S, int N, int K) {
  if (K > N)
    return 0;
  if (K == 0)
    return 1;
  if (S[0] - '0' == 0)
    return calc(S.substr(1), N - 1, K);
  long long ans = 0;
  ans += comb(N - 1, K) * pow(9, K);

  if (S[0] - '0' > 1) {
    ans += (S[0] - '0' - 1) * comb(N - 1, K - 1) * pow(9, K - 1);
  }
  ans += calc(S.substr(1), N - 1, K - 1);

  return ans;
}

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  int N = S.size();

  cout << calc(S, N, K) << endl;
}
Problem p02781 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(100)

n = input()
k = int(input())
m = {}


def doit(n, k):
    if len(n) == 0:
        return k == 0
    d = int(n[0])
    if (n, k) not in m:
        ret = 0
        for i in range(d + 1):
            if i == d:
                ret += doit(n[1:], k - 1 if i > 0 else k)
            else:
                ret += doit("9" * (len(n) - 1), k - 1 if i > 0 else k)
        m[(n, k)] = ret
    return m[(n, k)]


print(doit(n, k))


Fixed submission:
import sys

sys.setrecursionlimit(10000)

n = input()
k = int(input())
m = {}


def doit(n, k):
    if len(n) == 0:
        return k == 0
    d = int(n[0])
    if (n, k) not in m:
        ret = 0
        for i in range(d + 1):
            if i == d:
                ret += doit(n[1:], k - 1 if i > 0 else k)
            else:
                ret += doit("9" * (len(n) - 1), k - 1 if i > 0 else k)
        m[(n, k)] = ret
    return m[(n, k)]


print(doit(n, k))
Problem p02781 (C++)

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

using namespace std;

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;

  long dp[S.length()][5][2];
  for (int i = 0; i < S.length(); i++) {
    for (int j = 0; j < S.length() + 1; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[0][1][0] = 1;
  dp[0][1][1] = S[0] - '0' - 1;
  dp[0][0][1] = 1;
  // 0: t
  // 1: >t
  for (int i = 1; i < S.length(); i++) {
    for (int j = 0; j < 4; j++) {
      if (S[i] == '0') {
        dp[i][j][0] = dp[i - 1][j][0];
      } else {
        dp[i][j + 1][0] = dp[i - 1][j][0];
        dp[i][j + 1][1] += dp[i - 1][j][0] * (S[i] - '0' - 1);
        dp[i][j][1] += dp[i - 1][j][0];
      }

      dp[i][j + 1][1] += dp[i - 1][j][1] * 9;
      dp[i][j][1] += dp[i - 1][j][1];
    }
  }

  cout << dp[S.length() - 1][K][0] + dp[S.length() - 1][K][1] << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;

  long dp[S.length()][5][2];
  for (int i = 0; i < S.length(); i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[0][1][0] = 1;
  dp[0][1][1] = S[0] - '0' - 1;
  dp[0][0][1] = 1;
  // 0: t
  // 1: >t
  for (int i = 1; i < S.length(); i++) {
    for (int j = 0; j < 4; j++) {
      if (S[i] == '0') {
        dp[i][j][0] = dp[i - 1][j][0];
      } else {
        dp[i][j + 1][0] = dp[i - 1][j][0];
        dp[i][j + 1][1] += dp[i - 1][j][0] * (S[i] - '0' - 1);
        dp[i][j][1] += dp[i - 1][j][0];
      }

      dp[i][j + 1][1] += dp[i - 1][j][1] * 9;
      dp[i][j][1] += dp[i - 1][j][1];
    }
  }

  cout << dp[S.length() - 1][K][0] + dp[S.length() - 1][K][1] << endl;

  return 0;
}
Problem p02781 (C++)

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

#define LL long int

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  int sz = s.size();

  vector<int> num(sz);

  for (int i = 0; i < sz; ++i)
    num[i] = s[i] - '0';

  vector<vector<vector<LL>>> dp(sz + 1,
                                vector<vector<LL>>(2, vector<LL>(k + 1, 0)));

  dp[0][0][0] = 1;

  for (int i = 0; i < sz; ++i) {
    for (int smaller = 0; smaller < 2; ++smaller) {
      for (int j = 0; j <= k; ++j) {
        for (int x = 0; x <= (smaller ? 9 : num[i]); ++x) {
          dp[i + 1][smaller || x < num[i]][j + (x != 0)] += dp[i][smaller][j];
        }
      }
    }
  }

  cout << dp[sz][0][k] + dp[sz][1][k] << endl;

  return 0;
}

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

#define LL long int

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  int sz = s.size();

  vector<int> num(sz);

  for (int i = 0; i < sz; ++i)
    num[i] = s[i] - '0';

  vector<vector<vector<LL>>> dp(sz + 1,
                                vector<vector<LL>>(2, vector<LL>(k + 2, 0)));

  dp[0][0][0] = 1;

  for (int i = 0; i < sz; ++i) {
    for (int smaller = 0; smaller < 2; ++smaller) {
      for (int j = 0; j <= k; ++j) {
        for (int x = 0; x <= (smaller ? 9 : num[i]); ++x) {
          dp[i + 1][smaller || x < num[i]][j + (x != 0)] += dp[i][smaller][j];
        }
      }
    }
  }

  cout << dp[sz][0][k] + dp[sz][1][k] << endl;

  return 0;
}
Problem p02781 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll ans = 0;

int main() {
  string S;
  cin >> S;
  int N = sz(S);
  int K;
  cin >> K;
  /*
  dp[i][j][k]
  i: 頭からi桁 j: 未満フラグ k: 0以外の個数
  dp[i][0][0]:頭からi桁 n未満確定 全部0
  dp[i][0][1]:頭からi桁 n未満確定 0以外が1個
  dp[i][1][0]:頭からi桁 nは一致 全部0
  dp[i][1][1]:頭からi桁 nは一致 0以外が1個
  */
  vector<vector<vector<ll>>> dp(N + 1,
                                vector<vector<ll>>(2, vector<ll>(K + 1, 0)));
  dp[0][0][0] = 1;
  rep(i, N) {
    rep(smaller, 2) {
      rep(k, K + 1) {
        for (int x = 0; x <= (smaller ? 9 : (int)(S[i] - '0')); x++) {
          dp[i + 1][smaller || x < (int)(S[i] - '0')][k + (x != 0)] +=
              dp[i][smaller][k];
        }
      }
    }
  }
  cout << dp[N][0][K] + dp[N][1][K] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll ans = 0;

int main() {
  string S;
  cin >> S;
  int N = sz(S);
  int K;
  cin >> K;
  /*
  dp[i][j][k]
  i: 頭からi桁 j: 未満フラグ k: 0以外の個数
  dp[i][0][0]:頭からi桁 n未満確定 全部0
  dp[i][0][1]:頭からi桁 n未満確定 0以外が1個
  dp[i][1][0]:頭からi桁 nは一致 全部0
  dp[i][1][1]:頭からi桁 nは一致 0以外が1個
  */
  vector<vector<vector<ll>>> dp(N + 1,
                                vector<vector<ll>>(2, vector<ll>(K + 2, 0)));
  dp[0][0][0] = 1;
  rep(i, N) {
    rep(smaller, 2) {
      rep(k, K + 1) {
        for (int x = 0; x <= (smaller ? 9 : (int)(S[i] - '0')); x++) {
          dp[i + 1][smaller || x < (int)(S[i] - '0')][k + (x != 0)] +=
              dp[i][smaller][k];
        }
      }
    }
  }
  cout << dp[N][0][K] + dp[N][1][K] << endl;
  return 0;
}
Problem p02781 (Python)

Buggy submission (status: Runtime Error):
N = input()
K = int(input())

a = 1
b = [0] * (K + 1)
b[0] = 1
b[1] = int(N[0]) - 1

for c in N[1:]:
    t = int(c)
    for i in range(K - 1, -1, -1):
        b[i + 1] += b[i] * 9
    if t != 0:
        b[a + 1] += t - 1
        b[a] += 1
        a += 1

if a == K:
    print(b[K] + 1)
else:
    print(b[K])


Fixed submission:
N = input()
K = int(input())

a = 1
b = [0] * (K + 1)
b[0] = 1
b[1] = int(N[0]) - 1

for c in N[1:]:
    t = int(c)
    for i in range(K - 1, -1, -1):
        b[i + 1] += b[i] * 9
    if t != 0:
        if a + 1 <= K:
            b[a + 1] += t - 1
        if a <= K:
            b[a] += 1
        a += 1

if a == K:
    print(b[K] + 1)
else:
    print(b[K])
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;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  c_fac();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ra = r2 - r1 + 1, ca = c2 - c1 + 1;
  ll result = 0;
  result += nck(r1 + c1, c1);
  repr(i, 1, ra) {
    result += nck(i + r1 + c1, c1) * nck(ra + ca - (i + 1), ca - 1);
    result %= MOD;
  }
  repr(i, 1, ca) {
    result += nck(i + r1 + c1, r1) * nck(ra + ca - (i + 1), ra - 1);
    result %= MOD;
  }
  cout << result << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) * 2 + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  c_fac();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  int ra = r2 - r1 + 1, ca = c2 - c1 + 1;
  ll result = 0;
  result += nck(r1 + c1, c1);
  repr(i, 1, ra) {
    result += nck(i + r1 + c1, c1) * nck(ra + ca - (i + 1), ca - 1);
    result %= MOD;
  }
  repr(i, 1, ca) {
    result += nck(i + r1 + c1, r1) * nck(ra + ca - (i + 1), ra - 1);
    result %= MOD;
  }
  cout << result << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 1000002;
const int MOD = 1000000007;

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

// 二項係数計算
int 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;
}
signed main() {
  cout << fixed << setprecision(10);
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r2++;
  c2++;
  cout << (COM(r2 + c2, r2) + COM(r1 + c1, r1) + (MOD - (COM(r1 + c2, r1))) +
           (MOD - (COM(r2 + c1, r2)))) %
              MOD;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 2000003;
const int MOD = 1000000007;

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

// 二項係数計算
int 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;
}
signed main() {
  cout << fixed << setprecision(10);
  COMinit();
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r2++;
  c2++;
  cout << (COM(r2 + c2, r2) + COM(r1 + c1, r1) + (MOD - (COM(r1 + c2, r1))) +
           (MOD - (COM(r2 + c1, r2)))) %
              MOD;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1100000;
const long long MOD = 1000000007;

typedef long long ll;

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

ll mod_pow(ll x, ll n) { // べき乗
  ll ans = 1;
  x %= MOD;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans % MOD;
}

void com_init() { // 初期化
  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;
    finv[i] = finv[i - 1] * mod_pow(i, MOD - 2) % MOD; // フェルマーの小定理より
  }
}

ll comb(ll n, ll k) { // nCk
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll sum_comb(ll r, ll c) {
  ll tmp = 0;
  for (int i = 1; i <= r + 1; i++) {
    tmp += comb(i + c, c);
    tmp %= MOD;
  }
  return tmp % MOD;
  ;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  com_init();
  ll ans = sum_comb(r2, c2) + sum_comb(r1 - 1, c1 - 1) + 2 * MOD -
           sum_comb(r1 - 1, c2) - sum_comb(r2, c1 - 1);
  cout << ans % MOD << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2100000;
const long long MOD = 1000000007;

typedef long long ll;

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

ll mod_pow(ll x, ll n) { // べき乗
  ll ans = 1;
  x %= MOD;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans % MOD;
}

void com_init() { // 初期化
  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;
    finv[i] = finv[i - 1] * mod_pow(i, MOD - 2) % MOD; // フェルマーの小定理より
  }
}

ll comb(ll n, ll k) { // nCk
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll sum_comb(ll r, ll c) {
  ll tmp = 0;
  for (int i = 1; i <= r + 1; i++) {
    tmp += comb(i + c, c);
    tmp %= MOD;
  }
  return tmp % MOD;
  ;
}

int main() {
  ll r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  com_init();
  ll ans = sum_comb(r2, c2) + sum_comb(r1 - 1, c1 - 1) + 2 * MOD -
           sum_comb(r1 - 1, c2) - sum_comb(r2, c1 - 1);
  cout << ans % MOD << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 6;
ll r1, c1, r2, c2;
ll fac[N], infac[N];
ll sq(ll a) { return (a * a) % mod; }
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % mod;
  if (b & 1)
    return (a * sq(pw(a, b >> 1))) % mod;
  return sq(pw(a, b >> 1)) % mod;
}
ll ncr(ll n, ll r) {
  if (n == r)
    return 1;
  if (n < r)
    return 0;
  ll res = fac[n];
  res *= infac[r];
  res %= mod;
  res *= infac[n - r];
  res %= mod;
  return res;
}
int main() {
  infac[1] = infac[0] = fac[0] = fac[1] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
    infac[i] = pw(fac[i], mod - 2);
  }
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = 0;
  for (ll i = c1; i <= c2; i++) {
    ll tmp = ncr(r2 + i + 1, i + 1) - ncr(r1 + i, i + 1);
    tmp += mod;
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e6 + 6;
ll r1, c1, r2, c2;
ll fac[N], infac[N];
ll sq(ll a) { return (a * a) % mod; }
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % mod;
  if (b & 1)
    return (a * sq(pw(a, b >> 1))) % mod;
  return sq(pw(a, b >> 1)) % mod;
}
ll ncr(ll n, ll r) {
  if (n == r)
    return 1;
  if (n < r)
    return 0;
  ll res = fac[n];
  res *= infac[r];
  res %= mod;
  res *= infac[n - r];
  res %= mod;
  return res;
}
int main() {
  infac[1] = infac[0] = fac[0] = fac[1] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
    infac[i] = pw(fac[i], mod - 2);
  }
  cin >> r1 >> c1 >> r2 >> c2;
  ll ans = 0;
  for (ll i = c1; i <= c2; i++) {
    ll tmp = ncr(r2 + i + 1, i + 1) - ncr(r1 + i, i + 1);
    tmp += mod;
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

#define MAX_N 200005

ll factorial[MAX_N];
void make_factorial_tableMOD() {
  factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i % MOD;
  }
}

ll bisection_powerMOD(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  ll t = bisection_powerMOD(x * x % MOD, y / 2);
  if (y % 2 == 1) {
    t = t * x % MOD;
  }
  return t;
}

ll nCrMOD(int n, int r) {
  if (n < r || n <= 0 || r < 0) {
    return 0;
  }
  ll a = factorial[n];
  ll b = a * bisection_powerMOD(factorial[r], (MOD - 2)) % MOD;
  ll c = b * bisection_powerMOD(factorial[n - r], MOD - 2) % MOD;
  return c % MOD;
}

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

  make_factorial_tableMOD();
  ll ru = 0;
  for (int i = 0; i <= r2; i++) {
    ru += nCrMOD(i + c2 + 1, i + 1);
    ru %= MOD;
  }
  // dbg(ru);
  ll lu = 0;
  for (int i = 0; i <= r2; i++) {
    lu += nCrMOD(i + c1, i + 1);
    lu %= MOD;
  }
  //    dbg(lu);
  ll rd = 0;
  for (int i = 0; i <= c2; i++) {
    rd += nCrMOD(i + r1, i + 1);
    rd %= MOD;
  }
  //    dbg(rd);
  ll ld = 0;
  for (int i = 0; i < c1; i++) {
    ld += nCrMOD(i + r1, i + 1);
    ld %= MOD;
  }
  // dbg(ld);

  ll ans = ru - lu - rd + ld + 4 * MOD;
  ans %= MOD;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

#define MAX_N 2000005

ll factorial[MAX_N];
void make_factorial_tableMOD() {
  factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i % MOD;
  }
}

ll bisection_powerMOD(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  ll t = bisection_powerMOD(x * x % MOD, y / 2);
  if (y % 2 == 1) {
    t = t * x % MOD;
  }
  return t;
}

ll nCrMOD(int n, int r) {
  if (n < r || n <= 0 || r < 0) {
    return 0;
  }
  ll a = factorial[n];
  ll b = a * bisection_powerMOD(factorial[r], (MOD - 2)) % MOD;
  ll c = b * bisection_powerMOD(factorial[n - r], MOD - 2) % MOD;
  return c % MOD;
}

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

  make_factorial_tableMOD();
  ll ru = 0;
  for (int i = 0; i <= r2; i++) {
    ru += nCrMOD(i + c2 + 1, i + 1);
    ru %= MOD;
  }
  // dbg(ru);
  ll lu = 0;
  for (int i = 0; i <= r2; i++) {
    lu += nCrMOD(i + c1, i + 1);
    lu %= MOD;
  }
  //    dbg(lu);
  ll rd = 0;
  for (int i = 0; i <= c2; i++) {
    rd += nCrMOD(i + r1, i + 1);
    rd %= MOD;
  }
  //    dbg(rd);
  ll ld = 0;
  for (int i = 0; i < c1; i++) {
    ld += nCrMOD(i + r1, i + 1);
    ld %= MOD;
  }
  // dbg(ld);

  ll ans = ru - lu - rd + ld + 4 * MOD;
  ans %= MOD;
  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 < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
const int MAX = 1000001;
const ll 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;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1; // a^n%mod
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  COMinit();
  cout << (COM(c + 1 + d + 1, c + 1) + COM(a + b, a) - COM(a + d + 1, a) -
           COM(c + b + 1, b) + MOD * 2) %
              MOD
       << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
const int MAX = 2000005;
const ll 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;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1; // a^n%mod
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  COMinit();
  cout << (COM(c + 1 + d + 1, c + 1) + COM(a + b, a) - COM(a + d + 1, a) -
           COM(c + b + 1, b) + MOD * 2) %
              MOD
       << endl;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1000005;
const int mod = 1000000007;

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

ll inv(ll a, ll n) {
  ll d, x, y;
  gcd(a, n, d, x, y);
  return d == 1 ? (x % n + n) % (n / d) : -1;
}

ll p[maxn];

ll solve(int r, int c) {
  ll ret = 0;
  for (int i = 0; i <= c; i++) {
    ret += p[r + i + 1] * inv(p[i + 1] * p[r] % mod, mod) % mod;
    ret %= mod;
  }
  return ret;
}

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  p[0] = 1;
  for (int i = 1; i < maxn; i++)
    p[i] = p[i - 1] * i % mod;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ret = (solve(r2, c2) + solve(r1 - 1, c1 - 1) - solve(r1 - 1, c2) -
            solve(r2, c1 - 1));
  ret = (ret % mod + mod) % mod;
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 2000005;
const int mod = 1000000007;

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

ll inv(ll a, ll n) {
  ll d, x, y;
  gcd(a, n, d, x, y);
  return d == 1 ? (x % n + n) % (n / d) : -1;
}

ll p[maxn];

ll solve(int r, int c) {
  ll ret = 0;
  for (int i = 0; i <= c; i++) {
    ret += p[r + i + 1] * inv(p[i + 1] * p[r] % mod, mod) % mod;
    ret %= mod;
  }
  return ret;
}

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  p[0] = 1;
  for (int i = 1; i < maxn; i++)
    p[i] = p[i - 1] * i % mod;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll ret = (solve(r2, c2) + solve(r1 - 1, c1 - 1) - solve(r1 - 1, c2) -
            solve(r2, c1 - 1));
  ret = (ret % mod + mod) % mod;
  cout << ret << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> pii;
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const ll mod = 1000000007;
ll fact[maxn], inv[maxn];
ll power(ll a, int x) {
  ll ans = 1;
  while (x) {
    if (x & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    x >>= 1;
  }
  return ans;
}
void init() {
  fact[0] = 1ll;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }
  inv[maxn - 1] = power(fact[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = inv[i + 1] * (i + 1) % mod;
  }
}
ll f(int i, int j) { return fact[i + j] * inv[i] % mod * inv[j] % mod; }
ll cal(int i, int j) {
  ll res = 0;
  for (int k = 1; k <= i + 1; k++)
    res = (res + f(k, j)) % mod;
  return res;
}
int r1, c1, r2, c2;
int main() {
  cin >> r1 >> c1 >> r2 >> c2;
  init();
  ll ans = (cal(r2, c2) + cal(r1 - 1, c1 - 1)) % mod;
  ans = (ans - cal(r1 - 1, c2) + mod - cal(r2, c1 - 1) + mod) % mod;
  cout << ans << endl;
  return 0;
};

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> pii;
const int maxn = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const ll mod = 1000000007;
ll fact[maxn], inv[maxn];
ll power(ll a, int x) {
  ll ans = 1;
  while (x) {
    if (x & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    x >>= 1;
  }
  return ans;
}
void init() {
  fact[0] = 1ll;
  for (int i = 1; i < maxn; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }
  inv[maxn - 1] = power(fact[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; i--) {
    inv[i] = inv[i + 1] * (i + 1) % mod;
  }
}
ll f(int i, int j) { return fact[i + j] * inv[i] % mod * inv[j] % mod; }
ll cal(int i, int j) {
  ll res = 0;
  for (int k = 1; k <= i + 1; k++)
    res = (res + f(k, j)) % mod;
  return res;
}
int r1, c1, r2, c2;
int main() {
  cin >> r1 >> c1 >> r2 >> c2;
  init();
  ll ans = (cal(r2, c2) + cal(r1 - 1, c1 - 1)) % mod;
  ans = (ans - cal(r1 - 1, c2) + mod - cal(r2, c1 - 1) + mod) % mod;
  cout << ans << endl;
  return 0;
};
Problem p02782 (C++)

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9 + 7;

// mod付きコンビネーション
const int MAX = 510000;
const int MOD = 1000000007;

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

// 二項係数計算
Int 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;
  COMinit();
  Int res = 0;
  for (Int i = a + 1; i <= c + 1; i++) {
    Int add = fac[i + d] * finv[i] % mod * finv[d] % mod;
    Int sub = fac[i + b - 1] * finv[i] % mod * finv[b - 1] % mod;
    res = (res + add - sub + mod) % mod;
  }
  dump(res);
  return 0;
}

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9 + 7;

// mod付きコンビネーション
const int MAX = 5100000;
const int MOD = 1000000007;

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

// 二項係数計算
Int 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;
  COMinit();
  Int res = 0;
  for (Int i = a + 1; i <= c + 1; i++) {
    Int add = fac[i + d] * finv[i] % mod * finv[d] % mod;
    Int sub = fac[i + b - 1] * finv[i] % mod * finv[b - 1] % mod;
    res = (res + add - sub + mod) % mod;
  }
  dump(res);
  return 0;
}
Problem p02782 (C++)

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

const long long MOD = 1000000007;

const int MAX = 510000;
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;
  }
}

ll f(int i, int j) {
  return (fac[i + j] * finv[i] % MOD * finv[j] % MOD) % MOD;
}

void solve(std::vector<long long> r, std::vector<long long> c) {
  COMinit();
  ll ans = 0;
  for (int i = r[0]; i <= r[1]; i++) {
    ans += f(i + 1, c[1]) + MOD - f(i + 1, c[0] - 1);
    // if(i <= 20)cout<<f(i+1, c[1])<<" "<< f(i+1, c[1])<<endl;
    ans %= MOD;
  }
  cout << ans % MOD << endl;
}

int main() {
  std::vector<long long> r(2);
  std::vector<long long> c(2);
  for (int i = 0; i < 2; i++) {
    scanf("%lld", &r[i]);
    scanf("%lld", &c[i]);
  }
  solve(std::move(r), std::move(c));
  return 0;
}


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

const long long MOD = 1000000007;

const int MAX = 5100000;
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;
  }
}

ll f(int i, int j) {
  return (fac[i + j] * finv[i] % MOD * finv[j] % MOD) % MOD;
}

void solve(std::vector<long long> r, std::vector<long long> c) {
  COMinit();
  ll ans = 0;
  for (int i = r[0]; i <= r[1]; i++) {
    ans += f(i + 1, c[1]) + MOD - f(i + 1, c[0] - 1);
    // if(i <= 20)cout<<f(i+1, c[1])<<" "<< f(i+1, c[1])<<endl;
    ans %= MOD;
  }
  cout << ans % MOD << endl;
}

int main() {
  std::vector<long long> r(2);
  std::vector<long long> c(2);
  for (int i = 0; i < 2; i++) {
    scanf("%lld", &r[i]);
    scanf("%lld", &c[i]);
  }
  solve(std::move(r), std::move(c));
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

vector<ll> inv, fact, invfact;
void Mod_build(int n = 101010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  for (ll i = 0; i < n; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

ll cal(ll s, ll t) {
  return comb(s + 1 + t + 1, s + 1) - 1;
  ;
}

int main() {
  int i, j;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  Mod_build();
  ll ans = mod + mod + cal(r2, c2) + cal(r1 - 1, c1 - 1) - cal(r1 - 1, c2) -
           cal(r2, c1 - 1);
  cout << ans % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

vector<ll> inv, fact, invfact;
void Mod_build(int n = 3010101) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  for (ll i = 0; i < n; i++) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

ll cal(ll s, ll t) {
  return comb(s + 1 + t + 1, s + 1) - 1;
  ;
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
template <class Read> void in(Read &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
#define int long long
const int M = 1e9 + 7;
int r1, r2, c1, c2, n, f[1000005], ans, sum;
int ksm(int x, int y) {
  int s = 1;
  while (y) {
    if (y & 1)
      s = (s * x) % M;
    x = (x * x) % M;
    y >>= 1;
  }
  return s;
}
int work(int x, int y) {
  return f[x] * ksm(f[y], M - 2) % M * ksm(f[x - y], M - 2) % M;
}
signed main() {
  in(r1);
  in(c1);
  in(r2);
  in(c2);
  n = max(c1, c2) + r2 + 2;
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = (f[i - 1] * i) % M;
  for (int i = r1; i <= r2; i++)
    ans = (ans + work(i + c2 + 1, i + 1)) % M;
  for (int i = r1; i <= r2; i++)
    sum = (sum + work(i + c1, i + 1)) % M;
  cout << (ans - sum + M) % M << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
template <class Read> void in(Read &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
#define int long long
const int M = 1e9 + 7;
int r1, r2, c1, c2, n, f[5000005], ans, sum;
int ksm(int x, int y) {
  int s = 1;
  while (y) {
    if (y & 1)
      s = (s * x) % M;
    x = (x * x) % M;
    y >>= 1;
  }
  return s;
}
int work(int x, int y) {
  return f[x] * ksm(f[y], M - 2) % M * ksm(f[x - y], M - 2) % M;
}
signed main() {
  in(r1);
  in(c1);
  in(r2);
  in(c2);
  n = max(c1, c2) + r2 + 2;
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = (f[i - 1] * i) % M;
  for (int i = r1; i <= r2; i++)
    ans = (ans + work(i + c2 + 1, i + 1)) % M;
  for (int i = r1; i <= r2; i++)
    sum = (sum + work(i + c1, i + 1)) % M;
  cout << (ans - sum + M) % M << endl;
  return 0;
}
Problem p02782 (C++)

Buggy submission (status: Runtime Error):
// 引用:http://drken1215.hatenablog.com/entry/2018/06/08/210000
#include <iostream>
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;
}

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

signed main() {
  // 前処理
  COMinit();

  // 計算例
  // cout << COM(100000, 50000) << endl;

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

  long long ans = 0;
  for (int c = c1 + 1; c <= c2 + 1; c++) {
    ans += f(r2, c);
    ans %= MOD;
  }

  for (int c = c1 + 1; c <= c2 + 1; c++) {
    ans += MOD - f(r1 - 1, c);
    ans %= MOD;
  }

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

Fixed submission:
// 引用:http://drken1215.hatenablog.com/entry/2018/06/08/210000
#include <iostream>
using namespace std;

const int MAX = 2510000;
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;
}

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

signed main() {
  // 前処理
  COMinit();

  // 計算例
  // cout << COM(100000, 50000) << endl;

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

  long long ans = 0;
  for (int c = c1 + 1; c <= c2 + 1; c++) {
    ans += f(r2, c);
    ans %= MOD;
  }

  for (int c = c1 + 1; c <= c2 + 1; c++) {
    ans += MOD - f(r1 - 1, c);
    ans %= MOD;
  }

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