Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = (1e9) + 7;
const int maxn = 2010;
const int maxm = (2e5) + 10;
int n, h, w, x[maxn], y[maxn];
ll dp[maxn];
struct node {
  int x, y;
} D[maxn];
ll iv[maxm], ivjc[maxm], jc[maxm];
bool cmp(node a, node b) {
  if (a.x == b.x)
    return a.y < b.y;
  return a.x < b.x;
}
ll C(int x, int y) {
  x += y;
  return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod;
}
void update(ll &x, ll y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  // freopen("1.txt","r",stdin);
  jc[0] = iv[0] = ivjc[0] = 1;
  jc[1] = iv[1] = ivjc[1] = 1;
  for (int i = 2; i < maxm; i++) {
    jc[i] = jc[i - 1] * i % mod;
    iv[i] = (mod - mod / i) * iv[mod % i] % mod;
    ivjc[i] = ivjc[i - 1] * iv[i] % mod;
  }

  read(h);
  read(w);
  read(n);
  for (int i = 1; i <= n; i++)
    read(x[i]), read(y[i]);
  n += 2;
  x[n - 1] = 1;
  y[n - 1] = 1;
  x[n] = h;
  y[n] = w;

  for (int i = 1; i <= n; i++)
    D[i] = (node){x[i], y[i]};
  sort(D + 1, D + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    x[i] = D[i].x, y[i] = D[i].y;

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      if (y[j] <= y[i])
        update(dp[i], mod - dp[j] * C(x[i] - x[j], y[i] - y[j]) % mod);
  }
  printf("%lld\n", (mod - dp[n]) % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = (1e9) + 7;
const int maxn = 3010;
const int maxm = (2e5) + 10;
int n, h, w, x[maxn], y[maxn];
ll dp[maxn];
struct node {
  int x, y;
} D[maxn];
ll iv[maxm], ivjc[maxm], jc[maxm];
bool cmp(node a, node b) {
  if (a.x == b.x)
    return a.y < b.y;
  return a.x < b.x;
}
ll C(int x, int y) {
  x += y;
  return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod;
}
void update(ll &x, ll y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  // freopen("1.txt","r",stdin);
  jc[0] = iv[0] = ivjc[0] = 1;
  jc[1] = iv[1] = ivjc[1] = 1;
  for (int i = 2; i < maxm; i++) {
    jc[i] = jc[i - 1] * i % mod;
    iv[i] = (mod - mod / i) * iv[mod % i] % mod;
    ivjc[i] = ivjc[i - 1] * iv[i] % mod;
  }

  read(h);
  read(w);
  read(n);
  for (int i = 1; i <= n; i++)
    read(x[i]), read(y[i]);
  n += 2;
  x[n - 1] = 1;
  y[n - 1] = 1;
  x[n] = h;
  y[n] = w;

  for (int i = 1; i <= n; i++)
    D[i] = (node){x[i], y[i]};
  sort(D + 1, D + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    x[i] = D[i].x, y[i] = D[i].y;

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      if (y[j] <= y[i])
        update(dp[i], mod - dp[j] * C(x[i] - x[j], y[i] - y[j]) % mod);
  }
  printf("%lld\n", (mod - dp[n]) % mod);
  return 0;
}