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: Time Limit Exceeded)

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

using i64 = int64_t;

#define rep(i, x, y) for (i64 i = i64(x), i##_max_for_repmacro = i64(y); i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

template <typename T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

i64 extgcd(i64 a, i64 b, i64 &x, i64 &y) {
    i64 d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1;
        y = 0;
    }
    return d;
}

i64 mod_inverse(i64 a, i64 m) {
    i64 x, y;
    if (extgcd(a, m, x, y) == 1) return (x + m) % m;
    return 0;
}

vector<i64> fact;
void init_fact(i64 n, i64 m) {
    fact.resize(n + 1);
    fact[0] = fact[1] = 1;
    for (i64 i = 2; i <= n; ++i) fact[i] = (i * fact[i - 1]) % m;
}

//階乗の表をinit_factであらかじめ作っておいた場合はO(log_p(n)).作らない場合はO(p*log_p(n))
i64 mod_fact(i64 n, i64 p, i64 &e) {
    e = 0;
    if (n == 0) return 1;

    i64 res = mod_fact(n / p, p, e);
    e += n / p;

    if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
    return res * fact[n % p] % p;
}

i64 mod_comb(i64 n, i64 k, i64 p) {
    if (n < 0 or k < 0 or n < k) return 0;
    i64 num=fact[n]*mod_inverse(fact[n-k],p)%p,den=fact[k];
    return num * mod_inverse(den, p) % p;
}

void solve() {
    const i64 mod = 1'000'000'007;

    const i64 maxhw=200010;
    init_fact(maxhw,mod);

    i64 H,W,N;
    cin >> H >> W >> N;
    vector<i64> r(N),c(N);
    rep(i,0,N){
        cin >> r[i] >> c[i];
        --r[i];
        --c[i];
    }

    vector<pair<i64,i64>> rc(N);
    rep(i,0,N){
        rc[i]=make_pair(r[i],c[i]);
    }
    sort(begin(rc),end(rc));

    const i64 maxn=3001;
    static i64 dp[maxn][2]; // (今何番目の壁まで見たか, 今までに通過した壁の個数の偶奇) -> 今の壁へ移動する経路の個数
    fill_n((i64*)dp, maxn*2, 0);

    auto count_path = [&](i64 h,i64 w){
        return mod_comb(h+w,h,mod);
    };

    i64 ans=count_path(H-1,W-1);
    rep(i,0,N){
        i64 row=rc[i].first,col=rc[i].second;
        dp[i][1]+=count_path(row,col);
        rep(j,0,i){
            i64 dist_row=row-rc[j].first,dist_col=col-rc[j].second;
            if(dist_row<0 or dist_col<0) continue;
            rep(k,0,2){
                dp[i][(k+1)%2]+=dp[j][k]*count_path(dist_row,dist_col)%mod;
                dp[i][(k+1)%2]%=mod;
            }
        }

        // i番目の壁でfinish
        ans+=dp[i][0]*count_path((H-1)-row,(W-1)-col)%mod;
        ans%=mod;

        ans-=dp[i][1]*count_path((H-1)-row,(W-1)-col)%mod;
        ans%=mod;
        ans+=mod;
        ans%=mod;
    }

    cout << ans << endl;
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}