How many ways;
We are given a chessboard with squares. Each square contains a number. We start from the top-left square and want to reach the bottom-right square. In each move, we can only move one square up, down, left, or right and only under the condition that the square we move to has a strictly larger number than the square we are currently on. How many different ways can we reach from the top-left square to the bottom-right square?
(We consider two paths to be different if the sequences of coordinates of the squares they pass through are different.)
Input:
The first line of the input will contain two positive integers and , separated by a space: the dimensions of the chessboard. Each of the next lines will contain positive integers separated in pairs by a space: the numbers written on the squares of the chessboard, in the obvious order.
Output:
The output should consist of a single line containing exactly one integer: the number of ways to reach from the top-left square to the bottom-right square. Since this number can be very large, print the remainder of its division (modulo) by .
Constraints:
- The numbers in the squares of the chessboard will not exceed .
- Time Limit: sec.
- Memory Limit: MB.
- For test cases with a total value of , it will be and .
- For test cases with a total value of , it will be and .
Example of Input - Ouput:
input:
5 5
17 23 26 30 31
21 10 19 32 32
24 24 54 34 33
25 32 37 38 35
26 27 80 40 42
output:
6
Explanation of the example:
There are six ways to go from the top-left square to the bottom-right , always moving to squares with progressively larger numbers. One way is along the first row and then the last column: . Another way is: . There are four other ways that you should look for and find.
Comments