Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/kombinatorika-tosiqli-maydon

Large field with obstacles

Hard 2000 ms 128 MB 95%
Share

On a grid of $n$ rows and $m$ columns, $k$ cells are occupied by obstacles. In how many ways can one get from the top-left cell $(1; 1)$ to the bottom-right cell $(n; m)$, moving only right or down and never stepping on an obstacle? Print the answer modulo $10^{9} + 7$.

A large grid, a few obstacles and one of the right-down paths

Input

The first line contains three integers: $n$, $m$ and $k$. Each of the next $k$ lines contains two positive integers: the row $r_{i}$ and the column $c_{i}$ of an obstacle cell.

Output

One integer — the number of paths modulo $10^{9} + 7$.

Note

$1 \le n, m \le 10^{5}$
$0 \le k \le 1000$
the obstacles are in different cells, cells $(1; 1)$ and $(n; m)$ are free

Examples

Input #1
3 4 2
2 2
2 3
Output #1
2
Input #2
2 2 1
1 2
Output #2
1

Sign in to submit a solution

Signing up is free and takes a minute.