Problem p04051 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#define MOD 1000000007
#define inf 0x3f3f3f3f3f3f3f3f
#define pi acos(-1.0)
#define ri register int
#define Abs(x) ((x) < 0 ? (-(x)) : (x))
#define int long long
const int maxn = 4e3 + 20, delta = 2005;
inline int Max(int a, int b) { return a > b ? a : b; }
inline int Min(int a, int b) { return a > b ? b : a; }
namespace FastIO {
inline int read() {
  int f = 1, r = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') {
      f = -1;
    }
    c = getchar();
  }
  while (isdigit(c)) {
    r = 10 * r + c - '0';
    c = getchar();
  }
  return f * r;
}
inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
inline void writesp(int x) { write(x), putchar(' '); }
inline void writeln(int x) { write(x), puts(""); }
}; // namespace FastIO
using namespace FastIO;
int fac[maxn * 2], N, arr[200000 + 10][2], dp[maxn * 2][maxn * 2],
    invv[maxn * 2];
inline int exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  int d = exgcd(b, a % b, x, y);
  int tmp = y;
  y = x - a / b * y;
  x = tmp;
  return d;
}
inline int inv(int a, int b) {
  int x, y;
  exgcd(a, b, x, y);
  return (x + MOD) % MOD;
}
signed main() {
  // freopen("C:\Users\hjw\Downloads\testdata (4).in", "r", stdin);
  N = read();
  for (ri i = 1; i <= N; ++i) {
    arr[i][0] = read(), arr[i][1] = read(),
    dp[-arr[i][0] + delta][-arr[i][1] + delta]++;
  }
  for (ri i = -2000 + delta; i <= 2000 + delta; ++i) {
    for (ri j = -2000 + delta; j <= 2000 + delta; ++j) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j] + dp[i][j - 1]) % MOD;
    }
  }
  fac[0] = 1;
  for (ri i = 1; i < maxn * 2; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  for (ri i = 0; i < maxn * 2; ++i) {
    invv[i] = inv(fac[i], MOD);
  }
  int ans = 0;
  for (ri i = 1; i <= N; ++i) {
    int down = 2 * (arr[i][0] + arr[i][1]), up = 2 * arr[i][0];
    int tmp = fac[down] * invv[down - up] % MOD * invv[up] % MOD;
    ans = (ans + tmp) % MOD;
  }
  int sum = 0;
  for (ri i = 1; i <= N; ++i) {
    sum = (sum + dp[arr[i][0] + delta][arr[i][1] + delta]) % MOD;
  }
  sum = (sum - ans + MOD) % MOD;
  sum = (sum * inv(2, MOD)) % MOD;
  writeln(sum);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define MOD 1000000007
#define inf 0x3f3f3f3f3f3f3f3f
#define pi acos(-1.0)
#define ri register int
#define Abs(x) ((x) < 0 ? (-(x)) : (x))
#define int long long
const int maxn = 4e3 + 20, delta = 2005;
inline int Max(int a, int b) { return a > b ? a : b; }
inline int Min(int a, int b) { return a > b ? b : a; }
namespace FastIO {
inline int read() {
  int f = 1, r = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') {
      f = -1;
    }
    c = getchar();
  }
  while (isdigit(c)) {
    r = 10 * r + c - '0';
    c = getchar();
  }
  return f * r;
}
inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
inline void writesp(int x) { write(x), putchar(' '); }
inline void writeln(int x) { write(x), puts(""); }
}; // namespace FastIO
using namespace FastIO;
int fac[maxn * 2], N, arr[200000 + 10][2], dp[maxn][maxn], invv[maxn * 2];
inline int exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  int d = exgcd(b, a % b, x, y);
  int tmp = y;
  y = x - a / b * y;
  x = tmp;
  return d;
}
inline int inv(int a, int b) {
  int x, y;
  exgcd(a, b, x, y);
  return (x + MOD) % MOD;
}
signed main() {
  // freopen("C:\Users\hjw\Downloads\testdata (4).in", "r", stdin);
  N = read();
  for (ri i = 1; i <= N; ++i) {
    arr[i][0] = read(), arr[i][1] = read(),
    dp[-arr[i][0] + delta][-arr[i][1] + delta]++;
  }
  for (ri i = -2000 + delta; i <= 2000 + delta; ++i) {
    for (ri j = -2000 + delta; j <= 2000 + delta; ++j) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j] + dp[i][j - 1]) % MOD;
    }
  }
  fac[0] = 1;
  for (ri i = 1; i < maxn * 2; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  for (ri i = 0; i < maxn * 2; ++i) {
    invv[i] = inv(fac[i], MOD);
  }
  int ans = 0;
  for (ri i = 1; i <= N; ++i) {
    int down = 2 * (arr[i][0] + arr[i][1]), up = 2 * arr[i][0];
    int tmp = fac[down] * invv[down - up] % MOD * invv[up] % MOD;
    ans = (ans + tmp) % MOD;
  }
  int sum = 0;
  for (ri i = 1; i <= N; ++i) {
    sum = (sum + dp[arr[i][0] + delta][arr[i][1] + delta]) % MOD;
  }
  sum = (sum - ans + MOD) % MOD;
  sum = (sum * inv(2, MOD)) % MOD;
  writeln(sum);
  return 0;
}