Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000LL
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n;
ll x[4005][4005];
ll modpow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll F[10005], R[10005];
void make() {
  F[0] = 1;
  for (int i = 1; i < 10005; i++)
    F[i] = F[i - 1] * i % mod;
  for (int i = 0; i < 10005; i++)
    R[i] = modpow(F[i], mod - 2);
}
ll ans, hoge;
int c[2005], d[2005];
int main() {
  cin >> n;
  make();
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    x[2000 - a][2000 - b]++;
    hoge = (hoge + F[2 * a + 2 * b] * R[2 * a] % mod * R[2 * b] % mod) % mod;
    c[i] = a;
    d[i] = b;
  }
  rep(i, 4005) rep(j, 4005) x[i][j] =
      (x[i][j] + (i ? x[i - 1][j] : 0) + (j ? x[i][j - 1] : 0)) % mod;
  rep(i, n) ans = (ans + x[2000 + c[i]][2000 + d[i]]) % mod;
  cout << ((ans - hoge) % mod + mod) % mod * ((mod + 1) / 2) % mod << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000LL
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n;
ll x[4005][4005];
ll modpow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll F[10005], R[10005];
void make() {
  F[0] = 1;
  for (int i = 1; i < 10005; i++)
    F[i] = F[i - 1] * i % mod;
  for (int i = 0; i < 10005; i++)
    R[i] = modpow(F[i], mod - 2);
}
ll ans, hoge;
int c[200005], d[200005];
int main() {
  cin >> n;
  make();
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    x[2000 - a][2000 - b]++;
    hoge = (hoge + F[2 * a + 2 * b] * R[2 * a] % mod * R[2 * b] % mod) % mod;
    c[i] = a;
    d[i] = b;
  }
  rep(i, 4005) rep(j, 4005) x[i][j] =
      (x[i][j] + (i ? x[i - 1][j] : 0) + (j ? x[i][j - 1] : 0)) % mod;
  rep(i, n) ans = (ans + x[2000 + c[i]][2000 + d[i]]) % mod;
  cout << ((ans - hoge) % mod + mod) % mod * ((mod + 1) / 2) % mod << endl;
}