Problem p03184 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
const int mod = 1e9 + 7;
using modint = ModInt<mod>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, n;
  cin >> h >> w >> n;
  vector<pair<int, int>> cor(n);
  rep(i, n) {
    int r, c;
    cin >> r >> c;
    --r;
    --c;
    cor[i] = mp(r, c);
  }
  sort(cor.begin(), cor.end());
  const int n_max = h + w + 10;
  vector<modint> kai(n_max);
  kai[0] = 1;
  rep1(i, n_max - 1) kai[i] = kai[i - 1] * ((modint)i);
  vector<vector<modint>> dp(n, vector<modint>(2));
  rep(i, n) dp[i][0] = 0;
  rep(i, n) {
    int r = cor[i].first;
    int c = cor[i].second;
    dp[i][1] = kai[r + c] / kai[r] / kai[c];
  }
  modint ans = kai[h + w - 2] / kai[h - 1] / kai[w - 1];
  rep(i, n) {
    int ci = cor[i].second;
    int ri = cor[i].first;
    rep(j, i) {
      int cj = cor[j].second;
      if (ci >= cj) {
        int rj = cor[j].first;
        dp[i][0] +=
            dp[j][1] * kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj];
        dp[i][1] +=
            dp[j][0] * kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj];
      }
    }
    ans += (dp[i][0] - dp[i][1]) * kai[h - 1 - ri + w - 1 - ci] /
           kai[h - 1 - ri] / kai[w - 1 - ci];
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
const int mod = 1e9 + 7;
using modint = ModInt<mod>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, n;
  cin >> h >> w >> n;
  vector<pair<int, int>> cor(n);
  rep(i, n) {
    int r, c;
    cin >> r >> c;
    --r;
    --c;
    cor[i] = mp(r, c);
  }
  sort(cor.begin(), cor.end());
  const int n_max = h + w + 10;
  vector<modint> kai(n_max);
  kai[0] = 1;
  rep1(i, n_max - 1) kai[i] = kai[i - 1] * ((modint)i);
  vector<vector<modint>> dp(n, vector<modint>(2));
  rep(i, n) dp[i][0] = 0;
  rep(i, n) {
    int r = cor[i].first;
    int c = cor[i].second;
    dp[i][1] = kai[r + c] / kai[r] / kai[c];
  }
  modint ans = kai[h + w - 2] / kai[h - 1] / kai[w - 1];
  rep(i, n) {
    int ci = cor[i].second;
    int ri = cor[i].first;
    rep(j, i) {
      int cj = cor[j].second;
      if (ci >= cj) {
        int rj = cor[j].first;
        modint fac = kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj];
        dp[i][0] += dp[j][1] * fac;
        dp[i][1] += dp[j][0] * fac;
      }
    }
    ans += (dp[i][0] - dp[i][1]) * kai[h - 1 - ri + w - 1 - ci] /
           kai[h - 1 - ri] / kai[w - 1 - ci];
  }

  cout << ans << endl;
  return 0;
}