Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 100010;
const int mod = 1e9 + 7;

inline int Mod(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x = Mod(x + y); }

inline int ksm(int x, int k = mod - 2) {
  int s = 1;
  while (k) {
    if (k & 1)
      s = 1LL * s * x % mod;
    x = 1LL * x * x % mod, k >>= 1;
  }
  return s;
}

int fac[8010];
int ifac[8010];
int f[4010][4010];
int X[MAXN];
int Y[MAXN];

int main() {
#ifdef LOCAL
  FILE("");
#endif
  int n = ri;
  int res = 0, t = 2001;

  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= 8000; i++)
    ifac[i] = ksm(fac[i] = 1LL * fac[i - 1] * i % mod);

  for (register int i = 1; i <= n; i++) {
    int x = X[i] = ri, y = Y[i] = ri, s = x + y;
    res = (res + 1LL * fac[s << 1] * ifac[y << 1] % mod * ifac[x << 1]) % mod;
    f[t - x][t - y]++;
  }

  for (register int i = 1; i <= 4001; i++)
    for (register int j = 1; j <= 4001; j++)
      f[i][j] = Mod(Mod(f[i][j] + f[i - 1][j]) + f[i][j - 1]);

  res = Mod(mod - res);
  for (register int i = 1; i <= n; i++)
    Add(res, f[t + X[i]][t + Y[i]]);
  res = 500000004LL * res % mod;
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 200010;
const int mod = 1e9 + 7;

inline int Mod(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x = Mod(x + y); }

inline int ksm(int x, int k = mod - 2) {
  int s = 1;
  while (k) {
    if (k & 1)
      s = 1LL * s * x % mod;
    x = 1LL * x * x % mod, k >>= 1;
  }
  return s;
}

int fac[8010];
int ifac[8010];
int f[4010][4010];
int X[MAXN];
int Y[MAXN];

int main() {
#ifdef LOCAL
  FILE("");
#endif
  int n = ri;
  int res = 0, t = 2001;

  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= 8000; i++)
    ifac[i] = ksm(fac[i] = 1LL * fac[i - 1] * i % mod);

  for (register int i = 1; i <= n; i++) {
    int x = X[i] = ri, y = Y[i] = ri, s = x + y;
    res = (res + 1LL * fac[s << 1] * ifac[y << 1] % mod * ifac[x << 1]) % mod;
    f[t - x][t - y]++;
  }

  for (register int i = 1; i <= 4001; i++)
    for (register int j = 1; j <= 4001; j++)
      f[i][j] = Mod(Mod(f[i][j] + f[i - 1][j]) + f[i][j - 1]);

  res = Mod(mod - res);
  for (register int i = 1; i <= n; i++)
    Add(res, f[t + X[i]][t + Y[i]]);
  res = 500000004LL * res % mod;
  cout << res << endl;

  return 0;
}