Problem p04051 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(x, a, b) for (int x = (a); x <= (b); ++x)
#define rop(x, a, b) for (int x = (a); x < (b); ++x)
#define per(x, a, b) for (int x = (a); x >= (b); --x)
using namespace std;
typedef long long LL;
typedef double DB;
int mod = 1000000007;
inline int MOD(int x) { return x >= mod ? x - mod : x; }
int qpow(int a, int b) {
  int as = 1;
  while (b) {
    if (b & 1)
      as = 1ll * as * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return as;
}
int a[200005], b[200005], CC[4005][4005];
int n, m;
int F[10005], fc[10005], iv[10005], G[10005];
int &f(int x) { return F[x + 3000]; }
int &g(int x) { return G[x + 3000]; }
int as;
int C(int n, int m) { return CC[n][m]; }
int CCC(int n, int m) { return 1ll * fc[n] * iv[m] % mod * iv[n - m] % mod; }
void init() {
  CC[0][0] = 1;
  rep(i, 1, 4000) {
    CC[i][0] = CC[i][i] = 1;
    rop(j, 1, i) { CC[i][j] = MOD(CC[i - 1][j - 1] + CC[i - 1][j]); }
  }
  fc[0] = iv[0] = 1;
  rep(i, 1, 10000) fc[i] = 1ll * fc[i - 1] * i % mod;
  iv[10000] = qpow(fc[10000], mod - 2);
  per(i, 9999, 1) iv[i] = 1ll * iv[i + 1] * (i + 1) % mod;
}
int main() {
  init();
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d%d", &a[i], &b[i]);
  rep(i, 1, n) {
    rep(j, -b[i], a[i]) { f(j) = MOD(f(j) + C(a[i] + b[i], a[i] - j)); }
    rep(j, -a[i], b[i]) { g(j) = MOD(g(j) + C(a[i] + b[i], a[i] + j)); }
    as = MOD(as - CCC(a[i] + a[i] + b[i] + b[i], a[i] + a[i]) + mod);
  }
  rep(j, -2000, 2000) as = MOD(as + 1ll * f(j) * g(j) % mod);
  cout << 1ll * as * qpow(2, mod - 2) % mod << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(x, a, b) for (int x = (a); x <= (b); ++x)
#define rop(x, a, b) for (int x = (a); x < (b); ++x)
#define per(x, a, b) for (int x = (a); x >= (b); --x)
using namespace std;
typedef long long LL;
typedef double DB;
int mod = 1000000007;
inline int MOD(int x) { return x >= mod ? x - mod : x; }
int qpow(int a, int b) {
  int as = 1;
  while (b) {
    if (b & 1)
      as = 1ll * as * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return as;
}
int a[200005], b[200005], CC[4005][4005];
int n, m;
int F[10005], fc[10005], iv[10005], G[10005];
int &f(int x) { return F[x + 3000]; }
int &g(int x) { return G[x + 3000]; }
int as;
int C(int n, int m) { return CC[n][m]; }
int CCC(int n, int m) { return 1ll * fc[n] * iv[m] % mod * iv[n - m] % mod; }
void init() {
  CC[0][0] = 1;
  rep(i, 1, 4000) {
    CC[i][0] = CC[i][i] = 1;
    rop(j, 1, i) { CC[i][j] = MOD(CC[i - 1][j - 1] + CC[i - 1][j]); }
  }
  fc[0] = iv[0] = 1;
  rep(i, 1, 10000) fc[i] = 1ll * fc[i - 1] * i % mod;
  iv[10000] = qpow(fc[10000], mod - 2);
  per(i, 9999, 1) iv[i] = 1ll * iv[i + 1] * (i + 1) % mod;
}
int main() {
  init();
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d%d", &a[i], &b[i]);
  rep(i, 1, n) {
    rep(j, -b[i], a[i]) { f(j) = MOD(f(j) + C(a[i] + b[i], a[i] - j)); }
    rep(j, -a[i], b[i]) { g(j) = MOD(g(j) + C(a[i] + b[i], a[i] + j)); }
    as = MOD(as - CCC(a[i] + a[i] + b[i] + b[i], a[i] + a[i]) + mod);
  }
  rep(j, -2000, 2000) as = MOD(as + 1ll * f(j) * g(j) % mod);
  cout << 1ll * as * qpow(2, mod - 2) % mod << endl;
  return 0;
}