Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 1e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll sq(ll x) { return (x * x) % MOD; }
ll modP(ll a, ll b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}

ll fact[maxN], invfact[maxN];

void initFact() {
  fact[0] = 1;
  for (ll i = 1; i < maxN; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  invfact[maxN - 1] = modP(fact[maxN - 1], MOD - 2);
  for (ll i = maxN - 2; i > -1; i--) {
    invfact[i] = (invfact[i + 1] * (i + 1)) % MOD;
  }
}

ll choose(ll n, ll k) {
  return (((fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD;
}

ll dp[maxN];
pll P[maxN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  initFact();
  ll h, w, n;
  cin >> h >> w >> n;
  for (ll i = 0; i < n; i++) {
    cin >> P[i].first >> P[i].second;
  }
  P[n] = {h, w};
  sort(P, P + n + 1);
  ll x = P[0].first;
  ll y = P[0].second;
  dp[0] = choose(x + y - 2, max(x - 1, y - 1));
  for (ll i = 0; i <= n; i++) {
    ll x = P[i].first;
    ll y = P[i].second;
    dp[i] = choose(x + y - 2, max(x - 1, y - 1));
    for (ll j = 0; j < i; j++) {
      if (P[i].second < P[j].second)
        continue;
      ll x = P[i].first - P[j].first;
      ll y = P[i].second - P[j].second;
      dp[i] -= dp[j] * choose(x + y, max(x, y));
      dp[i] = (dp[i] + MOD) % MOD;
    }
  }
  while (dp[n] < 0)
    dp[n] += MOD;
  cout << dp[n] % MOD << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 2e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll sq(ll x) { return (x * x) % MOD; }
ll modP(ll a, ll b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}

ll fact[maxN], invfact[maxN];

void initFact() {
  fact[0] = 1;
  for (ll i = 1; i < maxN; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  invfact[maxN - 1] = modP(fact[maxN - 1], MOD - 2);
  for (ll i = maxN - 2; i > -1; i--) {
    invfact[i] = (invfact[i + 1] * (i + 1)) % MOD;
  }
}

ll choose(ll n, ll k) {
  return (((fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD;
}

ll dp[maxN];
pll P[maxN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  initFact();
  ll h, w, n;
  cin >> h >> w >> n;
  for (ll i = 0; i < n; i++) {
    cin >> P[i].first >> P[i].second;
  }
  P[n] = {h, w};
  sort(P, P + n + 1);
  ll x = P[0].first;
  ll y = P[0].second;
  dp[0] = choose(x + y - 2, max(x - 1, y - 1));
  for (ll i = 0; i <= n; i++) {
    ll x = P[i].first;
    ll y = P[i].second;
    dp[i] = choose(x + y - 2, max(x - 1, y - 1));
    for (ll j = 0; j < i; j++) {
      if (P[i].second < P[j].second)
        continue;
      ll x = P[i].first - P[j].first;
      ll y = P[i].second - P[j].second;
      dp[i] -= dp[j] * choose(x + y, max(x, y));
      dp[i] = (dp[i] + MOD) % MOD;
    }
  }
  while (dp[n] < 0)
    dp[n] += MOD;
  cout << dp[n] % MOD << endl;
  return 0;
}