Problem p04051 (C++)

Buggy submission (status: Runtime Error):
// <BBQ.cpp> - Sat Mar 25 11:43:04 2017
// This file is created by XuYike's black technology automatically.
// Copyright (C) 2015 ChangJun High School, Inc.
// I don't know what this program is.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long lol;
#define next NEXT
template <typename T> inline void gg(T &res) {
  res = 0;
  T fh = 1;
  char ch = getchar();
  while ((ch > '9' || ch < '0') && ch != '-')
    ch = getchar();
  if (ch == '-')
    fh = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    res = res * 10 + ch - '0', ch = getchar();
  res *= fh;
}
inline int gi() {
  int x;
  gg(x);
  return x;
}
inline lol gl() {
  lol x;
  gg(x);
  return x;
}
const int MAXN = 100010;
const int K = 2001;
const int N = 4002;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int qpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % MOD;
    x = 1ll * x * x % MOD;
    y >>= 1;
  }
  return res;
}
int jc[N << 1], nj[N << 1], f[N][N];
inline int C(int n, int m) {
  return 1ll * jc[n] * nj[m] % MOD * nj[n - m] % MOD;
}
int a[MAXN], b[MAXN];
int main() {
  jc[0] = 1;
  for (int i = 1; i <= 8000; i++)
    jc[i] = 1ll * jc[i - 1] * i % MOD;
  nj[8000] = qpow(jc[8000], MOD - 2);
  for (int i = 8000; i; i--)
    nj[i - 1] = 1ll * nj[i] * i % MOD;
  int n = gi(), ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = gi(), b[i] = gi();
    f[K - a[i]][K - b[i]]++;
    ans = (ans - C(a[i] + b[i] << 1, a[i] << 1) + MOD) % MOD;
  }
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      f[i][j] = (0ll + f[i][j] + f[i - 1][j] + f[i][j - 1]) % MOD;
  for (int i = 1; i <= n; i++)
    ans = (ans + f[a[i] + K][b[i] + K]) % MOD;
  printf("%d", 1ll * ans * qpow(2, MOD - 2) % MOD);
  return 0;
}

Fixed submission:
// <BBQ.cpp> - Sat Mar 25 11:43:04 2017
// This file is created by XuYike's black technology automatically.
// Copyright (C) 2015 ChangJun High School, Inc.
// I don't know what this program is.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long lol;
#define next NEXT
template <typename T> inline void gg(T &res) {
  res = 0;
  T fh = 1;
  char ch = getchar();
  while ((ch > '9' || ch < '0') && ch != '-')
    ch = getchar();
  if (ch == '-')
    fh = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    res = res * 10 + ch - '0', ch = getchar();
  res *= fh;
}
inline int gi() {
  int x;
  gg(x);
  return x;
}
inline lol gl() {
  lol x;
  gg(x);
  return x;
}
const int MAXN = 200010;
const int K = 2001;
const int N = 4002;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int qpow(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = 1ll * res * x % MOD;
    x = 1ll * x * x % MOD;
    y >>= 1;
  }
  return res;
}
int jc[N << 1], nj[N << 1], f[N][N];
inline int C(int n, int m) {
  return 1ll * jc[n] * nj[m] % MOD * nj[n - m] % MOD;
}
int a[MAXN], b[MAXN];
int main() {
  jc[0] = 1;
  for (int i = 1; i <= 8000; i++)
    jc[i] = 1ll * jc[i - 1] * i % MOD;
  nj[8000] = qpow(jc[8000], MOD - 2);
  for (int i = 8000; i; i--)
    nj[i - 1] = 1ll * nj[i] * i % MOD;
  int n = gi(), ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = gi(), b[i] = gi();
    f[K - a[i]][K - b[i]]++;
    ans = (ans - C(a[i] + b[i] << 1, a[i] << 1) + MOD) % MOD;
  }
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      f[i][j] = (0ll + f[i][j] + f[i - 1][j] + f[i][j - 1]) % MOD;
  for (int i = 1; i <= n; i++)
    ans = (ans + f[a[i] + K][b[i] + K]) % MOD;
  printf("%d", 1ll * ans * qpow(2, MOD - 2) % MOD);
  return 0;
}