Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef pair<int, int> pa;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar())
    v = v * 10 + ch - '0';
  if (fu)
    v = -v;
}
const int MO = 1e9 + 7;
LL ans, fac[4010], inv[4010];
int n, v[4010][4010], tag[4010][4010], a[200010], b[200010];
LL pw(LL x, LL y) {
  LL t = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      t = t * x % MO;
    x = x * x % MO;
  }
  return t;
}
LL C(int x, int y) { return fac[x] * inv[y] % MO * inv[x - y] % MO; }
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) {
    scanf("%d%d", &a[i], &b[i]);
    v[2000 - a[i]][2000 - b[i]]++;
    tag[2000 + a[i]][2000 + b[i]]++;
  }
  FOR(i, 0, 4000)
  FOR(j, 0, 4000) {
    (v[i + 1][j] += v[i][j]) %= MO;
    (v[i][j + 1] += v[i][j]) %= MO;
  }
  FOR(i, 0, 4000)
  FOR(j, 0, 4000)
  (ans += 1ll * v[i][j] * tag[i][j]) %= MO;
  fac[0] = 1;
  FOR(i, 1, 8000) fac[i] = fac[i - 1] * i % MO;
  inv[8000] = pw(fac[8000], MO - 2);
  FORD(i, 7999, 0) inv[i] = inv[i + 1] * (i + 1) % MO;
  FOR(i, 1, n)
  (ans -= C(a[i] + a[i] + b[i] + b[i], a[i] + a[i])) %= MO;
  ans = ans * pw(2, MO - 2) % MO;
  ans = (ans % MO + MO) % MO;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef pair<int, int> pa;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar())
    v = v * 10 + ch - '0';
  if (fu)
    v = -v;
}
const int MO = 1e9 + 7;
LL ans, fac[8010], inv[8010];
int n, v[4010][4010], tag[4010][4010], a[200010], b[200010];
LL pw(LL x, LL y) {
  LL t = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      t = t * x % MO;
    x = x * x % MO;
  }
  return t;
}
LL C(int x, int y) { return fac[x] * inv[y] % MO * inv[x - y] % MO; }
int main() {
  scanf("%d", &n);
  FOR(i, 1, n) {
    scanf("%d%d", &a[i], &b[i]);
    v[2000 - a[i]][2000 - b[i]]++;
    tag[2000 + a[i]][2000 + b[i]]++;
  }
  FOR(i, 0, 4000)
  FOR(j, 0, 4000) {
    (v[i + 1][j] += v[i][j]) %= MO;
    (v[i][j + 1] += v[i][j]) %= MO;
  }
  FOR(i, 0, 4000)
  FOR(j, 0, 4000)
  (ans += 1ll * v[i][j] * tag[i][j]) %= MO;
  fac[0] = 1;
  FOR(i, 1, 8000) fac[i] = fac[i - 1] * i % MO;
  inv[8000] = pw(fac[8000], MO - 2);
  FORD(i, 7999, 0) inv[i] = inv[i + 1] * (i + 1) % MO;
  FOR(i, 1, n)
  (ans -= C(a[i] + a[i] + b[i] + b[i], a[i] + a[i])) %= MO;
  ans = ans * pw(2, MO - 2) % MO;
  ans = (ans % MO + MO) % MO;
  cout << ans << endl;
  return 0;
}