Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define i32 int
#define i64 long long
#define db double
#define rep(i, s, n) for (i32 i = s; i <= n; i++)
#define rep_(i, n, s) for (i32 i = n; i >= s; i--)
i32 RI() {
  char ch = getchar();
  i32 sum = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    sum = sum * 10 + ch - '0', ch = getchar();
  return f * sum;
}
i64 RL() {
  char ch = getchar();
  i64 sum = 0;
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    sum = sum * 10 + ch - '0', ch = getchar();
  return sum;
}
const i32 N = 5005;
const i32 mod = 1e9 + 7;
i32 n, ans;
i32 F[N][N], A[N], B[N], jc[N << 2];
i32 ksm(i32 x, i32 y) {
  i32 s = 1;
  while (y) {
    if (y & 1)
      s = 1LL * s * x % mod;
    y >>= 1;
    x = 1LL * x * x % mod;
  }
  return s;
}
i32 C(i32 x, i32 y) {
  return 1LL * jc[x] * ksm(1LL * jc[x - y] * jc[y] % mod, mod - 2) % mod;
}
i32 get(i32 x, i32 y) { return C(x + y, x); }
i32 main() {
  n = RI();
  rep(i, 1, n) {
    A[i] = RI();
    B[i] = RI();
    F[-A[i] + 2001][-B[i] + 2001]++;
  }
  jc[0] = 1;
  rep(i, 1, 10000) jc[i] = 1LL * jc[i - 1] * i % mod;
  rep(i, 1, 4500) {
    rep(j, 1, 4500) { F[i][j] = (F[i][j] + F[i - 1][j] + F[i][j - 1]) % mod; }
  }
  rep(i, 1, n) {
    ans = (ans + F[A[i] + 2001][B[i] + 2001]) % mod;
    ans = (ans - get(A[i] << 1, B[i] << 1) + mod) % mod;
  }
  ans = 1LL * ans * ksm(2, mod - 2) % mod;
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define i32 int
#define i64 long long
#define db double
#define rep(i, s, n) for (i32 i = s; i <= n; i++)
#define rep_(i, n, s) for (i32 i = n; i >= s; i--)
i32 RI() {
  char ch = getchar();
  i32 sum = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    sum = sum * 10 + ch - '0', ch = getchar();
  return f * sum;
}
i64 RL() {
  char ch = getchar();
  i64 sum = 0;
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    sum = sum * 10 + ch - '0', ch = getchar();
  return sum;
}
const i32 N = 5005;
const i32 mod = 1e9 + 7;
i32 n, ans;
i32 F[N][N], A[300000], B[300000], jc[N << 2];
i32 ksm(i32 x, i32 y) {
  i32 s = 1;
  while (y) {
    if (y & 1)
      s = 1LL * s * x % mod;
    y >>= 1;
    x = 1LL * x * x % mod;
  }
  return s;
}
i32 C(i32 x, i32 y) {
  return 1LL * jc[x] * ksm(1LL * jc[x - y] * jc[y] % mod, mod - 2) % mod;
}
i32 get(i32 x, i32 y) { return C(x + y, x); }
i32 main() {
  n = RI();
  rep(i, 1, n) {
    A[i] = RI();
    B[i] = RI();
    F[-A[i] + 2001][-B[i] + 2001]++;
  }
  jc[0] = 1;
  rep(i, 1, 10000) jc[i] = 1LL * jc[i - 1] * i % mod;
  rep(i, 1, 4500) {
    rep(j, 1, 4500) { F[i][j] = (F[i][j] + F[i - 1][j] + F[i][j - 1]) % mod; }
  }
  rep(i, 1, n) {
    ans = (ans + F[A[i] + 2001][B[i] + 2001]) % mod;
    ans = (ans - get(A[i] << 1, B[i] << 1) + mod) % mod;
  }
  ans = 1LL * ans * ksm(2, mod - 2) % mod;
  printf("%d", ans);
}