Problem p03184 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pi 3.1415926535897
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define inf1 1000000000
#define endl '\n'
bool comp(pair<pair<ll, ll>, ll> &a, pair<pair<ll, ll>, ll> &b) {
  if (a.ff.ff < b.ff.ff)
    return true;
  else if (a.ff.ff == b.ff.ff)
    return a.ff.ss < b.ff.ss;
  else
    return false;
}
ll fact[200005];
pair<pair<ll, ll>, ll> str[3005];
ll power(ll a, ll b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  if (b == 1)
    return a % mod;
  ll c = power(a, b / 2);
  ll d = (c * c) % mod;
  if (b % 2 == 1)
    d = (d * a) % mod;
  return d;
}
ll cal(ll x, ll y) {
  ll a = x + y - 2;
  ll b = x - 1;
  ll c = y - 1;
  ll ans = fact[a];
  ans = (ans * power(fact[b], mod - 2)) % mod;
  ans = (ans * power(fact[c], mod - 2)) % mod;
  return ans;
}
int main() {
  fastio;
#ifdef APNA_IO
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll h, w, n;
  cin >> h >> w >> n;
  fact[0] = 1;
  for (ll i = 1; i < 200005; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  ll ans = cal(h, w);
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    str[i] = mp(mp(a, b), cal(a, b));
  }
  sort(str, str + n, comp);
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (str[i].ff.ff <= str[j].ff.ff && str[i].ff.ss <= str[j].ff.ss) {
        str[j].ss =
            (str[j].ss - (str[i].ss * cal(str[j].ff.ff - str[i].ff.ff + 1,
                                          str[j].ff.ss - str[i].ff.ss + 1)) %
                             mod) %
            mod;
      }
    }
  }
  for (ll i = 0; i < n; i++)
    ans = (ans - (str[i].ss * cal(h - str[i].ff.ff + 1, w - str[i].ff.ss + 1)) %
                     mod) %
          mod;
  ans = ans % mod;
  ans = (ans + mod) % mod;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pi 3.1415926535897
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define inf1 1000000000
#define endl '\n'
bool comp(pair<pair<ll, ll>, ll> &a, pair<pair<ll, ll>, ll> &b) {
  if (a.ff.ff < b.ff.ff)
    return true;
  else if (a.ff.ff == b.ff.ff)
    return a.ff.ss < b.ff.ss;
  else
    return false;
}
ll fact[200005];
pair<pair<ll, ll>, ll> str[3005];
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    res %= mod;
    y = y >> 1;
    x = x * x;
    x %= mod;
  }
  return res;
}
ll cal(ll x, ll y) {
  ll a = x + y - 2;
  ll b = x - 1;
  ll c = y - 1;
  ll ans = fact[a];
  ans = (ans * power(fact[b], mod - 2)) % mod;
  ans = (ans * power(fact[c], mod - 2)) % mod;
  return ans;
}
int main() {
  fastio;
#ifdef APNA_IO
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll h, w, n;
  cin >> h >> w >> n;
  fact[0] = 1;
  for (ll i = 1; i < 200005; i++)
    fact[i] = (fact[i - 1] * i) % mod;
  ll ans = cal(h, w);
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    str[i] = mp(mp(a, b), cal(a, b));
  }
  sort(str, str + n, comp);
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (str[i].ff.ff <= str[j].ff.ff && str[i].ff.ss <= str[j].ff.ss) {
        str[j].ss =
            (str[j].ss - (str[i].ss * cal(str[j].ff.ff - str[i].ff.ff + 1,
                                          str[j].ff.ss - str[i].ff.ss + 1)) %
                             mod) %
            mod;
      }
    }
  }
  for (ll i = 0; i < n; i++)
    ans = (ans - (str[i].ss * cal(h - str[i].ff.ff + 1, w - str[i].ff.ss + 1)) %
                     mod) %
          mod;
  ans = ans % mod;
  ans = (ans + mod) % mod;
  cout << ans << endl;
  return 0;
}