Problem:
Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns.
Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.

In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares.
It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Constraints

All values in input are integers.

2 \leq H, W \leq 10^5

1 \leq N \leq 3000

1 \leq r_i \leq H

1 \leq c_i \leq W

Squares (r_i, c_i) are all distinct.

Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W N
r_1 c_1
r_2 c_2
:
r_N c_N

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Sample Input 1

3 4 2
2 2
1 4

Sample Output 1

3

There are three paths as follows:

Sample Input 2

5 2 2
2 1
4 2

Sample Output 2

0

There may be no paths.

Sample Input 3

5 5 4
3 1
3 5
1 3
5 3

Sample Output 3

24

Sample Input 4

100000 100000 1
50000 50000

Sample Output 4

123445622

Be sure to print the count modulo 10^9 + 7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()),((v).end())
#define ll long long
#define fastIO cout << fixed << setprecision(9), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
double const EPS = 1e-9, PI = acos(-1);
const int N = 2e5 + 9, M = 1e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;
const ll inf = 1e18;

ll add(ll a, ll b) {
  a = ((a % MOD) + (b % MOD)) % MOD;
  return a;
}

ll sub(ll a, ll b) {
  a = (((((a % MOD) - (b % MOD)) % MOD) + MOD) % MOD);
  return a;
}

ll mul(ll a, ll b) {
  return (a * b) % MOD;
}

ll power(ll a, ll b) {
  ll ret = 1;
  while(b) {
    if(b & 1)
      ret = mul(ret, a);
     b /= 2;
     a = mul(a, a);
  }
  return ret;
}

string arr[M];
ll fact[N], inv[N], dp[N];

ll cmp(pair<int, int> a, pair<int, int> b) {
  return a.first + a.second < b.first + b.second;
}

int main() {
  fastIO;
#ifdef LOCAL
  freopen("input.in", "rt", stdin);
#endif
  fact[0] = 1, inv[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = mul(i, fact[i - 1]), inv[i] = power(fact[i], MOD - 2);
  int n, m, k, x, y;
  cin >> n >> m >> k;
  vector<pair<int, int>> vec;
  for (int i = 0; i < k; ++i) {
    cin >> x >> y;
    vec.push_back({x, y});
//    dp[i] = mul(fact[(x - 1) + (y - 1)], mul(inv[(x - 1)],inv[y - 1])); // (x - 1) + (y - 1) - (y - 1) => (x-1)
    cerr << dp[i] << endl;
  }
  sort(vec.begin(), vec.end(), cmp);
  for (int i = 0; i < k; ++i) {
    x = vec[i].first, y = vec[i].second;
    ll tmp = mul(fact[(x - 1) + (y - 1)], mul(inv[(x - 1)],inv[y - 1]));
    for (int j = 0; j < i; ++j) {
      if(vec[j].first <= vec[i].first && vec[j].second <= vec[i].second) {
        ll temp = mul(fact[(vec[i].first - vec[j].first) + (vec[i].second - vec[j].second)], mul(inv[(vec[i].first - vec[j].first)],inv[(vec[i].second - vec[j].second)]));
        tmp = sub(tmp, mul(dp[j], temp));
//        cerr << dp[i] << endl;
      }
    }
    dp[i] = tmp;
  }
  dp[k] = mul(fact[(n - 1) + (m - 1)], mul(inv[(n - 1)],inv[m - 1]));
  for (int j = 0; j < k; ++j) {
    if(vec[j].first <= n && vec[j].second <= m) {
      ll temp = mul(fact[(n - vec[j].first) + (m - vec[j].second)], mul(inv[(n - vec[j].first)],inv[(m - vec[j].second)]));
      dp[k] = sub(dp[k], mul(dp[j], temp));
      cerr << dp[k] << endl;
    }
  }
  cout << dp[k];
//  for (int i = 1; i <= mm; ++i) {
//    int m = i;
//    memset(dp, 0, sizeof dp);
//    dp[0][0] = 1;
//    int a = n, b = m;
//    if(a < b)
//      swap(a, b);
//    ll ans = mul(fact[a+b-2], mul(inv[b - 1], inv[a - 1]));
//    cerr << ans << endl;
//    for (int i = 0; i < n; ++i) {
//      for (int j = 0; j < m; ++j) {
//        if(arr[i][j] == '#') continue;
//        if(i)
//          dp[i][j] = add(dp[i][j], dp[i-1][j]);
//        if(j)
//          dp[i][j] = add(dp[i][j], dp[i][j-1]);
//      }
//    }
//    cout << (dp[n-1][m-1] != ans) << endl;
//  }
  return 0;
}