Problem p03184 (C++)

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

using namespace std;

#define ff first
#define ss second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<long long, long long> pl;

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-6;
const int MAX_H = 1e5 + 5;
const int MAX_N = 1e3 + 5;

ll fastExpo(int base, int e) {
  if (e == 0)
    return 1;
  if (e == 1)
    return base;
  ll ans = fastExpo(base, e / 2);
  ans = ans * ans % MOD;
  if (e % 2 == 1)
    ans = ans * base % MOD;
  return ans;
}

namespace modOp {
int add(int a, int b, int mod = MOD) { return (a + b) % mod; }
int sub(int a, int b, int mod = MOD) { return (a - b + mod) % mod; }
int mul(int a, int b, int mod = MOD) { return (ll)a * b % mod; }
int divide(int a, int b, int mod = MOD) {
  return (ll)a * fastExpo(b, mod - 2) % mod;
}
} // namespace modOp
using namespace modOp;

int H, W, N;
pi walls[MAX_N];
int dp[MAX_N];
ll fac[2 * MAX_H], inv[2 * MAX_H];

int f(int a, int b) {
  a += b;
  if (a < 0 || b < 0 || b > a)
    return 0;
  return mul(fac[a], mul(inv[b], inv[a - b]));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fac[0] = inv[0] = 1;
  for (int i = 1; i < 2 * MAX_H; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = divide(1, fac[i]);
  }

  cin >> H >> W >> N;
  for (int i = 0; i < N; ++i) {
    cin >> walls[i].ff >> walls[i].ss;
  }
  walls[N] = {H, W};

  sort(walls, walls + N + 1,
       [](pi a, pi b) { return a.ff + a.ss < b.ff + b.ss; });

  for (int i = 0; i <= N; ++i) {
    dp[i] = f(walls[i].ff - 1, walls[i].ss - 1);
    for (int j = 0; j < i; ++j) {
      if (walls[i].ff >= walls[j].ff && walls[i].ss >= walls[j].ss) {
        dp[i] = sub(dp[i], mul(dp[j], f(walls[i].ff - walls[j].ff,
                                        walls[i].ss - walls[j].ss)));
      }
    }
  }

  cout << dp[N] << '\n';

  return 0;
}

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

using namespace std;

#define ff first
#define ss second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<long long, long long> pl;

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-6;
const int MAX_H = 1e5 + 5;
const int MAX_N = 3e3 + 5;

ll fastExpo(int base, int e) {
  if (e == 0)
    return 1;
  if (e == 1)
    return base;
  ll ans = fastExpo(base, e / 2);
  ans = ans * ans % MOD;
  if (e % 2 == 1)
    ans = ans * base % MOD;
  return ans;
}

namespace modOp {
int add(int a, int b, int mod = MOD) { return (a + b) % mod; }
int sub(int a, int b, int mod = MOD) { return (a - b + mod) % mod; }
int mul(int a, int b, int mod = MOD) { return (ll)a * b % mod; }
int divide(int a, int b, int mod = MOD) {
  return (ll)a * fastExpo(b, mod - 2) % mod;
}
} // namespace modOp
using namespace modOp;

int H, W, N;
pi walls[MAX_N];
int dp[MAX_N];
ll fac[2 * MAX_H], inv[2 * MAX_H];

int f(int a, int b) {
  a += b;
  if (a < 0 || b < 0 || b > a)
    return 0;
  return mul(fac[a], mul(inv[b], inv[a - b]));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fac[0] = inv[0] = 1;
  for (int i = 1; i < 2 * MAX_H; ++i) {
    fac[i] = mul(fac[i - 1], i);
    inv[i] = divide(1, fac[i]);
  }

  cin >> H >> W >> N;
  for (int i = 0; i < N; ++i) {
    cin >> walls[i].ff >> walls[i].ss;
  }
  walls[N] = {H, W};

  sort(walls, walls + N + 1,
       [](pi a, pi b) { return a.ff + a.ss < b.ff + b.ss; });

  for (int i = 0; i <= N; ++i) {
    dp[i] = f(walls[i].ff - 1, walls[i].ss - 1);
    for (int j = 0; j < i; ++j) {
      if (walls[i].ff >= walls[j].ff && walls[i].ss >= walls[j].ss) {
        dp[i] = sub(dp[i], mul(dp[j], f(walls[i].ff - walls[j].ff,
                                        walls[i].ss - walls[j].ss)));
      }
    }
  }

  cout << dp[N] << '\n';

  return 0;
}