Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define ff first
#define ss second
#define pb push_back
#define all(s) (s).begin(), (s).end()
const ll mod = 1e9 + 7;

ll h, w, x;
vector<pair<ll, ll>> arr;
vi ans, fact;

ll binexp(ll a) {
  ll res = 1, y = mod - 2;
  while (y > 0) {
    if (y & 1)
      res = ((res * a) % mod + mod) % mod;
    y >>= 1;
    a = ((a * a) % mod + mod) % mod;
  }
  return res % mod;
}

ll num(ll x1, ll y1, ll x2, ll y2) {
  ll x = fact[x2 - x1 + y2 - y1];
  ll y = binexp(((fact[x2 - x1] * fact[y2 - y1]) % mod + mod) % mod);
  return ((x * y) % mod + mod) % mod;
}

void solve() {
  for (int i = 2; i <= h + w + 1; i++)
    fact[i] = ((fact[i - 1] * i) % mod + mod) % mod;

  for (int i = 0; i <= x; i++) {
    ll ans_i = num(1, 1, arr[i].ff, arr[i].ss);
    ll ans_t = 0;
    for (int j = 0; j < i; j++) {
      if (arr[j].ff <= arr[i].ff && arr[j].ss <= arr[i].ss) {
        ans_t +=
            (ans[j] * num(arr[j].ff, arr[j].ss, arr[i].ff, arr[i].ss) + mod) %
            mod;
        ans_t %= mod;
      }
    }
    ans[i] = (ans_i - ans_t) % mod + mod;
    ans[i] %= mod;
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> h >> w >> x;
  arr.resize(x);
  ans.resize(x + 1);
  fact.resize(h + w + 1);
  fact[0] = fact[1] = 1;
  for (int i = 0; i < x; i++) {
    ll a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  sort(all(arr));
  arr.pb({h, w});
  solve();
  cout << ans[x] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
#define ff first
#define ss second
#define pb push_back
#define all(s) (s).begin(), (s).end()
const ll mod = 1e9 + 7;

ll h, w, x;
vector<pair<ll, ll>> arr;
vi ans, fact;

ll binexp(ll a) {
  ll res = 1, y = mod - 2;
  while (y > 0) {
    if (y & 1)
      res = ((res * a) % mod + mod) % mod;
    y >>= 1;
    a = ((a * a) % mod + mod) % mod;
  }
  return res % mod;
}

ll num(ll x1, ll y1, ll x2, ll y2) {
  ll x = fact[x2 - x1 + y2 - y1];
  ll y = binexp(((fact[x2 - x1] * fact[y2 - y1]) % mod + mod) % mod);
  return ((x * y) % mod + mod) % mod;
}

void solve() {
  for (int i = 2; i <= h + w; i++)
    fact[i] = ((fact[i - 1] * i) % mod + mod) % mod;

  for (int i = 0; i <= x; i++) {
    ll ans_i = num(1, 1, arr[i].ff, arr[i].ss);
    ll ans_t = 0;
    for (int j = 0; j < i; j++) {
      if (arr[j].ff <= arr[i].ff && arr[j].ss <= arr[i].ss) {
        ans_t +=
            (ans[j] * num(arr[j].ff, arr[j].ss, arr[i].ff, arr[i].ss) + mod) %
            mod;
        ans_t %= mod;
      }
    }
    ans[i] = (ans_i - ans_t) % mod + mod;
    ans[i] %= mod;
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> h >> w >> x;
  arr.resize(x);
  ans.resize(x + 1);
  fact.resize(h + w + 1);
  fact[0] = fact[1] = 1;
  for (int i = 0; i < x; i++) {
    ll a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  sort(all(arr));
  arr.pb({h, w});
  solve();
  cout << ans[x] << endl;
  return 0;
}