Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair

int read() {
  int v = 0, f = 1;
  char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (48 <= c && c <= 57)
    v = (v << 3) + v + v + c - 48, c = getchar();
  return v * f;
}

const ll MOD = 1e9 + 7;
const int N = 101000;

ll a[N], b[N], fac[N], inv[N];
int f[4010][4010], n;

ll C(int u, int v) { return fac[u + v] * inv[u] % MOD * inv[v] % MOD; }

int main() {
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= 100000; i++)
    inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  for (int i = 1; i <= 100000; i++)
    inv[i] = inv[i - 1] * inv[i] % MOD;
  fac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    fac[i] = fac[i - 1] * i % MOD;
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    f[2000 - a[i]][2000 - b[i]]++;
  }
  for (int i = 0; i <= 4000; i++)
    for (int j = 0; j <= 4000; j++) {
      (f[i + 1][j] += f[i][j]) %= MOD;
      (f[i][j + 1] += f[i][j]) %= MOD;
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += f[2000 + a[i]][2000 + b[i]]) %= MOD;
  for (int i = 1; i <= n; i++)
    ans = (ans - C(a[i] + a[i], b[i] + b[i]) + MOD) % MOD;
  ans = ans * inv[2] % MOD;
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair

int read() {
  int v = 0, f = 1;
  char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (48 <= c && c <= 57)
    v = (v << 3) + v + v + c - 48, c = getchar();
  return v * f;
}

const ll MOD = 1e9 + 7;
const int N = 201000;

ll a[N], b[N], fac[N], inv[N];
int f[4010][4010], n;

ll C(int u, int v) { return fac[u + v] * inv[u] % MOD * inv[v] % MOD; }

int main() {
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= 100000; i++)
    inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  for (int i = 1; i <= 100000; i++)
    inv[i] = inv[i - 1] * inv[i] % MOD;
  fac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    fac[i] = fac[i - 1] * i % MOD;
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    f[2000 - a[i]][2000 - b[i]]++;
  }
  for (int i = 0; i <= 4000; i++)
    for (int j = 0; j <= 4000; j++) {
      (f[i + 1][j] += f[i][j]) %= MOD;
      (f[i][j + 1] += f[i][j]) %= MOD;
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    (ans += f[2000 + a[i]][2000 + b[i]]) %= MOD;
  for (int i = 1; i <= n; i++)
    ans = (ans - C(a[i] + a[i], b[i] + b[i]) + MOD) % MOD;
  ans = ans * inv[2] % MOD;
  printf("%lld\n", ans);
}