1+ package main .java .videos ;
2+
3+ import main .java .InputReader ;
4+
5+ import java .math .BigInteger ;
6+ import java .util .Arrays ;
7+
8+ public class GiantChessBoard {
9+ private static long mod = (long ) (1e9 + 7 );
10+ private static long [] fact = generateFactorials (1000000 );
11+ private static long [] invFact = generateReverseFactorials (1000000 );
12+
13+ public static void main (final String [] args ) {
14+ final InputReader in = new InputReader (System .in );
15+ final int n = in .readInt ();
16+ final int m = in .readInt ();
17+ final int k = in .readInt ();
18+ final Point point [] = new Point [k ];
19+ for (int i = 0 ; i < k ; i ++) {
20+ point [i ] = new Point (in .readInt (), in .readInt ());
21+ }
22+ Arrays .sort (point );
23+ final int [] x = new int [k + 1 ];
24+ final int [] y = new int [k + 1 ];
25+ for (int i = 0 ; i < k ; i ++) {
26+ x [i ] = point [i ].x ;
27+ y [i ] = point [i ].y ;
28+ }
29+ x [k ] = n ;
30+ y [k ] = m ;
31+ final long [] dp = new long [k + 1 ];
32+ for (int i = 0 ; i <= k ; i ++) {
33+ dp [i ] = comb (x [i ] - 1 , x [i ] - 1 + y [i ] - 1 );
34+ for (int j = 0 ; j < i ; j ++) {
35+ if (y [j ] <= y [i ]) {
36+ dp [i ] -= dp [j ] * comb (x [i ] - x [j ], x [i ] - x [j ] + y [i ] - y [j ]) % mod ;
37+ }
38+ }
39+ dp [i ] = ((dp [i ] % mod ) + mod ) % mod ;
40+ }
41+ long result = dp [k ];
42+ System .out .println (result );
43+ }
44+
45+ private static long comb (final int m , final int n ) {
46+ return (((fact [n ] * invFact [m ]) % mod ) * invFact [n - m ]) % mod ;
47+ }
48+
49+ public static long [] generateFactorials (int count ) {
50+ long [] result = new long [count ];
51+ result [0 ] = 1 ;
52+ for (int i = 1 ; i < count ; i ++) {
53+ result [i ] = (result [i - 1 ] * i ) % mod ;
54+ }
55+ return result ;
56+ }
57+
58+ public static long [] generateReverseFactorials (int upTo ) {
59+ final long [] reverseFactorials = new long [upTo ];
60+ reverseFactorials [0 ] = reverseFactorials [1 ] = 1 ;
61+ final BigInteger BIG_MOD = BigInteger .valueOf (mod );
62+ for (int i = 1 ; i < upTo ; i ++) {
63+ reverseFactorials [i ] = (BigInteger .valueOf (i ).modInverse (BIG_MOD ).longValue () * reverseFactorials [i - 1 ]) % mod ;
64+ }
65+ return reverseFactorials ;
66+ }
67+ }
68+
69+ class Point implements Comparable <Point > {
70+ final int x , y ;
71+
72+ Point (final int x , final int y ) {
73+ this .x = x ;
74+ this .y = y ;
75+ }
76+
77+ @ Override
78+ public int compareTo (final Point other ) {
79+ if (x < other .x ) {
80+ return -1 ;
81+ } else if (x > other .x ) {
82+ return 1 ;
83+ }
84+ return this .y - other .y ;
85+ }
86+ }
0 commit comments