Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 1e5 + 10, Lim = 2e3 + 10;
const int mod = 1e9 + 7;
int dp[Lim << 1][Lim << 1], fac[Lim << 2], inv[Lim << 2];
struct node {
  int x, y;
} Point[N];
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
inline int fpm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod, b /= 2;
  }
  return res;
}
inline void init(int n) {
  fac[0] = 1;
  For(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = fpm(fac[n], mod - 2);
  Fordown(i, n, 1) inv[i - 1] = 1ll * inv[i] * i % mod;
}
inline int Mod(int val) {
  if (val >= mod)
    val -= mod;
  if (val < 0)
    val += mod;
  return val;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  int n = read();
  init((Lim - 5) << 2);
  int ans = 0;
  For(i, 1, n) {
    int x = read(), y = read();
    dp[Lim - x][Lim - y]++;
    ans = (ans + C((x << 1) + (y << 1), x << 1)) % mod;
    Point[i] = (node){x, y};
  }
  ans = Mod(-ans);
  int Max = (Lim << 1) - 5;
  For(i, 1, Max) For(j, 1, Max) dp[i][j] =
      Mod(dp[i][j] + dp[i - 1][j] + dp[i][j - 1]);
  For(i, 1, n) ans = Mod(ans + dp[Lim + Point[i].x][Lim + Point[i].y]);
  printf("%lld\n", 1ll * ans * fpm(2, mod - 2) % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 2e5 + 10, Lim = 2e3 + 10;
const int mod = 1e9 + 7;
int dp[Lim << 1][Lim << 1], fac[Lim << 2], inv[Lim << 2];
struct node {
  int x, y;
} Point[N];
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
inline int fpm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod, b /= 2;
  }
  return res;
}
inline void init(int n) {
  fac[0] = 1;
  For(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = fpm(fac[n], mod - 2);
  Fordown(i, n, 1) inv[i - 1] = 1ll * inv[i] * i % mod;
}
inline int Mod(int val) {
  if (val >= mod)
    val -= mod;
  if (val < 0)
    val += mod;
  return val;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  int n = read();
  init((Lim - 5) << 2);
  int ans = 0;
  For(i, 1, n) {
    int x = read(), y = read();
    dp[Lim - x][Lim - y]++;
    ans = (ans + C((x << 1) + (y << 1), x << 1)) % mod;
    Point[i] = (node){x, y};
  }
  ans = Mod(-ans);
  int Max = (Lim << 1) - 5;
  For(i, 1, Max) For(j, 1, Max) dp[i][j] =
      Mod(dp[i][j] + dp[i - 1][j] + dp[i][j - 1]);
  For(i, 1, n) ans = Mod(ans + dp[Lim + Point[i].x][Lim + Point[i].y]);
  printf("%lld\n", 1ll * ans * fpm(2, mod - 2) % mod);
  return 0;
}