Under construction

Still being built. Things may change

Daegu Ilgwa Science High School

Time limit1sMemory limit1024 MB

Summary
Count monotone lattice paths in d dimensions from (1,...,1) to (N,...,N) that visit P given points in order, modulo 1e9+7.
Level

Medium6 of 10

Topics
Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

Daegu Ilgwa Science High School is a high school in Daegu Metropolitan City, located in dd-dimensional space. Graduating seniors must move out of their dormitories after the college entrance exam and commute to school. Seojin is a third-year student, so Seojin is worried about the commute and decided to look for a route. The walk from Seojin's home to the school must follow a special rule.

If Seojin is at (p1,p2,…,pd−1,pd)(p_1, p_2, \ldots, p_{d-1}, p_d), Seojin can move to one of these positions: (p1+1,p2,…,pd−1,pd)(p_1+1, p_2, \ldots, p_{d-1}, p_d), (p1,p2+1,…,pd−1,pd)(p_1, p_2+1, \ldots, p_{d-1}, p_d), …\ldots, (p1,p2,…,pd−1+1,pd)(p_1, p_2, \ldots, p_{d-1}+1, p_d), (p1,p2,…,pd−1,pd+1)(p_1, p_2, \ldots, p_{d-1}, p_d+1). On the way from home to school, Seojin found PP points that must be passed. These points must be passed in the given order. No point is repeated, and no point is at the same position as Seojin's home or the school.

Seojin's home is at (a1,a2,…,ad−1,ad)(a_1, a_2, \ldots, a_{d-1}, a_d), and the school is at (b1,b2,…,bd−1,bd)(b_1, b_2, \ldots, b_{d-1}, b_d). Here, ai=1a_i = 1 and bi=Nb_i = N for every 1≤i≤d1 \le i \le d. Find the number of ways to move from home to the school, modulo 109+710^9+7.

Input

The first line contains NN, dd, and PP, separated by spaces. From the second line on, the PP lines each contain the coordinates of a point, qi,1,qi,2,…,qi,d−1,qi,dq_{i,1}, q_{i,2}, \ldots, q_{i,d-1}, q_{i,d}, separated by spaces.

Output

Print the answer on the first line.

Constraints

  • 0≤P≤10000 \le P \le 1000
  • 2≤N2 \le N
  • 1≤d1 \le d
  • N×d≤106N \times d \le 10^6
  • d×P≤106d \times P \le 10^6
  • 1≤qi,j≤N1 \le q_{i,j} \le N (1≤i≤P1 \le i \le P, 1≤j≤d1 \le j \le d)
  • qi,j≤qi+1,jq_{i,j} \le q_{i+1,j} (1≤i<P1 \le i < P, 1≤j≤d1 \le j \le d)
  • All numbers in the input are integers.

Examples1

  1. Example 1

    Input
    4 2 1
    2 3
    
    Expected output
    9