Problem p03184 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define taskname "CNT"
const int MOD = 1e9 + 7;
typedef long long LL;

const int lim2 = 2e6 + 1;
LL fact[lim2], ifact[lim2];
void computeFactorials(int N);
LL powerMod(LL a, LL n, LL m);
LL combination(int N, int K);
LL countPath(int m, int n);

const int maxK = 1005;
LL F[maxK];

struct TPoint {
  int x, y;
  bool operator<(TPoint a) const { return (x + y) < (a.x + a.y); }
};
TPoint P[maxK];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N, M, K;
  cin >> N >> M >> K;
  computeFactorials(M + N);
  for (int i = 1; i <= K; i++)
    cin >> P[i].x >> P[i].y;
  P[++K].x = N;
  P[K].y = M;
  // P[0].x = P[0].y = 1;
  sort(P + 1, P + 1 + K);
  F[1] = countPath(P[1].x, P[1].y);
  for (int i = 2; i <= K; i++) {
    F[i] = countPath(P[i].x, P[i].y);
    for (int j = 1; j < i; j++) {
      if (P[j].x > P[i].x or P[j].y > P[i].y)
        continue;
      F[i] -= F[j] * countPath(P[i].x - P[j].x + 1, P[i].y - P[j].y + 1);
      F[i] %= MOD;
      if (F[i] < 0)
        F[i] += MOD;
    }
  }
  cout << F[K];
}

LL powerMod(LL a, LL n, LL m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return (a % m);
  LL t = powerMod(a, n / 2, m);
  t = (t * t) % m;
  if (n % 2)
    t = (t * a) % m;
  return t;
}

void computeFactorials(int N) {
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = (i * fact[i - 1]) % MOD;
  ifact[N] = powerMod(fact[N], MOD - 2, MOD);
  for (int i = N - 1; i >= 0; i--)
    ifact[i] = (ifact[i + 1] * (i + 1)) % MOD;
}

LL combination(int N, int K) {
  return (((fact[N] * ifact[K]) % MOD) * ifact[N - K]) % MOD;
}

LL countPath(int m, int n) { return combination(m + n - 2, m - 1); }

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define taskname "CNT"
const int MOD = 1e9 + 7;
typedef long long LL;

const int lim2 = 2e6 + 1;
LL fact[lim2], ifact[lim2];
void computeFactorials(int N);
LL powerMod(LL a, LL n, LL m);
LL combination(int N, int K);
LL countPath(int m, int n);

const int maxK = 2e5;
LL F[maxK];

struct TPoint {
  int x, y;
  bool operator<(TPoint a) const { return (x + y) < (a.x + a.y); }
};
TPoint P[maxK];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N, M, K;
  cin >> N >> M >> K;
  computeFactorials(M + N);
  for (int i = 1; i <= K; i++)
    cin >> P[i].x >> P[i].y;
  P[++K].x = N;
  P[K].y = M;
  // P[0].x = P[0].y = 1;
  sort(P + 1, P + 1 + K);
  F[1] = countPath(P[1].x, P[1].y);
  for (int i = 2; i <= K; i++) {
    F[i] = countPath(P[i].x, P[i].y);
    for (int j = 1; j < i; j++) {
      if (P[j].x > P[i].x or P[j].y > P[i].y)
        continue;
      F[i] -= F[j] * countPath(P[i].x - P[j].x + 1, P[i].y - P[j].y + 1);
      F[i] %= MOD;
      if (F[i] < 0)
        F[i] += MOD;
    }
  }
  cout << F[K];
}

LL powerMod(LL a, LL n, LL m) {
  if (n == 0)
    return 1;
  if (n == 1)
    return (a % m);
  LL t = powerMod(a, n / 2, m);
  t = (t * t) % m;
  if (n % 2)
    t = (t * a) % m;
  return t;
}

void computeFactorials(int N) {
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = (i * fact[i - 1]) % MOD;
  ifact[N] = powerMod(fact[N], MOD - 2, MOD);
  for (int i = N - 1; i >= 0; i--)
    ifact[i] = (ifact[i + 1] * (i + 1)) % MOD;
}

LL combination(int N, int K) {
  return (((fact[N] * ifact[K]) % MOD) * ifact[N - K]) % MOD;
}

LL countPath(int m, int n) { return combination(m + n - 2, m - 1); }