Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll mod = 1e9 + 7;
const int maxv = 200100;
const int maxn = 3100;
ll fact[maxv], ifact[maxv];

ll power(ll x, ll y) {
  if (y == 0)
    return 1LL;
  else if (y == 1)
    return 1LL * x % mod;
  else {
    if (y % 2 == 0) {
      ll half = power(x, y / 2);
      return (half * half) % mod;
    } else
      return (x * power(x, y - 1)) % mod;
  }
}

void preprocess() {
  fact[0] = 1LL;
  ifact[0] = power(fact[0], mod - 2);
  for (ll i = 1LL; i < maxv; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
    ifact[i] = power(fact[i], mod - 2);
  }
}

ll ncr(ll x, ll y) {
  return 1LL * ((fact[x] * ifact[y]) % mod * ifact[x - y]) % mod;
}

int n, m, k;
vector<pair<int, int>> points;
ll cntunique[maxn];

int main() {
  preprocess();
  cin >> n >> m >> k;
  int x, y;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    points.pb(mp(x, y));
  }

  sort(points.begin(), points.end());
  ll result = ncr(n + m - 2, n - 1);
  for (int i = 0; i < k; i++) {
    cntunique[i] =
        ncr(points[i].first + points[i].second - 2, points[i].first - 1);
    for (int j = 0; j < k; j++) {
      if (points[j].first <= points[i].first &&
          points[j].second <= points[j].second && i != j) {
        int diffx = points[i].first - points[j].first;
        int diffy = points[i].second - points[j].second;
        cntunique[i] =
            (cntunique[i] - (cntunique[j] * ncr(diffx + diffy, diffy)) % mod +
             mod) %
            mod;
        cntunique[i] %= mod;
      }
    }
    int diffx = n - points[i].first;
    int diffy = m - points[i].second;
    result =
        (result - (cntunique[i] * ncr(diffx + diffy, diffy)) % mod + mod) % mod;
    result %= mod;
  }
  cout << result << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll mod = 1e9 + 7;
const int maxv = 200100;
const int maxn = 3100;
ll fact[maxv], ifact[maxv];

ll power(ll x, ll y) {
  if (y == 0)
    return 1LL;
  else if (y == 1)
    return 1LL * x % mod;
  else {
    if (y % 2 == 0) {
      ll half = power(x, y / 2);
      return (half * half) % mod;
    } else
      return (x * power(x, y - 1)) % mod;
  }
}

void preprocess() {
  fact[0] = 1LL;
  ifact[0] = power(fact[0], mod - 2);
  for (ll i = 1LL; i < maxv; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
    ifact[i] = power(fact[i], mod - 2);
  }
}

ll ncr(ll x, ll y) {
  return 1LL * ((fact[x] * ifact[y]) % mod * ifact[x - y]) % mod;
}

int n, m, k;
vector<pair<int, int>> points;
ll cntunique[maxn];

int main() {
  preprocess();
  cin >> n >> m >> k;
  int x, y;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    points.pb(mp(x, y));
  }

  sort(points.begin(), points.end());
  ll result = ncr(n + m - 2, n - 1);
  for (int i = 0; i < k; i++) {
    cntunique[i] =
        ncr(points[i].first + points[i].second - 2, points[i].first - 1);
    for (int j = 0; j < k; j++) {
      if (points[j].first <= points[i].first &&
          points[j].second <= points[i].second && i != j) {
        int diffx = points[i].first - points[j].first;
        int diffy = points[i].second - points[j].second;
        cntunique[i] =
            (cntunique[i] - (cntunique[j] * ncr(diffx + diffy, diffy)) % mod +
             mod) %
            mod;
        cntunique[i] %= mod;
      }
    }
    int diffx = n - points[i].first;
    int diffy = m - points[i].second;
    result =
        (result - (cntunique[i] * ncr(diffx + diffy, diffy)) % mod + mod) % mod;
    result %= mod;
  }
  cout << result << "\n";
  return 0;
}