PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000096. Nhảy ô của đàn bò (Cow Hopscotch)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhảy ô của đàn bò (Cow Hopscotch)

Cow Hopscotch

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Cow Hopscotch takes place on a grid of RR rows and CC columns. Cell (i,j)(i,j) has an integer label ai,ja_{i,j} between 11 and KK. A cow starts at the top-left cell (1,1)(1,1) and wants to reach the bottom-right cell (R,C)(R,C).

From cell (i,j)(i,j), the cow may jump directly to cell (x,y)(x,y) if all three conditions hold:

  • The destination is on a strictly lower row: x>ix>i.
  • The destination is in a strictly later column: y>jy>j.
  • The two cells have different labels: ax,y≠ai,ja_{x,y}\ne a_{i,j}.

A jump may skip any number of rows or columns; adjacent cells are not required. Two jump sequences are distinct if they visit different sequences of cells.

Count the ways to reach (R,C)(R,C), modulo 109+710^9+7.

Input

The first line contains integers R,C,KR,C,K. Each of the next RR lines contains exactly CC integers; the jj-th value on the ii-th grid line is ai,ja_{i,j}.

Output

Print one integer between 00 and 109+610^9+6: the number of valid jump sequences modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 2≤R,C≤72\le R,C\le7; 1≤K≤RC1\le K\le RC; 1≤ai,j≤K1\le a_{i,j}\le K for every cell.
  • Subtask 2 (30 points): 2≤R,C≤702\le R,C\le70; 1≤K≤RC1\le K\le RC; 1≤ai,j≤K1\le a_{i,j}\le K for every cell.
  • Subtask 3 (50 points): 2≤R,C≤7502\le R,C\le750; 1≤K≤RC1\le K\le RC; 1≤ai,j≤K1\le a_{i,j}\le K for every cell.

Examples

Example 1

Input

4 4 4
1 1 1 1
1 3 2 1
1 2 4 1
1 1 1 1

Output

5

Explanation

The start (1,1)(1,1) and destination (4,4)(4,4) both have label 11, so a direct jump is invalid. The reachable intermediate cells include (2,2)(2,2) with label 33, (2,3)(2,3) with label 22, (3,2)(3,2) with label 22, and (3,3)(3,3) with label 44. There are five valid complete jump sequences: via (2,2)(2,2), via (2,3)(2,3), via (3,2)(3,2), via (3,3)(3,3), and via (2,2)(2,2) followed by (3,3)(3,3). Therefore, the output is 55.

Example 2

Input

2 2 2
1 1
1 2

Output

1

Explanation

The direct jump from (1,1)(1,1) to (2,2)(2,2) moves strictly down and right and changes the label from 11 to 22. It is the only possible path to the destination, giving answer 11.