Problem p03184 (C++)

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

using namespace std;
typedef long long LL;

#define MAXN 200001
#define MOD 1000000007

int H, W, N;
pair<int, int> bl[2001];
int dp[2001];

// x must be relatively prime to p
int fact[MAXN], inv[MAXN], ifact[MAXN];

// x must be relatively prime to p

template <typename T, typename T1> T mod(T x, T1 p) {
  x %= p;
  if (x < 0)
    x += p;
  return x;
}

// x must be relatively prime to p

template <typename T> T inverse(T x, T p) {
  x = mod(x, p);
  if (x == 1)
    return x;
  return mod((1LL * (-p / x) * (inv[p % x] % p)), p);
  // Since inverse of p % x is already calculated.
}

int NcR(int n, int r) {
  int ret = (1LL * ifact[n - r] * ifact[r]) % MOD;
  ret = (1LL * ret * fact[n]) % MOD;
  return ret;
}

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = (1LL * fact[i - 1] * i) % MOD;
  }

  ifact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    inv[i] = inverse(i, MOD);
    ifact[i] = (1LL * ifact[i - 1] * inv[i]) % MOD;
  }

  cin >> H >> W >> N;
  for (int i = 0; i < N; i++)
    cin >> bl[i].first >> bl[i].second;

  bl[N].first = H;
  bl[N].second = W;
  N++;

  sort(bl, bl + N);
  for (int i = 0; i < N; i++) {
    int x = bl[i].first;
    int y = bl[i].second;

    dp[i] = NcR(x + y - 2, x - 1);

    for (int j = 0; j < i; j++) {
      if (bl[j].second <= y) {
        int dx = x - bl[j].first;
        int dy = y - bl[j].second;

        dp[i] -= (1LL * dp[j] * NcR(dx + dy, dx)) % MOD;
        if (dp[i] < 0)
          dp[i] += MOD;
      }
    }
  }

  cout << dp[N - 1] << endl;
  return 0;
}

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

using namespace std;
typedef long long LL;

#define MAXN 200001
#define MOD 1000000007

int H, W, N;
pair<int, int> bl[3001];
int dp[3001];

// x must be relatively prime to p
int fact[MAXN], inv[MAXN], ifact[MAXN];

// x must be relatively prime to p

template <typename T, typename T1> T mod(T x, T1 p) {
  x %= p;
  if (x < 0)
    x += p;
  return x;
}

// x must be relatively prime to p

template <typename T> T inverse(T x, T p) {
  x = mod(x, p);
  if (x == 1)
    return x;
  return mod((1LL * (-p / x) * (inv[p % x] % p)), p);
  // Since inverse of p % x is already calculated.
}

int NcR(int n, int r) {
  int ret = (1LL * ifact[n - r] * ifact[r]) % MOD;
  ret = (1LL * ret * fact[n]) % MOD;
  return ret;
}

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

  fact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = (1LL * fact[i - 1] * i) % MOD;
  }

  ifact[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    inv[i] = inverse(i, MOD);
    ifact[i] = (1LL * ifact[i - 1] * inv[i]) % MOD;
  }

  cin >> H >> W >> N;
  for (int i = 0; i < N; i++)
    cin >> bl[i].first >> bl[i].second;

  bl[N].first = H;
  bl[N].second = W;
  N++;

  sort(bl, bl + N);
  for (int i = 0; i < N; i++) {
    int x = bl[i].first;
    int y = bl[i].second;

    dp[i] = NcR(x + y - 2, x - 1);

    for (int j = 0; j < i; j++) {
      if (bl[j].second <= y) {
        int dx = x - bl[j].first;
        int dy = y - bl[j].second;

        dp[i] -= (1LL * dp[j] * NcR(dx + dy, dx)) % MOD;
        if (dp[i] < 0)
          dp[i] += MOD;
      }
    }
  }

  cout << dp[N - 1] << endl;
  return 0;
}