Problem p04051 (C++)

Buggy submission (status: Runtime Error):
// quming
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
using namespace std;
const int P = 1e9 + 7;
inline void upd(R int &x, R int y) { (x += y) >= P ? x -= P : 0; }
inline int add(R int x, R int y) { return x + y >= P ? x + y - P : x + y; }
inline int dec(R int x, R int y) { return x - y < 0 ? x - y + P : x - y; }
inline int mul(R int x, R int y) { return 1ll * x * y - 1ll * x * y / P * P; }
int ksm(R int x, R int y) {
  R int res = 1;
  for (; y; y >>= 1, x = mul(x, x))
    (y & 1) ? res = mul(res, x) : 0;
  return res;
}
const int N = 1e5 + 5, M = 5005, S = 2005;
int a[N], b[N], fac[N], ifac[N], f[M][M], xmx, ymx, n, res;
inline int C(R int n, R int m) {
  return m > n ? 0 : 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;
}
void init(int n) {
  fac[0] = ifac[0] = 1;
  fp(i, 1, n) fac[i] = mul(fac[i - 1], i);
  ifac[n] = ksm(fac[n], P - 2);
  fd(i, n - 1, 1) ifac[i] = mul(ifac[i + 1], i + 1);
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  fp(i, 1, n) scanf("%d%d", &a[i], &b[i]), cmax(xmx, a[i]), cmax(ymx, b[i]);
  init((xmx + ymx) << 1);
  fp(i, 1, n)++ f[S - a[i]][S - b[i]];
  fp(i, S - xmx, S + xmx) fp(j, S - ymx, S + ymx) if (f[i][j])
      upd(f[i + 1][j], f[i][j]),
      upd(f[i][j + 1], f[i][j]);
  fp(i, 1, n) upd(res, f[S + a[i]][S + b[i]]);
  fp(i, 1, n) res = dec(res, C((a[i] + b[i]) << 1, a[i] << 1));
  printf("%d\n", mul(res, (P + 1) >> 1));
  return 0;
}

Fixed submission:
// quming
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
using namespace std;
const int P = 1e9 + 7;
inline void upd(R int &x, R int y) { (x += y) >= P ? x -= P : 0; }
inline int add(R int x, R int y) { return x + y >= P ? x + y - P : x + y; }
inline int dec(R int x, R int y) { return x - y < 0 ? x - y + P : x - y; }
inline int mul(R int x, R int y) { return 1ll * x * y - 1ll * x * y / P * P; }
int ksm(R int x, R int y) {
  R int res = 1;
  for (; y; y >>= 1, x = mul(x, x))
    (y & 1) ? res = mul(res, x) : 0;
  return res;
}
const int N = 2e5 + 5, M = 5005, S = 2005;
int a[N], b[N], fac[N], ifac[N], f[M][M], xmx, ymx, n, res;
inline int C(R int n, R int m) {
  return m > n ? 0 : 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;
}
void init(int n) {
  fac[0] = ifac[0] = 1;
  fp(i, 1, n) fac[i] = mul(fac[i - 1], i);
  ifac[n] = ksm(fac[n], P - 2);
  fd(i, n - 1, 1) ifac[i] = mul(ifac[i + 1], i + 1);
}
int main() {
  //	freopen("testdata.in","r",stdin);
  scanf("%d", &n);
  fp(i, 1, n) scanf("%d%d", &a[i], &b[i]), cmax(xmx, a[i]), cmax(ymx, b[i]);
  init((xmx + ymx) << 1);
  fp(i, 1, n)++ f[S - a[i]][S - b[i]];
  fp(i, S - xmx, S + xmx) fp(j, S - ymx, S + ymx) if (f[i][j])
      upd(f[i + 1][j], f[i][j]),
      upd(f[i][j + 1], f[i][j]);
  fp(i, 1, n) upd(res, f[S + a[i]][S + b[i]]);
  fp(i, 1, n) res = dec(res, C((a[i] + b[i]) << 1, a[i] << 1));
  printf("%d\n", mul(res, (P + 1) >> 1));
  return 0;
}