Problem p03184 (C++)

Buggy submission (status: Runtime Error):
/*
 _____ _             _              _           _
|_   _| |__   ___   / \   _ __  ___| |__  _   _| |
  | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  | | | | | |  __// ___ \| | | \__ \ | | | |_| | |
  |_| |_| |_|\___/_/   \_\_| |_|___/_| |_|\__,_|_|

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

/*	For Debugging	*/
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x)                                                             \
  trav(a, x) cout << a << " ";                                                 \
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

std::mt19937_64
    rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME                                                                   \
  cerr << "\nTime elapsed: " << setprecision(5)                                \
       << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n)                                                             \
  cout << fixed;                                                               \
  cout << setprecision(n);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 200005
ll dp[1005];
ll fact[N], modi[N];
ll add(ll a, ll b, ll mod = hell) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod = hell) { return (a - b + mod) % mod; }
ll mul(ll a, ll b, ll mod = hell) { return (a * b) % mod; }
ll expo(ll base, ll exponent, ll mod) { // return base^exponent modulo modulus
  ll ans = 1;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base = base * base;
    base %= mod;
    exponent >>= 1;
  }
  return ans % mod;
}
ll modii(ll num) { return expo(num, hell - 2, hell); }
void init() {
  fact[0] = 1;
  rep(i, 1, N) fact[i] = (fact[i - 1] * i) % hell;
  rep(i, 0, N) modi[i] = modii(fact[i]);
}
ll fun(ll x1, ll y1, ll x2, ll y2) {
  ll x = abs(x1 - x2), y = abs(y1 - y2);
  // cout<<x<<" - "<<y<<" "<<x+y<<endl;
  return mul(fact[x + y], mul(modi[x], modi[y]));
}

void solve() {
  ll h, w, n;
  cin >> h >> w >> n;
  vii v(n);
  rep(i, 0, n) { cin >> v[i].F >> v[i].S; }
  v.pb({h, w});
  sort(all(v));
  n++;
  rep(i, 0, n) dp[i] = fun(1, 1, v[i].F, v[i].S);
  // rep(i,0,n)
  // {
  // 	cout<<v[i].F<<" "<<v[i].S<<" "<<dp[i]<<"\n";
  // }
  // cout<<endl;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (v[i].F > v[j].F || v[i].S > v[j].S)
        continue;
      dp[j] = sub(dp[j], mul(dp[i], fun(v[i].F, v[i].S, v[j].F, v[j].S)));
    }
  }
  cout << dp[n - 1] << endl;
  return;
}
int main() {
  init();
  FAST int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  TIME return 0;
}

Fixed submission:
/*
 _____ _             _              _           _
|_   _| |__   ___   / \   _ __  ___| |__  _   _| |
  | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  | | | | | |  __// ___ \| | | \__ \ | | | |_| | |
  |_| |_| |_|\___/_/   \_\_| |_|___/_| |_|\__,_|_|

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define ppb pop_back
#define endl '\n'
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

/*	For Debugging	*/
#define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl;
#define display(x)                                                             \
  trav(a, x) cout << a << " ";                                                 \
  cout << endl;
#define what_is(x) cerr << #x << " is " << x << endl;

std::mt19937_64
    rng(std::chrono::steady_clock::now().time_since_epoch().count());
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define TIME                                                                   \
  cerr << "\nTime elapsed: " << setprecision(5)                                \
       << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define DECIMAL(n)                                                             \
  cout << fixed;                                                               \
  cout << setprecision(n);
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace __gnu_pbds;
using namespace std;
#define PI 3.141592653589793
#define N 200005
ll dp[3005];
ll fact[N], modi[N];
ll add(ll a, ll b, ll mod = hell) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod = hell) { return (a - b + mod) % mod; }
ll mul(ll a, ll b, ll mod = hell) { return (a * b) % mod; }
ll expo(ll base, ll exponent, ll mod) { // return base^exponent modulo modulus
  ll ans = 1;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base = base * base;
    base %= mod;
    exponent >>= 1;
  }
  return ans % mod;
}
ll modii(ll num) { return expo(num, hell - 2, hell); }
void init() {
  fact[0] = 1;
  rep(i, 1, N) fact[i] = (fact[i - 1] * i) % hell;
  rep(i, 0, N) modi[i] = modii(fact[i]);
}
ll fun(ll x1, ll y1, ll x2, ll y2) {
  ll x = abs(x1 - x2), y = abs(y1 - y2);
  // cout<<x<<" - "<<y<<" "<<x+y<<endl;
  return mul(fact[x + y], mul(modi[x], modi[y]));
}

void solve() {
  ll h, w, n;
  cin >> h >> w >> n;
  vii v(n);
  rep(i, 0, n) { cin >> v[i].F >> v[i].S; }
  v.pb({h, w});
  sort(all(v));
  n++;
  rep(i, 0, n) dp[i] = fun(1, 1, v[i].F, v[i].S);
  // rep(i,0,n)
  // {
  // 	cout<<v[i].F<<" "<<v[i].S<<" "<<dp[i]<<"\n";
  // }
  // cout<<endl;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (v[i].F > v[j].F || v[i].S > v[j].S)
        continue;
      dp[j] = sub(dp[j], mul(dp[i], fun(v[i].F, v[i].S, v[j].F, v[j].S)));
    }
  }
  cout << dp[n - 1] << endl;
  return;
}
int main() {
  init();
  FAST int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  TIME return 0;
}