inputs: I found an interesting problem on https://codeforces.com/problemset/problem/559/C:
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?

The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.


-----Input-----

The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 2000). 

Next n lines contain the description of black cells. The i-th of these lines contains numbers r_{i}, c_{i} (1 ≤ r_{i} ≤ h, 1 ≤ c_{i} ≤ w) — the number of the row and column of the i-th cell.

It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.


-----Output-----

Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 10^9 + 7.


-----Examples-----
Input
3 4 2
2 2
2 3

Output
2

Input
100 100 3
15 16
16 15
99 88

Output
545732279

I tried it in Python, but could not do it. Can you solve it?
targets: def init_factorials(N, mod):
    f = 1
    fac = [1] * N
    for i in range(1, N):
        f *= i
        f %= mod
        fac[i] = f
    return fac

def init_inv(N, mod, fac):
    b = bin(mod-2)[2:][-1::-1]
    ret = 1
    tmp = fac[N]
    if b[0] == '1':
        ret = fac[N]
    for bi in b[1:]:
        tmp *= tmp
        tmp %= mod
        if bi == '1':
            ret *= tmp
            ret %= mod
    inv = [1] * (N + 1)
    inv[N] = ret
    for i in range(N-1, 0, -1):
        ret *= i + 1
        ret %= mod
        inv[i] = ret
    return inv


def f(r, c, mod, fac, inv):
    return (fac[r + c] * inv[r] * inv[c]) % mod


def read_data():
    h, w, n = list(map(int, input().split()))
    blacks = []
    for i in range(n):
        r, c = list(map(int, input().split()))
        blacks.append((r, c))
    return h, w, n, blacks

def solve(h, w, n, blacks):
    mod = 10**9 + 7
    fac = init_factorials(h + w + 10, mod)
    inv = init_inv(h + w + 5, mod, fac)
    ans = (fac[h+w-2]*inv[h-1]*inv[w-1]) % mod
    eb = [(r + c, r, c) for r, c in blacks]
    eb.sort()
    blacks = [(r, c) for rc, r, c in eb]
    g = [f(r-1, c-1, mod, fac, inv) for r, c in blacks]
    hw = h+w
    for i, (r, c) in enumerate(blacks):
        gi = g[i]
        rc = r + c
        ans -= gi*fac[hw-rc]*inv[h-r]*inv[w-c]
        ans %= mod
        for j, (rj, cj) in enumerate(blacks[i+1:], i+1):
            if r <= rj and c <= cj:
                g[j] -= gi*fac[rj+cj-rc]*inv[rj-r]*inv[cj-c]
                g[j] %= mod
    return ans

h, w, n, blacks = read_data()
print(solve(h, w, n, blacks))