Problem p04051 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int a[200005], b[200005];
int dp[5005][5005];
int f[100005], inv[100005];
const int mod = 1e9 + 7;
long long qpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int main() {
#ifndef ONLINE_JUDGE
  file("at1983");
#endif
  n = read();
  F(i, 1, n) {
    a[i] = read(), b[i] = read();
    dp[2000 - a[i]][2000 - b[i]]++;
  }
  F(i, 0, 4000) {
    F(j, 0, 4000) {
      dp[i][j] = ((j == 0 ? 0 : dp[i][j - 1]) + (i == 0 ? 0 : dp[i - 1][j]) +
                  dp[i][j]) %
                 mod;
    }
  }
  int ans = 0;
  F(i, 1, n) { ans = (ans + dp[a[i] + 2000][b[i] + 2000]) % mod; }
  f[0] = 1;
  F(i, 1, 30000) f[i] = f[i - 1] * 1ll * i % mod;
  inv[30000] = qpow(f[30000], mod - 2);
  f(i, 30000 - 1, 0) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  //   F(i,1,30000)cout<<f[i]<<" "<<inv[i]<<endl;
  F(i, 1, n) {
    ans = (ans -
           f[a[i] + a[i] + b[i] + b[i]] * 1ll * inv[b[i] + b[i]] % mod *
               inv[a[i] + a[i]] % mod +
           mod) %
          mod;
  }
  ans = (1ll * ans * qpow(2, mod - 2)) % mod;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n;
int a[200005], b[200005];
int dp[5005][5005];
int f[100005], inv[100005];
const int mod = 1e9 + 7;
long long qpow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int main() {
  n = read();
  F(i, 1, n) {
    a[i] = read(), b[i] = read();
    dp[2000 - a[i]][2000 - b[i]]++;
  }
  F(i, 0, 4000) {
    F(j, 0, 4000) {
      dp[i][j] = ((j == 0 ? 0 : dp[i][j - 1]) + (i == 0 ? 0 : dp[i - 1][j]) +
                  dp[i][j]) %
                 mod;
    }
  }
  int ans = 0;
  F(i, 1, n) { ans = (ans + dp[a[i] + 2000][b[i] + 2000]) % mod; }
  f[0] = 1;
  F(i, 1, 30000) f[i] = f[i - 1] * 1ll * i % mod;
  inv[30000] = qpow(f[30000], mod - 2);
  f(i, 30000 - 1, 0) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  //   F(i,1,30000)cout<<f[i]<<" "<<inv[i]<<endl;
  F(i, 1, n) {
    ans = (ans -
           f[a[i] + a[i] + b[i] + b[i]] * 1ll * inv[b[i] + b[i]] % mod *
               inv[a[i] + a[i]] % mod +
           mod) %
          mod;
  }
  ans = (1ll * ans * qpow(2, mod - 2)) % mod;
  printf("%d\n", ans);
  return 0;
}