Gerald and Giant Chess
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.
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 ≤ 105, 1 ≤ n ≤ 2000). Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ 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.
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 109 + 7.
def sol(h, w, n, blacks):
    blacks.sort()
    blacks.append((h - 1, w - 1))
    p = 10 ** 9 + 7
    dp1 = [0] * (h + w + 1)
    dp2 = [0] * (h + w + 1)
    cur = 1
    for i in range(h + w + 1):
        dp1[i] = cur
        cur = cur * (i + 1) % p
    cur = pow(dp1[-1], -1, p)
    for j in range(h + w, -1, -1):
        dp2[j] = cur
        cur = cur * j % p

    def f(a, b):
        if a < b:
            return f(b, a)
        return dp1[a + b] * dp2[a] * dp2[b] % p
    memory = [0] * (n + 1)
    for i in range(n + 1):
        memory[i] = f(blacks[i][0], blacks[i][1])
    for i in range(1, n + 1):
        for j in range(i - 1, -1, -1):
            ydif = blacks[i][1] - blacks[j][1]
            if ydif >= 0:
                memory[i] = (memory[i] - memory[j] * f(blacks[i][0] - blacks[j][0], ydif)) % p
    return memory[-1]
(h, w, n) = list(map(int, input().split()))
blacks = []
for _ in range(n):
    (x, y) = list(map(int, input().split()))
    blacks.append((x - 1, y - 1))
print(sol(h, w, n, blacks))
import math
import sys
readline = sys.stdin.readline

def Extended_Euclid(n, m):
    stack = []
    while m:
        stack.append((n, m))
        (n, m) = (m, n % m)
    if n >= 0:
        (x, y) = (1, 0)
    else:
        (x, y) = (-1, 0)
    for i in range(len(stack) - 1, -1, -1):
        (n, m) = stack[i]
        (x, y) = (y, x - n // m * y)
    return (x, y)

class MOD:

    def __init__(self, p, e=1):
        self.p = p
        self.e = e
        self.mod = self.p ** self.e

    def Pow(self, a, n):
        a %= self.mod
        if n >= 0:
            return pow(a, n, self.mod)
        else:
            assert math.gcd(a, self.mod) == 1
            x = Extended_Euclid(a, self.mod)[0]
            return pow(x, -n, self.mod)

    def Build_Fact(self, N):
        assert N >= 0
        self.factorial = [1]
        self.cnt = [0] * (N + 1)
        for i in range(1, N + 1):
            ii = i
            self.cnt[i] = self.cnt[i - 1]
            while ii % self.p == 0:
                ii //= self.p
                self.cnt[i] += 1
            self.factorial.append(self.factorial[-1] * ii % self.mod)
        self.factorial_inve = [None] * (N + 1)
        self.factorial_inve[-1] = self.Pow(self.factorial[-1], -1)
        for i in range(N - 1, -1, -1):
            ii = i + 1
            while ii % self.p == 0:
                ii //= self.p
            self.factorial_inve[i] = self.factorial_inve[i + 1] * ii % self.mod

    def Fact(self, N):
        return self.factorial[N] * pow(self.p, self.cnt[N], self.mod) % self.mod

    def Fact_Inve(self, N):
        if self.cnt[N]:
            return None
        return self.factorial_inve[N]

    def Comb(self, N, K, divisible_count=False):
        if K < 0 or K > N:
            return 0
        retu = self.factorial[N] * self.factorial_inve[K] * self.factorial_inve[N - K] % self.mod
        cnt = self.cnt[N] - self.cnt[N - K] - self.cnt[K]
        if divisible_count:
            return (retu, cnt)
        else:
            retu *= pow(self.p, cnt, self.mod)
            retu %= self.mod
            return retu
(H, W, N) = map(int, readline().split())
RC = [tuple(map(int, readline().split())) for i in range(N)]
RC.sort()
mod = 10 ** 9 + 7
MD = MOD(mod)
MD.Build_Fact(2 * 10 ** 5)
dp = [0] * N
ans = MD.Comb(H + W - 2, H - 1)
for i in range(N):
    (r, c) = RC[i]
    dp[i] = MD.Comb(r + c - 2, r - 1)
    for j in range(i):
        dr = r - RC[j][0]
        dc = c - RC[j][1]
        dp[i] -= dp[j] * MD.Comb(dr + dc, dr)
        dp[i] %= mod
for i in range(N):
    (r, c) = RC[i]
    dr = H - r
    dc = W - c
    dp[i] *= MD.Comb(dr + dc, dr)
    dp[i] %= mod
ans -= sum(dp)
ans %= mod
print(ans)
from sys import stdin, stdout
import collections, heapq, bisect, math
input = stdin.readline

def rint():
    return int(input())

def rstr():
    return input().strip()

def rlstr():
    return list(input().strip().split())

def rlint():
    return list(map(int, input().split()))
(f, inv) = ([1], [1, 1])
mod = 1000000007
for i in range(1, 200001):
    f.append(f[i - 1] * i % mod)
for i in range(2, 200001):
    inv.append(-(mod // i) * inv[mod % i] % mod)
for i in range(2, 200001):
    inv[i] *= inv[i - 1]
    inv[i] %= mod

def c(n, k):
    return f[n] * inv[k] * inv[n - k] % mod

def main():
    (h, w, n) = rlint()
    arr = []
    for _ in range(n):
        arr.append(rlint())
    arr.append([h, w])
    arr.sort()
    dp = [0] * (n + 1)
    for i in range(n + 1):
        dp[i] = c(arr[i][0] + arr[i][1] - 2, arr[i][0] - 1)
        for j in range(i):
            if arr[i][0] >= arr[j][0] and arr[i][1] >= arr[j][1]:
                dp[i] -= dp[j] * c(arr[i][0] - arr[j][0] + arr[i][1] - arr[j][1], arr[i][0] - arr[j][0])
                dp[i] %= mod
    print(dp[-1])
if __name__ == '__main__':
    main()