Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define lld long double
#define pb push_back
#define pii pair<ll, ll>
#define mi map<ll, ll>
#define vec vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define mod 1000000007
#define nmod -1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = a; i <= b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define mp make_pair
#define mit map<ll, ll>::iterator
#define sit set<ll>::iterator
#define pit pair<ll, ll>::iterator
#define xxx exit(0);
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y) { return (x | y) & (~x | ~y); }
ll bin_Expo(ll x, ll n) {
  if (x == 0)
    return 0;
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return bin_Expo(x * x, n / 2);
  else // n is odd
    return x * bin_Expo(x * x, (n - 1) / 2);
}
ll mod_Expo(ll x, ll n, ll M) {
  if (x == 0)
    return 0;
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return mod_Expo((x * x) % M, n / 2, M);
  else // n is odd
    return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M;
}
bool prime_check(ll x) {
  bool prime = (x >= 2);
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      prime = false;
      break;
    }
  }
  return prime;
}
ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); }
ll h, w, n, dp[100005], f[200005], fc[200005];
ll ways(ll x, ll y) {
  if (x < 0 || y < 0)
    return 0;
  return (((f[x + y] * fc[x]) % mod) * fc[y]) % mod;
}
bool cc(pii &a, pii &b) { return (a.F + a.S) < (b.F + b.S); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    ll i, l, j, x1, y1, x, y, q, h;
    cin >> h >> w >> n;
    vector<pii> z(n + 1);
    rep(i, 0, n) {
      cin >> x1 >> y1;
      z[i] = {x1, y1};
    }
    z[n] = {mod, mod};
    sort(all(z), cc);
    z[n] = {h, w};
    f[0] = 1;
    fc[0] = 1;
    dp[0] = 0;
    repi(i, 1, 200004) {
      f[i] = (f[i - 1] * i) % mod;
      fc[i] = mod_Expo(f[i], mod - 2, mod);
      dp[i] = 0;
    }
    repi(i, 0, n) {
      dp[i] = ways(z[i].F - 1, z[i].S - 1);
      rep(j, 0, i) {
        if (z[i].F >= z[j].F && z[i].S >= z[j].S)
          dp[i] =
              (dp[i] -
               ((ways(z[i].F - z[j].F, z[i].S - z[j].S) * dp[j]) % mod) + mod) %
              mod;
      }
    }
    cout << dp[n];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define lld long double
#define pb push_back
#define pii pair<ll, ll>
#define mi map<ll, ll>
#define vec vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define mod 1000000007
#define nmod -1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = a; i <= b; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define mp make_pair
#define mit map<ll, ll>::iterator
#define sit set<ll>::iterator
#define pit pair<ll, ll>::iterator
#define xxx exit(0);
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y) { return (x | y) & (~x | ~y); }
ll bin_Expo(ll x, ll n) {
  if (x == 0)
    return 0;
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return bin_Expo(x * x, n / 2);
  else // n is odd
    return x * bin_Expo(x * x, (n - 1) / 2);
}
ll mod_Expo(ll x, ll n, ll M) {
  if (x == 0)
    return 0;
  if (n == 0)
    return 1;
  else if (n % 2 == 0) // n is even
    return mod_Expo((x * x) % M, n / 2, M);
  else // n is odd
    return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M;
}
bool prime_check(ll x) {
  bool prime = (x >= 2);
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      prime = false;
      break;
    }
  }
  return prime;
}
ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); }
ll h, w, n, dp[200005], f[200005], fc[200005];
ll ways(ll x, ll y) {
  if (x < 0 || y < 0)
    return 0;
  return (((f[x + y] * fc[x]) % mod) * fc[y]) % mod;
}
bool cc(pii &a, pii &b) { return (a.F + a.S) < (b.F + b.S); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //	cin>>TESTS;
  while (TESTS--) {
    ll i, l, j, x1, y1, x, y, q, h;
    cin >> h >> w >> n;
    vector<pii> z(n + 1);
    rep(i, 0, n) {
      cin >> x1 >> y1;
      z[i] = {x1, y1};
    }
    z[n] = {mod, mod};
    sort(all(z), cc);
    z[n] = {h, w};
    f[0] = 1;
    fc[0] = 1;
    dp[0] = 0;
    repi(i, 1, 200004) {
      f[i] = (f[i - 1] * i) % mod;
      fc[i] = mod_Expo(f[i], mod - 2, mod);
      dp[i] = 0;
    }
    repi(i, 0, n) {
      dp[i] = ways(z[i].F - 1, z[i].S - 1);
      rep(j, 0, i) {
        if (z[i].F >= z[j].F && z[i].S >= z[j].S)
          dp[i] =
              (dp[i] -
               ((ways(z[i].F - z[j].F, z[i].S - z[j].S) * dp[j]) % mod) + mod) %
              mod;
      }
    }
    cout << dp[n];
  }
  return 0;
}