Problem p03184 (C++)

Buggy submission (status: Runtime Error):
/* Akshat Sharma @akshat98 */
#include <bits/stdc++.h>
#include <iostream>
#define IOS ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define MAX 1000000001
#define vi vector<int>
#define vl vector<ll>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define rep(i, a, b) for (i = a; i < b; i++)
#define reqp(i, a, b) for (i = a; i <= b; i++)
#define ren for (i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll fact[200005]{}, ifact[200005]{};
ll mod(ll a, ll m = MOD) {
  a %= m;
  if (a < 0)
    a += m;
  return a;
}

ll power(ll a, ll b, ll m = MOD) {
  ll res = 1;
  while (b) {
    if (b % 2)
      res = mod(1ll * res * a), b--;
    else
      a = mod(1ll * a * a), b /= 2;
  }
  return res;
}

ll ncr(ll n, ll r) {
  ll res = mod(ifact[n - r] * ifact[r]);
  res = mod(res * fact[n]);
  return res;
}

ll calc(ll x1, ll y1, ll x2, ll y2) { return ncr(x2 - x1 + y2 - y1, x2 - x1); }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input_file.txt", "r", stdin);
// freopen("output_file.txt", "w", stdout);
#endif
  ll i, j, k, l, n, test_case, m, r, H, W;
  test_case = 1;
  // cin>>test_case;
  fact[0] = fact[1] = ifact[0] = ifact[1] = 1;
  rep(i, 2, 200005) {
    fact[i] = mod(1ll * i * fact[i - 1]);
    ifact[i] = power(fact[i], MOD - 2);
  }
  while (test_case--) {
    cin >> H >> W >> n;
    vl dp(n);
    vpi vec(n);
    ren cin >> vec[i].F >> vec[i].S;
    sort(ALL(vec), [](const pii &a, const pii &b) {
      return ((a.F < b.F) || ((a.F == b.F) && (a.S < b.S)));
    });
    ll tans = 0;
    rep(i, 0, n) {
      ll temp = 0;
      rep(j, 0, i) {
        if ((vec[j].F <= vec[i].F) && (vec[j].S <= vec[i].S)) {
          temp = mod(temp +
                     mod(dp[j] * calc(vec[j].F, vec[j].S, vec[i].F, vec[i].S)));
        }
      }
      dp[i] = mod(calc(1, 1, vec[i].F, vec[i].S) - temp);
      tans = mod(tans + mod(dp[i] * calc(vec[i].F, vec[i].S, H, W)));
    }
    cout << mod(calc(1, 1, H, W) - tans) << "\n";
  }

  return 0;
}

Fixed submission:
/* Akshat Sharma @akshat98 */
#include <bits/stdc++.h>
#include <iostream>
#define IOS ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define MAX 1000000001
#define vi vector<int>
#define vl vector<ll>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define rep(i, a, b) for (i = a; i < b; i++)
#define reqp(i, a, b) for (i = a; i <= b; i++)
#define ren for (i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll fact[200005]{}, ifact[200005]{};
ll mod(ll a, ll m = MOD) {
  a %= m;
  if (a < 0)
    a += m;
  return a;
}

ll power(ll a, ll b, ll m = MOD) {
  ll res = 1;
  while (b) {
    if (b % 2)
      res = mod(1ll * res * a), b--;
    else
      a = mod(1ll * a * a), b /= 2;
  }
  return res;
}

ll ncr(ll n, ll r) {
  ll res = mod(ifact[n - r] * ifact[r]);
  res = mod(res * fact[n]);
  return res;
}

ll calc(ll x1, ll y1, ll x2, ll y2) { return ncr(x2 - x1 + y2 - y1, x2 - x1); }

int main() {
#ifndef ONLINE_JUDGE
// freopen("input_file.txt", "r", stdin);
// freopen("output_file.txt", "w", stdout);
#endif
  ll i, j, k, l, n, test_case, m, r, H, W;
  test_case = 1;
  // cin>>test_case;
  fact[0] = fact[1] = ifact[0] = ifact[1] = 1;
  rep(i, 2, 200005) {
    fact[i] = mod(1ll * i * fact[i - 1]);
    ifact[i] = power(fact[i], MOD - 2);
  }
  while (test_case--) {
    cin >> H >> W >> n;
    vl dp(n);
    vpi vec(n);
    ren cin >> vec[i].F >> vec[i].S;
    sort(ALL(vec), [](const pii &a, const pii &b) {
      return ((a.F < b.F) || ((a.F == b.F) && (a.S < b.S)));
    });
    ll tans = 0;
    rep(i, 0, n) {
      ll temp = 0;
      rep(j, 0, i) {
        if ((vec[j].F <= vec[i].F) && (vec[j].S <= vec[i].S)) {
          temp = mod(temp +
                     mod(dp[j] * calc(vec[j].F, vec[j].S, vec[i].F, vec[i].S)));
        }
      }
      dp[i] = mod(calc(1, 1, vec[i].F, vec[i].S) - temp);
      tans = mod(tans + mod(dp[i] * calc(vec[i].F, vec[i].S, H, W)));
    }
    cout << mod(calc(1, 1, H, W) - tans) << "\n";
  }

  return 0;
}