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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll N, M;
vector<int> A;

int main() {
  cin >> N >> M;
  A.resize(M);
  rep(i, M) cin >> A[i];
  if (M == 1) {
    cout << A[0] << endl;
    if (A[0] > 1) {
      cout << 2 << endl;
      cout << A[0] - 1 << " " << 1 << endl;
    } else {
      cout << 1 << endl;
      cout << 1 << endl;
    }
    return 0;
  }
  vector<int> os;
  rep(i, M) {
    if (A[i] % 2 == 1)
      os.push_back(i);
  }
  if (os.size() > 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (os.size() == 1) {
    swap(A[0], A[os[0]]);
  } else {
    swap(A[0], A[os[0]]);
    swap(A[M - 1], A[os[1]]);
  }
  rep(i, M) cout << A[i] << " ";
  cout << endl;
  if (A[0] - 1 > 0) {
    cout << M << endl;
    cout << A[0] - 1 << " ";
  } else {
    cout << M - 1 << endl;
  }
  repl(i, 1, M - 1) cout << A[i] << " ";
  cout << A[M - 1] + 1 << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll N, M;
vector<int> A;

int main() {
  cin >> N >> M;
  A.resize(M);
  rep(i, M) cin >> A[i];
  if (M == 1) {
    cout << A[0] << endl;
    if (A[0] > 1) {
      cout << 2 << endl;
      cout << A[0] - 1 << " " << 1 << endl;
    } else {
      cout << 1 << endl;
      cout << 1 << endl;
    }
    return 0;
  }
  vector<int> os;
  rep(i, M) {
    if (A[i] % 2 == 1)
      os.push_back(i);
  }
  if (os.size() > 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (os.size() == 1) {
    swap(A[0], A[os[0]]);
  } else if (os.size() == 2) {
    swap(A[0], A[os[0]]);
    swap(A[M - 1], A[os[1]]);
  }
  rep(i, M) cout << A[i] << " ";
  cout << endl;
  if (A[0] - 1 > 0) {
    cout << M << endl;
    cout << A[0] - 1 << " ";
  } else {
    cout << M - 1 << endl;
  }
  repl(i, 1, M - 1) cout << A[i] << " ";
  cout << A[M - 1] + 1 << endl;
  return 0;
}
Problem p04050 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 100
using namespace std;
int n, m, a[N], b[N], bl;
int main() {
  scanf("%d%d", &n, &m);
  int t = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    if (a[i] % 2)
      t++;
  }
  if (m == 1) {
    if (a[1] > 1)
      b[++bl] = a[1] - 1;
    b[++bl] = 1;
    printf("%d\n%d\n", a[1], bl);
    for (int i = 1; i <= bl; i++)
      printf("%d ", b[i]);
    return 0;
  }
  if (t > 2) {
    printf("Impossible\n");
    return 0;
  }
  for (int i = 1; i <= m; i++)
    if (a[i] % 2) {
      swap(a[i], a[1]);
      break;
    }
  for (int i = 2; i <= m; i++)
    if (a[i] % 2) {
      swap(a[i], a[m]);
      break;
    }
  if (a[1] > 1)
    b[++bl] = a[1] - 1;
  for (int i = 2; i < m; i++)
    b[++bl] = a[i];
  b[++bl] = a[m] + 1;
  for (int i = 1; i <= m; i++)
    printf("%d ", a[i]);
  printf("\n");
  printf("%d\n", bl);
  for (int i = 1; i <= bl; i++)
    printf("%d ", b[i]);
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 110
using namespace std;
int n, m, a[N], b[N], bl;
int main() {
  scanf("%d%d", &n, &m);
  int t = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    if (a[i] % 2)
      t++;
  }
  if (m == 1) {
    if (a[1] > 1)
      b[++bl] = a[1] - 1;
    b[++bl] = 1;
    printf("%d\n%d\n", a[1], bl);
    for (int i = 1; i <= bl; i++)
      printf("%d ", b[i]);
    return 0;
  }
  if (t > 2) {
    printf("Impossible\n");
    return 0;
  }
  for (int i = 1; i <= m; i++)
    if (a[i] % 2) {
      swap(a[i], a[1]);
      break;
    }
  for (int i = 2; i <= m; i++)
    if (a[i] % 2) {
      swap(a[i], a[m]);
      break;
    }
  if (a[1] > 1)
    b[++bl] = a[1] - 1;
  for (int i = 2; i < m; i++)
    b[++bl] = a[i];
  b[++bl] = a[m] + 1;
  for (int i = 1; i <= m; i++)
    printf("%d ", a[i]);
  printf("\n");
  printf("%d\n", bl);
  for (int i = 1; i <= bl; i++)
    printf("%d ", b[i]);
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
// by OIerC
// Forca Barcelona!
#include <cstdio>
using namespace std;
const int P = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  a >= P ? a -= P : 0;
}
void sub(int &a, int b) {
  a -= b;
  a < 0 ? a += P : 0;
}

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

const int M = 2e3 + 5, N = 2e5 + 5;
int a[N], b[N], dp[M * 2][M * 2];
int fac[M * 4 + 5], ifac[M * 4 + 5];
int ans;

int Pow(int x, int t) {
  int res = 1;
  for (; t; t >>= 1, x = 1LL * x * x % P)
    if (t & 1)
      res = 1LL * res * x % P;
  return res;
}

int C(int n, int m) {
  if (n < m)
    return 0;
  return 1LL * fac[n] * ifac[n - m] % P * ifac[m] % P;
}

int main() {
  int n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();

  for (int i = 1; i <= n; ++i)
    ++dp[M - a[i]][M - b[i]];
  for (int i = 1; i <= 2 * M; ++i)
    for (int j = 1; j <= 2 * M; ++j)
      add(dp[i][j], (dp[i][j - 1] + dp[i - 1][j]) % P);
  for (int i = 1; i <= n; ++i)
    add(ans, dp[a[i] + M][b[i] + M]);

  fac[0] = 1;
  for (int i = 1; i <= 4 * M; ++i)
    fac[i] = 1LL * fac[i - 1] * i % P;
  ifac[4 * M] = Pow(fac[4 * M], P - 2);
  for (int i = 4 * M; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % P;

  for (int i = 1; i <= n; ++i)
    sub(ans, C(2 * a[i] + 2 * b[i], 2 * a[i]));

  printf("%d\n", 1LL * ans * ifac[2] % P);
  return 0;
}


Fixed submission:
// by OIerC
// Forca Barcelona!
#include <cstdio>
using namespace std;
const int P = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  a >= P ? a -= P : 0;
}
void sub(int &a, int b) {
  a -= b;
  a < 0 ? a += P : 0;
}

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

const int M = 2e3 + 5, N = 2e5 + 5;
int a[N], b[N], dp[M * 2][M * 2];
int fac[M * 4 + 5], ifac[M * 4 + 5];
int ans;

int Pow(int x, int t) {
  int res = 1;
  for (; t; t >>= 1, x = 1LL * x * x % P)
    if (t & 1)
      res = 1LL * res * x % P;
  return res;
}

int C(int n, int m) {
  if (n < m)
    return 0;
  return 1LL * fac[n] * ifac[n - m] % P * ifac[m] % P;
}

int main() {
  int n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();

  for (int i = 1; i <= n; ++i)
    ++dp[M - a[i]][M - b[i]];
  for (int i = 1; i < 2 * M; ++i)
    for (int j = 1; j < 2 * M; ++j)
      add(dp[i][j], (dp[i][j - 1] + dp[i - 1][j]) % P);
  for (int i = 1; i <= n; ++i)
    add(ans, dp[a[i] + M][b[i] + M]);

  fac[0] = 1;
  for (int i = 1; i <= 4 * M; ++i)
    fac[i] = 1LL * fac[i - 1] * i % P;
  ifac[4 * M] = Pow(fac[4 * M], P - 2);
  for (int i = 4 * M; i; --i)
    ifac[i - 1] = 1LL * ifac[i] * i % P;

  for (int i = 1; i <= n; ++i)
    sub(ans, C(2 * a[i] + 2 * b[i], 2 * a[i]));

  printf("%d\n", 1LL * ans * ifac[2] % P);
  return 0;
}
Problem p04051 (C++)

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

using namespace std;

#define M 4005
#define N 200005
#define delta 2001
#define MOD 1000000007
#define ll int
// #define int long long

int n;
int a[N], b[N];
ll ans;
int f[M][M], mns[M][M];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    (x *= 10) += ch - '0';
    ch = getchar();
  }
  return x * f;
}

signed main() {
  freopen("01-03.txt", "r", stdin);
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    f[-a[i] + delta][-b[i] + delta]++;
  }

  for (int i = 1; i <= delta * 2; i++)
    for (int j = 1; j <= delta * 2; j++)
      (f[i][j] += (f[i - 1][j] + f[i][j - 1]) % MOD) %= MOD;
  mns[1][1] = 1;
  for (int i = 1; i <= 4004; i++)
    for (int j = 1; j <= 4004; j++)
      (mns[i][j] += (mns[i - 1][j] + mns[i][j - 1]) % MOD) %= MOD;

  for (int i = 1; i <= n; i++) {
    (ans += f[a[i] + delta][b[i] + delta]) %= MOD;
    ((ans -= mns[a[i] * 2 + 1][b[i] * 2 + 1]) += MOD) %= MOD;
  }

  cout << 1ll * ans * 500000004 % MOD;

  return 0;
}

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

using namespace std;

#define M 4005
#define N 200005
#define delta 2001
#define MOD 1000000007
#define ll int
// #define int long long

int n;
int a[N], b[N];
ll ans;
int f[M][M], mns[M][M];

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    (x *= 10) += ch - '0';
    ch = getchar();
  }
  return x * f;
}

signed main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    f[-a[i] + delta][-b[i] + delta]++;
  }

  for (int i = 1; i <= delta * 2; i++)
    for (int j = 1; j <= delta * 2; j++)
      (f[i][j] += (f[i - 1][j] + f[i][j - 1]) % MOD) %= MOD;
  mns[1][1] = 1;
  for (int i = 1; i <= 4004; i++)
    for (int j = 1; j <= 4004; j++)
      (mns[i][j] += (mns[i - 1][j] + mns[i][j - 1]) % MOD) %= MOD;

  for (int i = 1; i <= n; i++) {
    (ans += f[a[i] + delta][b[i] + delta]) %= MOD;
    ((ans -= mns[a[i] * 2 + 1][b[i] * 2 + 1]) += MOD) %= MOD;
  }

  cout << 1ll * ans * 500000004 % MOD;

  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4005, M = 1e9 + 7;
int fac[N * 2], n, inv[N * 2], dp[N][N], a[N], b[N];
int ksm(int x, int y) {
  if (!y)
    return 1;
  int z = ksm(x, y / 2);
  z *= z;
  z %= M;
  if (y & 1)
    z *= x;
  return z % M;
}
int C(int x, int y) { return fac[x] * inv[y] % M * inv[x - y] % M; }
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &a[i], &b[i]);
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= 8000; i++)
    fac[i] = fac[i - 1] * i % M, inv[i] = ksm(fac[i], M - 2);
  for (int i = 1; i <= n; i++)
    dp[-a[i] + 2001][-b[i] + 2001]++;
  for (int i = 1; i <= 4001; i++)
    for (int j = 1; j <= 4001; j++)
      (dp[i][j] += dp[i - 1][j] + dp[i][j - 1]) %= M;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += dp[a[i] + 2001][b[i] + 2001]) %= M;
  for (int i = 1; i <= n; i++)
    (ans += M - C(a[i] + b[i] + a[i] + b[i], a[i] + a[i])) %= M;
  printf("%lld\n", ans * ksm(2, M - 2) % M);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4005, M = 1e9 + 7;
int fac[N * 2], n, inv[N * 2], dp[N][N], a[N * 50], b[N * 50];
int ksm(int x, int y) {
  if (!y)
    return 1;
  int z = ksm(x, y / 2);
  z *= z;
  z %= M;
  if (y & 1)
    z *= x;
  return z % M;
}
int C(int x, int y) { return fac[x] * inv[y] % M * inv[x - y] % M; }
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &a[i], &b[i]);
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= 8000; i++)
    fac[i] = fac[i - 1] * i % M, inv[i] = ksm(fac[i], M - 2);
  for (int i = 1; i <= n; i++)
    dp[-a[i] + 2001][-b[i] + 2001]++;
  for (int i = 1; i <= 4001; i++)
    for (int j = 1; j <= 4001; j++)
      (dp[i][j] += dp[i - 1][j] + dp[i][j - 1]) %= M;
  int ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += dp[a[i] + 2001][b[i] + 2001]) %= M;
  for (int i = 1; i <= n; i++)
    (ans += M - C(a[i] + b[i] + a[i] + b[i], a[i] + a[i])) %= M;
  printf("%lld\n", ans * ksm(2, M - 2) % M);
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = (0); i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> inline void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = 4005, lim = 4000, mod = 1e9 + 7;
int f[N][N], C[N][N], a[N], b[N], n, ans;

void add(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); }

int main() {
  read(n);
  rep(i, 1, n) {
    read(a[i]), read(b[i]);
    f[lim / 2 - a[i]][lim / 2 - b[i]]++;
  }
  C[0][0] = 1;
  rep(i, 0, lim) rep(j, 0, lim) {
    if (i) {
      add(f[i][j], f[i - 1][j]);
      add(C[i][j], C[i - 1][j]);
    }
    if (j) {
      add(f[i][j], f[i][j - 1]);
      add(C[i][j], C[i][j - 1]);
    }
  }
  rep(i, 1, n) add(ans, f[lim / 2 + a[i]][lim / 2 + b[i]]);
  rep(i, 1, n) ans = (ans + mod - C[a[i] + a[i]][b[i] + b[i]]) % mod;
  ans = (ll)ans * (mod / 2 + 1) % mod;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = (0); i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

template <class T> inline void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  if (f)
    x = -x;
}

const int N = 4005, lim = 4000, mod = 1e9 + 7;
int f[N][N], C[N][N], a[2000005], b[2000005], n, ans;

void add(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); }

int main() {
  read(n);
  rep(i, 1, n) {
    read(a[i]), read(b[i]);
    f[lim / 2 - a[i]][lim / 2 - b[i]]++;
  }
  C[0][0] = 1;
  rep(i, 0, lim) rep(j, 0, lim) {
    if (i) {
      add(f[i][j], f[i - 1][j]);
      add(C[i][j], C[i - 1][j]);
    }
    if (j) {
      add(f[i][j], f[i][j - 1]);
      add(C[i][j], C[i][j - 1]);
    }
  }
  rep(i, 1, n) add(ans, f[lim / 2 + a[i]][lim / 2 + b[i]]);
  rep(i, 1, n) ans = (ans + mod - C[a[i] + a[i]][b[i] + b[i]]) % mod;
  ans = (ll)ans * (mod / 2 + 1) % mod;
  cout << ans << endl;
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define SIZE 4005
#define MD 2002
#define MOD 1000000007LL

using namespace std;
typedef long long int ll;

ll dp[SIZE][SIZE];
ll fac[SIZE * 2], finv[SIZE * 2], inv[SIZE * 2];
int A[SIZE], B[SIZE];

void make() {
  inv[1] = 1;
  fac[0] = fac[1] = finv[0] = finv[1] = 1;
  for (int i = 2; i < SIZE * 2; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll C(int a, int b) {
  if (a < b)
    return 0;
  return fac[a] * (finv[b] * finv[a - b] % MOD) % MOD;
}
int main() {
  make();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &A[i], &B[i]);
    dp[MD - A[i]][MD - B[i]]++;
  }
  for (int i = 0; i < SIZE; i++) {
    for (int j = 0; j < SIZE; j++) {
      if (i > 0)
        dp[i][j] += dp[i - 1][j];
      if (j > 0)
        dp[i][j] += dp[i][j - 1];
      if (dp[i][j] >= MOD)
        dp[i][j] -= MOD;
    }
  }
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    ret += dp[MD + A[i]][MD + B[i]];
    if (ret >= MOD)
      ret -= MOD;
    ret -= C(2 * (A[i] + B[i]), 2 * A[i]);
    if (ret < 0)
      ret += MOD;
  }
  ret *= inv[2];
  ret %= MOD;
  printf("%lld\n", ret);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define SIZE 4005
#define MD 2002
#define MOD 1000000007LL

using namespace std;
typedef long long int ll;

ll dp[SIZE][SIZE];
ll fac[SIZE * 2], finv[SIZE * 2], inv[SIZE * 2];
int A[SIZE * 50], B[SIZE * 50];

void make() {
  inv[1] = 1;
  fac[0] = fac[1] = finv[0] = finv[1] = 1;
  for (int i = 2; i < SIZE * 2; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll C(int a, int b) {
  if (a < b)
    return 0;
  return fac[a] * (finv[b] * finv[a - b] % MOD) % MOD;
}
int main() {
  make();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &A[i], &B[i]);
    dp[MD - A[i]][MD - B[i]]++;
  }
  for (int i = 0; i < SIZE; i++) {
    for (int j = 0; j < SIZE; j++) {
      if (i > 0)
        dp[i][j] += dp[i - 1][j];
      if (j > 0)
        dp[i][j] += dp[i][j - 1];
      if (dp[i][j] >= MOD)
        dp[i][j] -= MOD;
    }
  }
  ll ret = 0;
  for (int i = 0; i < n; i++) {
    ret += dp[MD + A[i]][MD + B[i]];
    if (ret >= MOD)
      ret -= MOD;
    ret -= C(2 * (A[i] + B[i]), 2 * A[i]);
    if (ret < 0)
      ret += MOD;
  }
  ret *= inv[2];
  ret %= MOD;
  printf("%lld\n", ret);
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (register int i = (a), _b = (b); i <= _b; i++)
inline int rd(void) {
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  int x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int N = 200005;
const int S = 8000;
const int M = 2000;
const int P = 1e9 + 7;

int n, a[N], b[N];
int fac[S + 5], inv[S + 5], ifac[S + 5];
int f[4005][4005];
int ans;

inline void add(int &x, int y) { x = (x + y) % P; }
inline int C(int n, int m) {
  return 1LL * fac[n] * ifac[m] % P * ifac[n - m] % P;
}

int main(void) {
#ifndef ONLINE_JUDGE
  freopen("agc.in", "r", stdin);
#endif

  n = rd();
  F(i, 1, n) a[i] = rd(), b[i] = rd();

  fac[0] = 1;
  F(i, 1, S) fac[i] = 1LL * fac[i - 1] * i % P;
  inv[1] = 1;
  F(i, 2, S) inv[i] = 1LL * (P - P / i) * inv[P % i] % P;
  ifac[0] = 1;
  F(i, 1, S) ifac[i] = 1LL * ifac[i - 1] * inv[i] % P;

  F(i, 1, n) f[-a[i] + M][-b[i] + M]++;
  F(i, -M, M) F(j, -M, M) if (f[i + M][j + M] != 0) {
    if (i < M)
      add(f[i + 1 + M][j + M], f[i + M][j + M]);
    if (j < M)
      add(f[i + M][j + 1 + M], f[i + M][j + M]);
  }

  F(i, 1, n)
  add(ans, f[a[i] + M][b[i] + M]);
  F(i, 1, n)
  add(ans, -C(a[i] + a[i] + b[i] + b[i], a[i] + a[i]));
  ans = 1LL * ans * inv[2] % P;
  printf("%d\n", (ans + P) % P);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, b) for (register int i = (a), _b = (b); i <= _b; i++)
inline int rd(void) {
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  int x = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int N = 200005;
const int S = 8000;
const int M = 2000;
const int P = 1e9 + 7;

int n, a[N], b[N];
int fac[S + 5], inv[S + 5], ifac[S + 5];
int f[4005][4005];
int ans;

inline void add(int &x, int y) { x = (x + y) % P; }
inline int C(int n, int m) {
  return 1LL * fac[n] * ifac[m] % P * ifac[n - m] % P;
}

int main(void) {
  n = rd();
  F(i, 1, n) a[i] = rd(), b[i] = rd();

  fac[0] = 1;
  F(i, 1, S) fac[i] = 1LL * fac[i - 1] * i % P;
  inv[1] = 1;
  F(i, 2, S) inv[i] = 1LL * (P - P / i) * inv[P % i] % P;
  ifac[0] = 1;
  F(i, 1, S) ifac[i] = 1LL * ifac[i - 1] * inv[i] % P;

  F(i, 1, n) f[-a[i] + M][-b[i] + M]++;
  F(i, -M, M) F(j, -M, M) if (f[i + M][j + M] != 0) {
    if (i < M)
      add(f[i + 1 + M][j + M], f[i + M][j + M]);
    if (j < M)
      add(f[i + M][j + 1 + M], f[i + M][j + M]);
  }

  F(i, 1, n)
  add(ans, f[a[i] + M][b[i] + M]);
  F(i, 1, n)
  add(ans, -C(a[i] + a[i] + b[i] + b[i], a[i] + a[i]));
  ans = 1LL * ans * inv[2] % P;
  printf("%d\n", (ans + P) % P);

  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;

inline int read() {
  int sum = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return f * sum;
}

const int N = 8005;
const int M = 2002;
const int P = 2000001;
const int Mod = 1000000007;
int n, a[P], b[P];
int f[N][N], cj[P] = {1};

inline ll fpow(int a, int b) {
  ll ret = 1;
  for (; b; a = 1ll * a * a % Mod, b >>= 1)
    if (b & 1)
      ret = 1ll * ret * a % Mod;
  return ret;
}

inline ll inv(int x) { return fpow(x, Mod - 2); }

inline ll C(int m, int n) {
  if (n < m)
    return 0;
  return 1ll * cj[n] * inv(cj[m]) % Mod * inv(cj[n - m]) % Mod;
}

signed main() {
  //	freopen("3.in","r",stdin);
  n = read();
  for (int i = 1; i < P; i++)
    cj[i] = 1ll * cj[i - 1] * i % Mod;
  for (int i = 1; i <= n; i++)
    a[i] = read(), b[i] = read();
  for (int i = 1; i <= n; i++)
    f[M - a[i]][M - b[i]]++;
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      f[i][j] = (f[i][j] + f[i - 1][j] + f[i][j - 1]) % Mod;
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans = (ll)(ans + f[a[i] + M][b[i] + M]) % Mod;
  for (int i = 1; i <= n; i++)
    ans = (ll)(ans - C(a[i] << 1, (a[i] + b[i]) << 1)) % Mod;
  ans = (ans + Mod) % Mod;
  ans = 1ll * ans * inv(2) % Mod;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;

inline int read() {
  int sum = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return f * sum;
}

const int N = 5005;
const int M = 2002;
const int P = 2000001;
const int Mod = 1000000007;
int n, a[P], b[P];
int f[N][N], cj[P] = {1};

inline ll fpow(int a, int b) {
  ll ret = 1;
  for (; b; a = 1ll * a * a % Mod, b >>= 1)
    if (b & 1)
      ret = 1ll * ret * a % Mod;
  return ret;
}

inline ll inv(int x) { return fpow(x, Mod - 2); }

inline ll C(int m, int n) {
  if (n < m)
    return 0;
  return 1ll * cj[n] * inv(cj[m]) % Mod * inv(cj[n - m]) % Mod;
}

signed main() {
  //	freopen("3.in","r",stdin);
  n = read();
  for (int i = 1; i < P; i++)
    cj[i] = 1ll * cj[i - 1] * i % Mod;
  for (int i = 1; i <= n; i++)
    a[i] = read(), b[i] = read();
  for (int i = 1; i <= n; i++)
    f[M - a[i]][M - b[i]]++;
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      f[i][j] = (f[i][j] + f[i - 1][j] + f[i][j - 1]) % Mod;
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans = (ll)(ans + f[a[i] + M][b[i] + M]) % Mod;
  for (int i = 1; i <= n; i++)
    ans = (ll)(ans - C(a[i] << 1, (a[i] + b[i]) << 1)) % Mod;
  ans = (ans + Mod) % Mod;
  ans = 1ll * ans * inv(2) % Mod;
  printf("%lld\n", ans);
  return 0;
}
Problem p04051 (C++)

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

using namespace std;

typedef long long ll;

const int maxn = 2e5 + 3;
const int maxB = 4000 + 10;
const int mod = 1e9 + 7;
int B = 2000;
int x[maxn], y[maxn];
int fac[maxB], rev[maxB], dp[maxB][maxB];

int power(int a, int b) {
  if (b == 0)
    return 1;
  int ret = power(a, b / 2);
  ret = 1ll * ret * ret % mod;
  if (b & 1)
    ret = 1ll * ret * a % mod;
  return ret;
}

int Choose(int n, int r) {
  return 1ll * fac[n] * rev[r] % mod * rev[n - r] % mod;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  fac[0] = 1;
  for (int i = 1; i <= 4 * B; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  rev[4 * B] = power(fac[4 * B], mod - 2);
  for (int i = 4 * B - 1; i >= 0; i--)
    rev[i] = 1ll * rev[i + 1] * (i + 1) % mod;

  for (int i = 0; i < n; i++)
    dp[-x[i] + B][-y[i] + B]++;

  for (int i = 0; i <= 2 * B; i++) {
    for (int j = 0; j <= 2 * B; j++) {
      if (i > 0)
        dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
      if (j > 0)
        dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
    }
  }
  int answer = 0;
  for (int i = 0; i < n; i++) {
    answer = (answer + dp[x[i] + B][y[i] + B]) % mod;
    answer = (answer - Choose(2 * (x[i] + y[i]), 2 * x[i]) + mod) % mod;
  }
  cout << 1ll * answer * power(2, mod - 2) % mod << endl;
}


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

using namespace std;

typedef long long ll;

const int maxn = 2e5 + 3;
const int maxB = 8000 + 10;
const int mod = 1e9 + 7;
int B = 2000;
int x[maxn], y[maxn];
int fac[maxB], rev[maxB], dp[maxB][maxB];

int power(int a, int b) {
  if (b == 0)
    return 1;
  int ret = power(a, b / 2);
  ret = 1ll * ret * ret % mod;
  if (b & 1)
    ret = 1ll * ret * a % mod;
  return ret;
}

int Choose(int n, int r) {
  return 1ll * fac[n] * rev[r] % mod * rev[n - r] % mod;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  fac[0] = 1;
  for (int i = 1; i <= 4 * B; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  rev[4 * B] = power(fac[4 * B], mod - 2);
  for (int i = 4 * B - 1; i >= 0; i--)
    rev[i] = 1ll * rev[i + 1] * (i + 1) % mod;

  for (int i = 0; i < n; i++)
    dp[-x[i] + B][-y[i] + B]++;

  for (int i = 0; i <= 2 * B; i++) {
    for (int j = 0; j <= 2 * B; j++) {
      if (i > 0)
        dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod;
      if (j > 0)
        dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
    }
  }
  int answer = 0;
  for (int i = 0; i < n; i++) {
    answer = (answer + dp[x[i] + B][y[i] + B]) % mod;
    answer = (answer - Choose(2 * (x[i] + y[i]), 2 * x[i]) + mod) % mod;
  }
  cout << 1ll * answer * power(2, mod - 2) % mod << endl;
}
Problem p04051 (C++)

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

using namespace std;

const int maxn = 200000 + 5;
const int maxa = 2000 + 5;
const int MOD = 1e9 + 7;

#define inv(x) pow_mod(x, MOD - 2)

int pow_mod(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MOD)
    if (y & 1)
      ret = 1ll * ret * x % MOD;
  return ret;
}

int fac[maxa << 1], inv_fac[maxa << 1];

void init(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % MOD;

  inv_fac[n] = inv(fac[n]);
  for (int i = n; i >= 1; i--)
    inv_fac[i - 1] = 1ll * inv_fac[i] * i % MOD;
}

int C(int n, int m) {
  return 1ll * fac[n] * inv_fac[m] % MOD * inv_fac[n - m] % MOD;
}

#define f(i, j) f[i + mxa][j + mxb]

int f[maxa << 1][maxa << 1];
int A[maxn], B[maxn], N, mxa, mxb;

int solve() {
  for (int i = 1; i <= N; i++)
    ++f(-A[i], -B[i]);

  for (int i = -mxa; i <= mxa; i++)
    for (int j = -mxb; j <= mxb; j++) {
      if (i > -mxa)
        f(i, j) = (f(i, j) + f(i - 1, j)) % MOD;
      if (j > -mxb)
        f(i, j) = (f(i, j) + f(i, j - 1)) % MOD;
    }

  int ans = 0;
  for (int i = 1; i <= N; i++)
    ans = (ans + f(A[i], B[i])) % MOD;

  init(mxa + mxb << 1);
  for (int i = 1; i <= N; i++)
    ans = (ans - C(A[i] + B[i] << 1, A[i] << 1) + MOD) % MOD;

  return 1ll * ans * inv(2) % MOD;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &A[i], &B[i]);
    mxa = max(mxa, A[i]);
    mxb = max(mxb, B[i]);
  }

  printf("%d\n", solve());
  return 0;
}

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

using namespace std;

const int maxn = 200000 + 5;
const int maxa = 2000 + 5;
const int MOD = 1e9 + 7;

#define inv(x) pow_mod(x, MOD - 2)

int pow_mod(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MOD)
    if (y & 1)
      ret = 1ll * ret * x % MOD;
  return ret;
}

int fac[maxa << 2], inv_fac[maxa << 2];

void init(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % MOD;

  inv_fac[n] = inv(fac[n]);
  for (int i = n; i >= 1; i--)
    inv_fac[i - 1] = 1ll * inv_fac[i] * i % MOD;
}

int C(int n, int m) {
  return 1ll * fac[n] * inv_fac[m] % MOD * inv_fac[n - m] % MOD;
}

#define f(i, j) f[i + mxa][j + mxb]

int f[maxa << 1][maxa << 1];
int A[maxn], B[maxn], N, mxa, mxb;

int solve() {
  for (int i = 1; i <= N; i++)
    ++f(-A[i], -B[i]);

  for (int i = -mxa; i <= mxa; i++)
    for (int j = -mxb; j <= mxb; j++) {
      if (i > -mxa)
        f(i, j) = (f(i, j) + f(i - 1, j)) % MOD;
      if (j > -mxb)
        f(i, j) = (f(i, j) + f(i, j - 1)) % MOD;
    }

  int ans = 0;
  for (int i = 1; i <= N; i++)
    ans = (ans + f(A[i], B[i])) % MOD;

  init(mxa + mxb << 1);
  for (int i = 1; i <= N; i++)
    ans = (ans - C(A[i] + B[i] << 1, A[i] << 1) + MOD) % MOD;

  return 1ll * ans * inv(2) % MOD;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &A[i], &B[i]);
    mxa = max(mxa, A[i]);
    mxb = max(mxb, B[i]);
  }

  printf("%d\n", solve());
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 4001;
const ll p = 1e9 + 7;
int A[N + 5], B[N + 5];
ll a[N + 5][N + 5], tag[N + 5][N + 5];
ll fac[(N << 1) + 10], ifac[(N << 1) + 10];

inline void add(ll &a, ll b) { a = a + b >= p ? a + b - p : a + b; }

ll fpm(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1)
    ret = (b & 1) ? ret * a % p : ret, a = a * a % p;
  return ret;
}

int main() {
  int n, mx = 0, my = 0;
  scanf("%d", &n);
  for (int i = 1, x, y; i <= n; ++i) {
    scanf("%d %d", &x, &y);
    A[i] = x;
    B[i] = y;
    mx = max(mx, x);
    my = max(my, y);
    ++a[2001 + x][2001 + y];
    ++tag[2001 - x][2001 - y];
  };
  mx += 2001;
  my += 2001;
  ll ans = 0;
  for (int i = mx; i >= 1; --i)
    for (int j = my; j >= 1; --j) {
      add(a[i][j - 1], a[i][j]);
      add(a[i - 1][j], a[i][j]);
      if (tag[i][j])
        add(ans, tag[i][j] * a[i][j] % p);
    };

  fac[1] = 1;
  for (int i = 2; i <= (N << 1); ++i)
    fac[i] = fac[i - 1] * i % p;
  ifac[N << 1] = fpm(fac[N << 1], p - 2);
  for (int i = (N << 1) - 1; i >= 1; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % p;

  ll tmp = 0;
  for (int i = 1; i <= n; ++i)
    add(tmp, fac[2 * (A[i] + B[i])] * ifac[2 * A[i]] % p * ifac[2 * B[i]] % p);
  add(ans, p - tmp);
  ans = ans * fpm(2, p - 2) % p;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 4001;
const ll p = 1e9 + 7;
int A[200010], B[200010];
ll a[N + 5][N + 5], tag[N + 5][N + 5];
ll fac[(N << 1) + 10], ifac[(N << 1) + 10];

inline void add(ll &a, ll b) { a = a + b >= p ? a + b - p : a + b; }

ll fpm(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1)
    ret = (b & 1) ? ret * a % p : ret, a = a * a % p;
  return ret;
}

int main() {
  int n, mx = 0, my = 0;
  scanf("%d", &n);
  for (int i = 1, x, y; i <= n; ++i) {
    scanf("%d %d", &x, &y);
    A[i] = x;
    B[i] = y;
    mx = max(mx, x);
    my = max(my, y);
    ++a[2001 + x][2001 + y];
    ++tag[2001 - x][2001 - y];
  };
  mx += 2001;
  my += 2001;
  ll ans = 0;
  for (int i = mx; i >= 1; --i)
    for (int j = my; j >= 1; --j) {
      add(a[i][j - 1], a[i][j]);
      add(a[i - 1][j], a[i][j]);
      if (tag[i][j])
        add(ans, tag[i][j] * a[i][j] % p);
    };

  fac[1] = 1;
  for (int i = 2; i <= (N << 1); ++i)
    fac[i] = fac[i - 1] * i % p;
  ifac[N << 1] = fpm(fac[N << 1], p - 2);
  for (int i = (N << 1) - 1; i >= 1; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % p;

  ll tmp = 0;
  for (int i = 1; i <= n; ++i)
    add(tmp, fac[2 * (A[i] + B[i])] * ifac[2 * A[i]] % p * ifac[2 * B[i]] % p);
  add(ans, p - tmp);
  ans = ans * fpm(2, p - 2) % p;
  printf("%lld\n", ans);
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
const int MOD = 1e9 + 7;
int p[2 * N][2 * N];
int C[4 * N][2 * N];
int main() {
  for (int i = 0; i < 4 * N; i++)
    for (int j = 0; j <= min(i, 2 * N); j++)
      if (i == j)
        C[i][j] = 1;
      else if (j == 0)
        C[i][j] = 1;
      else
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
  int n;
  int ans = 0;
  int ans2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    p[N - x][N - y]++;
    p[N + x][N + y]++;
    ans = (ans + C[2 * (x + y)][2 * x]) % MOD;
  }
  for (int i = 1; i < 2 * N; i++)
    for (int j = 1; j < 2 * N; j++) {
      if (i > N && j > N && p[i][j]) {
        ans2 = ((1ll * p[i][j] * ((p[i - 1][j] + p[i][j - 1]) % MOD)) % MOD +
                ans2) %
               MOD;
        p[i][j] = (p[i - 1][j] + p[i][j - 1]) % MOD;
      } else
        p[i][j] = (1ll * p[i - 1][j] + p[i][j - 1] + p[i][j]) % MOD;
    }
  ans = (ans2 + MOD - ans) % MOD;
  if (ans % 2)
    ans += MOD;
  printf("%d\n", ans / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
const int MOD = 1e9 + 7;
int p[2 * N][2 * N];
int C[4 * N][2 * N];
int main() {
  for (int i = 0; i < 4 * N; i++)
    for (int j = 0; j <= min(i, 2 * N - 1); j++)
      if (i == j)
        C[i][j] = 1;
      else if (j == 0)
        C[i][j] = 1;
      else
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
  int n;
  int ans = 0;
  int ans2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    p[N - x][N - y]++;
    p[N + x][N + y]++;
    ans = (ans + C[2 * (x + y)][2 * x]) % MOD;
  }
  for (int i = 1; i < 2 * N; i++)
    for (int j = 1; j < 2 * N; j++) {
      if (i > N && j > N && p[i][j]) {
        ans2 = ((1ll * p[i][j] * ((p[i - 1][j] + p[i][j - 1]) % MOD)) % MOD +
                ans2) %
               MOD;
        p[i][j] = (p[i - 1][j] + p[i][j - 1]) % MOD;
      } else
        p[i][j] = (1ll * p[i - 1][j] + p[i][j - 1] + p[i][j]) % MOD;
    }
  ans = (ans2 + MOD - ans) % MOD;
  if (ans % 2)
    ans += MOD;
  printf("%d\n", ans / 2);
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Memory Limit Exceeded):
/*  بسم الله الرحمن الرحیم  */

/*  شهید حاج قاسم سلیمانی
    انتقام سخت نزدیک است
*/

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

// #define int ll

#define all(x) x.begin(), x.end()

#define PB push_back
#define F first
#define S second
#define _sz(x) (int)x.size()

#define debug(x) cerr << #x << " = " << x << endl

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

const int MAX_N = 2010, MAX_X = 2010 * 2, MAX_Y = 2010 * 4,
          MAX_M = 200 * 1000 + 19;
const int inf = 2e9 + 19;
const int MOD = 1e9 + 7;

ll a[MAX_M], b[MAX_M], n;
ll dp[MAX_X][MAX_X], C[MAX_Y][MAX_X];
ll ans = 0;

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

ll sum(ll x, ll y) {
  x += y;
  if (x < 0)
    x += MOD;
  else if (x >= MOD)
    x -= MOD;
  return x;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    dp[MAX_N - a[i]][MAX_N - b[i]]++;
  }
  for (int i = 1; i < MAX_X; i++)
    for (int j = 1; j < MAX_X; j++)
      dp[i][j] = sum(sum(dp[i][j], dp[i - 1][j]), dp[i][j - 1]);
  for (int i = 0; i < MAX_Y; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j < min(i, MAX_X); j++) {
      C[i][j] = sum(C[i - 1][j], C[i - 1][j - 1]);
      // if (i == 8038)
      //         debug(j) ;
    }
  }
  for (int i = 1; i <= n; i++)
    ans = sum(ans, dp[MAX_N + a[i]][MAX_N + b[i]]),
    ans = sum(ans, -C[a[i] * 2 + b[i] * 2][a[i] * 2]);
  ans = mul(ans, (MOD + 1) / 2);
  if (ans < 0)
    ans += MOD;
  cout << ans;
  return (0);
}


Fixed submission:
/*  بسم الله الرحمن الرحیم  */

/*  شهید حاج قاسم سلیمانی
    انتقام سخت نزدیک است
*/

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

// #define int ll

#define all(x) x.begin(), x.end()

#define PB push_back
#define F first
#define S second
#define _sz(x) (int)x.size()

#define debug(x) cerr << #x << " = " << x << endl

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

const int MAX_N = 2010, MAX_X = 2010 * 2, MAX_Y = 2010 * 4,
          MAX_M = 200 * 1000 + 19;
const int inf = 2e9 + 19;
const int MOD = 1e9 + 7;

int a[MAX_M], b[MAX_M], n;
int dp[MAX_X][MAX_X], C[MAX_Y][MAX_X];
ll ans = 0;

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

ll sum(ll x, ll y) {
  x += y;
  if (x < 0)
    x += MOD;
  else if (x >= MOD)
    x -= MOD;
  return x;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    dp[MAX_N - a[i]][MAX_N - b[i]]++;
  }
  for (int i = 1; i < MAX_X; i++)
    for (int j = 1; j < MAX_X; j++)
      dp[i][j] = sum(sum(dp[i][j], dp[i - 1][j]), dp[i][j - 1]);
  for (int i = 0; i < MAX_Y; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j < min(i, MAX_X); j++) {
      C[i][j] = sum(C[i - 1][j], C[i - 1][j - 1]);
      // if (i == 8038)
      //         debug(j) ;
    }
  }
  for (int i = 1; i <= n; i++)
    ans = sum(ans, dp[MAX_N + a[i]][MAX_N + b[i]]),
    ans = sum(ans, -C[a[i] * 2 + b[i] * 2][a[i] * 2]);
  ans = mul(ans, (MOD + 1) / 2);
  if (ans < 0)
    ans += MOD;
  cout << ans;
  return (0);
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int N = 2002, M = 1000000007;
typedef long long ll;
int n, i, j, a[N], b[N], f[N * 2][N * 2];
ll fac[N * 4], ans;
ll ksm(ll x, int p) {
  ll s = 1;
  while (p) {
    if (p & 1)
      s = s * x % M;
    p >>= 1;
    x = x * x % M;
  }
  return s;
}
ll inv(ll x) { return ksm(x, M - 2); }
void init() {
  fac[0] = 1;
  for (i = 1; i < N * 4; i++)
    fac[i] = i * fac[i - 1] % M;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", a + i, b + i);
    f[N - a[i]][N - b[i]]++;
    ans = (ans -
           fac[a[i] * 2 + b[i] * 2] * inv(fac[a[i] * 2]) % M *
               inv(fac[b[i] * 2]) % M +
           M) %
          M;
    // printf("%lld\n",fac[a[i]*2+b[i]*2]*inv(fac[a[i]+b[i]])%M*inv(fac[a[i]+b[i]])%M);
  }
}
void work() {
  for (i = 1; i < N * 2; i++)
    for (j = 1; j < N * 2; j++)
      f[i][j] = (1ll * f[i][j - 1] + f[i - 1][j] + f[i][j]) % M;
  for (i = 1; i <= n; i++) {
    // printf("%d\n",f[N+a[i]][N+b[i]]);
    ans = (ans + f[N + a[i]][N + b[i]]) % M;
  }
  // printf("%lld\n",ans);
  printf("%lld", ans * inv(2) % M);
}
int main() {
  init();
  work();
  // while(1);
  return 0;
}

Fixed submission:
#include <cstdio>
const int N = 2002, M = 1000000007;
typedef long long ll;
int n, i, j, a[N * 100], b[N * 100], f[N * 2][N * 2];
ll fac[N * 4], ans;
ll ksm(ll x, int p) {
  ll s = 1;
  while (p) {
    if (p & 1)
      s = s * x % M;
    p >>= 1;
    x = x * x % M;
  }
  return s;
}
ll inv(ll x) { return ksm(x, M - 2); }
void init() {
  fac[0] = 1;
  for (i = 1; i < N * 4; i++)
    fac[i] = i * fac[i - 1] % M;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", a + i, b + i);
    f[N - a[i]][N - b[i]]++;
    ans = (ans -
           fac[a[i] * 2 + b[i] * 2] * inv(fac[a[i] * 2]) % M *
               inv(fac[b[i] * 2]) % M +
           M) %
          M;
    // printf("%lld\n",fac[a[i]*2+b[i]*2]*inv(fac[a[i]+b[i]])%M*inv(fac[a[i]+b[i]])%M);
  }
}
void work() {
  for (i = 1; i < N * 2; i++)
    for (j = 1; j < N * 2; j++)
      f[i][j] = (1ll * f[i][j - 1] + f[i - 1][j] + f[i][j]) % M;
  for (i = 1; i <= n; i++) {
    // printf("%d\n",f[N+a[i]][N+b[i]]);
    ans = (ans + f[N + a[i]][N + b[i]]) % M;
  }
  // printf("%lld\n",ans);
  printf("%lld", ans * inv(2) % M);
}
int main() {
  init();
  work();
  // while(1);
  return 0;
}
Problem p04051 (C++)

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

using namespace std;

typedef long long ll;

const ll maxx = 4004, maxn = 2e5 + 20, mod = 1e9 + 7, gaam = 2002;

ll dp[maxx][maxx], fac[maxn], a[maxn], b[maxn], n;

ll pw(ll a, ll b) {
  if (!b)
    return 1ll;
  ll x = pw(a, b / 2);
  x = (x * x) % mod;
  if (b % 2)
    x = (x * a) % mod;
  return x;
}

ll sel(ll a, ll b) {
  ll x = 1ll;
  x = (x * fac[a]) % mod;
  x = (x * pw(fac[b], mod - 2)) % mod;
  x = (x * pw(fac[a - b], mod - 2)) % mod;
  return x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  fac[0] = 1ll;
  for (ll i = 1; i < maxn; i++)
    fac[i] = (fac[i - 1] * 1ll * i) % mod;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i] >> b[i], dp[-a[i] + gaam][-b[i] + gaam] += 1;
  for (ll i = -gaam; i <= gaam; i++)
    for (ll j = -gaam; j <= gaam; j++)
      //		if (dp[i + gaam][j + gaam] == 0)
      dp[i + gaam][j + gaam] =
          (dp[i + gaam][j + gaam] +
           ((i + gaam) ? dp[i - 1 + gaam][j + gaam] : 0ll) +
           ((j + gaam) ? dp[i + gaam][j - 1 + gaam] : 0ll)) %
          mod;
  //			cout << i << " " << j << "   " << dp[i + gaam][j + gaam] <<
  //endl;
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans = (ans + dp[a[i] + gaam][b[i] + gaam]) % mod,
    ans = (((ans - sel(a[i] * 2 + b[i] * 2, a[i] * 2)) % mod) + mod) % mod;
  ans = (ans * pw(2, mod - 2)) % mod;
  cout << ans << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

const ll maxx = 4004, maxn = 2e5 + 20, mod = 1e9 + 7, gaam = 2001;

ll dp[maxx][maxx], fac[maxn], a[maxn], b[maxn], n;

ll pw(ll a, ll b) {
  if (!b)
    return 1ll;
  ll x = pw(a, b / 2);
  x = (x * x) % mod;
  if (b % 2)
    x = (x * a) % mod;
  return x;
}

ll sel(ll a, ll b) {
  ll x = 1ll;
  x = (x * fac[a]) % mod;
  x = (x * pw(fac[b], mod - 2)) % mod;
  x = (x * pw(fac[a - b], mod - 2)) % mod;
  return x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  fac[0] = 1ll;
  for (ll i = 1; i < maxn; i++)
    fac[i] = (fac[i - 1] * 1ll * i) % mod;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i] >> b[i], dp[-a[i] + gaam][-b[i] + gaam] += 1;
  for (ll i = -gaam; i <= gaam; i++)
    for (ll j = -gaam; j <= gaam; j++)
      //		if (dp[i + gaam][j + gaam] == 0)
      dp[i + gaam][j + gaam] =
          (dp[i + gaam][j + gaam] +
           ((i + gaam) ? dp[i - 1 + gaam][j + gaam] : 0ll) +
           ((j + gaam) ? dp[i + gaam][j - 1 + gaam] : 0ll)) %
          mod;
  //			cout << i << " " << j << "   " << dp[i + gaam][j + gaam] <<
  //endl;
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans = (ans + dp[a[i] + gaam][b[i] + gaam]) % mod,
    ans = (((ans - sel(a[i] * 2 + b[i] * 2, a[i] * 2)) % mod) + mod) % mod;
  ans = (ans * pw(2, mod - 2)) % mod;
  cout << ans << endl;
  return 0;
}
Problem p04051 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
typedef long long ll;
const int N = 2020, M = 4040, P = 1000000007, ny2 = (P + 1) / 2;
int i, j, k, n, m, ch, ff, ans;
int a[200200], b[200200];
int s[M][M];
int C[M][M];
void R(int &x) {
  ff = x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      ff = 1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  if (ff)
    x = -x;
}
int main() {
  freopen("01-03.txt", "r", stdin);
  R(n);
  for (i = 1; i <= n; i++) {
    R(a[i]);
    R(b[i]);
    s[N - a[i]][N - b[i]]++;
  }
  for (i = 1; i < M; i++)
    C[i][0] = C[0][i] = 1;
  for (i = 1; i < M; i++)
    for (j = 1; j < M; j++) {
      s[i][j] = (s[i - 1][j] + s[i][j - 1] + s[i][j]) % P;
      C[i][j] = (C[i - 1][j] + C[i][j - 1]) % P;
    }
  for (i = 1; i <= n; i++)
    ans = (ans + s[N + a[i]][N + b[i]]) % P;
  for (i = 1; i <= n; i++)
    ans = (ans + P - C[a[i] + a[i]][b[i] + b[i]]) % P;
  ans = (ll)ans * ny2 % P;
  printf("%d\n", ans);
}

Fixed submission:
#include <cstdio>
using namespace std;
typedef long long ll;
const int N = 2020, M = 4040, P = 1000000007, ny2 = (P + 1) / 2;
int i, j, k, n, m, ch, ff, ans;
int a[200200], b[200200];
int s[M][M];
int C[M][M];
void R(int &x) {
  ff = x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      ff = 1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  if (ff)
    x = -x;
}
int main() {
  R(n);
  for (i = 1; i <= n; i++) {
    R(a[i]);
    R(b[i]);
    s[N - a[i]][N - b[i]]++;
  }
  for (i = 1; i < M; i++)
    C[i][0] = C[0][i] = 1;
  for (i = 1; i < M; i++)
    for (j = 1; j < M; j++) {
      s[i][j] = (s[i - 1][j] + s[i][j - 1] + s[i][j]) % P;
      C[i][j] = (C[i - 1][j] + C[i][j - 1]) % P;
    }
  for (i = 1; i <= n; i++)
    ans = (ans + s[N + a[i]][N + b[i]]) % P;
  for (i = 1; i <= n; i++)
    ans = (ans + P - C[a[i] + a[i]][b[i] + b[i]]) % P;
  ans = (ll)ans * ny2 % P;
  printf("%d\n", ans);
}
Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define ll long long
#define db double
#define vi vector<int>
#define pb push_back
#define mod 1000000007
int c[4005][4005];
int a[4005];
const int mxs = 4005;
void read(int &a) {
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch = getchar();
  while (ch >= '0' && ch <= '9')
    a = a * 10 + ch - '0', ch = getchar();
}
int main() {
  for (int i = 0; i < 4005; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i)
        c[i][j] = 1;
      else {
        c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
      }
    }
  }
  int test = 0;
  int n;
  if (test)
    n = 200000;
  else
    read(n);
  const int pls = 2001;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int x, b;
    if (test)
      b = 1900 + rand() % 100, x = 1900 + rand() % 100;
    else
      read(x), read(b);
    x += b;
    double nans = 0;
    for (int j = pls - b; j <= min(mxs - 1, x + pls - b); j++) {
      ll q = 1ll * a[j] * c[x][b + j - pls];
      nans = (nans + q), ans = (ans + q);
    }
    ans -= (ll)(nans / mod) * mod;
    ans %= mod;
    if (ans < 0)
      ans += mod;
    //	cout << ans << ' ' << nans << endl;
    for (int j = b + pls - x; j <= b + pls; j++) {
      a[j] += c[x][b + pls - j];
      a[j] = (a[j] >= mod ? a[j] - mod : a[j]);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define ll long long
#define db double
#define vi vector<int>
#define pb push_back
#define mod 1000000007
int c[4005][4005];
int a[4005];
const int mxs = 4005;
void read(int &a) {
  a = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0')
    ch = getchar();
  while (ch >= '0' && ch <= '9')
    a = a * 10 + ch - '0', ch = getchar();
}
int main() {
  for (int i = 0; i < 4005; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i)
        c[i][j] = 1;
      else {
        c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
      }
    }
  }
  int test = 0;
  int n;
  if (test)
    n = 200000;
  else
    read(n);
  const int pls = 2001;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int x, b;
    if (test)
      b = 1900 + rand() % 100, x = 1900 + rand() % 100;
    else
      read(x), read(b);
    x += b;
    double nans = 0;
    for (int j = pls - b; j <= min(mxs - 1, x + pls - b); j++) {
      ll q = 1ll * a[j] * c[x][b + j - pls];
      nans = (nans + q), ans = (ans + q);
    }
    ans -= (ll)(nans / mod) * mod;
    ans %= mod;
    if (ans < 0)
      ans += mod;
    //	cout << ans << ' ' << nans << endl;
    for (int j = b + pls - x; j <= b + pls; j++) {
      a[j] += c[x][b + pls - j];
      a[j] = (a[j] >= mod ? a[j] - mod : a[j]);
    }
  }
  cout << ans << endl;
  return 0;
}