Problem p04051 (C++)

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

inline int read() {
  int a = 0;
  bool f = 0;
  char c = getchar();
  while (c != EOF && !isdigit(c)) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while (c != EOF && isdigit(c)) {
    a = (a << 3) + (a << 1) + (c ^ '0');
    c = getchar();
  }
  return f ? -a : a;
}

const int MOD = 1e9 + 7, MAXN = 4000;
int f[MAXN + 10 << 1][MAXN + 10 << 1],
    jc[MAXN + 10 << 1] = {1, 1}, ny[MAXN + 10 << 1] = {1}, a[MAXN], b[MAXN];

inline long long poww(long long a, int b) {
  int times = 1;
  while (b) {
    if (b & 1)
      times = times * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return times;
}

int main() {
  for (long long i = 2; i <= MAXN << 1; i++)
    jc[i] = jc[i - 1] * i % MOD;
  ny[MAXN << 1] = poww(jc[MAXN << 1], MOD - 2);
  for (long long i = (MAXN << 1) - 1; i; i--)
    ny[i] = ny[i + 1] * (i + 1) % MOD;
  int N = read(), ans = 0;
  for (int i = 1; i <= N; i++) {
    a[i] = read();
    b[i] = read();
    f[-a[i] + 2000][-b[i] + 2000]++;
    ans = (ans + MOD -
           jc[a[i] + b[i] << 1] * (long long)ny[a[i] << 1] % MOD *
               ny[b[i] << 1] % MOD) %
          MOD;
  }
  for (int i = 1; i <= MAXN; i++) {
    f[i][0] += f[i - 1][0];
    f[0][i] += f[0][i - 1];
  }
  for (int i = 1; i <= MAXN; i++)
    for (int j = 1; j <= MAXN; j++)
      f[i][j] = ((f[i][j] + f[i - 1][j]) % MOD + f[i][j - 1]) % MOD;
  for (int i = 1; i <= N; i++)
    ans = (ans + f[a[i] + 2000][b[i] + 2000]) % MOD;
  cout << ans * poww(2, MOD - 2) % MOD;
  return 0;
}


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

inline int read() {
  int a = 0;
  bool f = 0;
  char c = getchar();
  while (c != EOF && !isdigit(c)) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while (c != EOF && isdigit(c)) {
    a = (a << 3) + (a << 1) + (c ^ '0');
    c = getchar();
  }
  return f ? -a : a;
}

const int MOD = 1e9 + 7, MAXN = 4000, MAXM = 200000;
int f[MAXN + 10][MAXN + 10],
    jc[MAXN + 10 << 1] = {1, 1}, ny[MAXN + 10 << 1] = {1}, a[MAXM + 10],
                    b[MAXM + 10];

inline long long poww(long long a, int b) {
  int times = 1;
  while (b) {
    if (b & 1)
      times = times * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return times;
}

int main() {
  for (long long i = 2; i <= MAXN << 1; i++)
    jc[i] = jc[i - 1] * i % MOD;
  ny[MAXN << 1] = poww(jc[MAXN << 1], MOD - 2);
  for (long long i = (MAXN << 1) - 1; i; i--)
    ny[i] = ny[i + 1] * (i + 1) % MOD;
  int N = read(), ans = 0;
  for (int i = 1; i <= N; i++) {
    a[i] = read();
    b[i] = read();
    f[-a[i] + 2000][-b[i] + 2000]++;
    ans = (ans + MOD -
           jc[a[i] + b[i] << 1] * (long long)ny[a[i] << 1] % MOD *
               ny[b[i] << 1] % MOD) %
          MOD;
  }
  for (int i = 1; i <= MAXN; i++) {
    f[i][0] += f[i - 1][0];
    f[0][i] += f[0][i - 1];
  }
  for (int i = 1; i <= MAXN; i++)
    for (int j = 1; j <= MAXN; j++)
      f[i][j] = ((f[i][j] + f[i - 1][j]) % MOD + f[i][j - 1]) % MOD;
  for (int i = 1; i <= N; i++)
    ans = (ans + f[a[i] + 2000][b[i] + 2000]) % MOD;
  cout << ans * poww(2, MOD - 2) % MOD;
  return 0;
}