Ensure that you are logged in and have the required permissions to access the test.
In a N×M grid, each grid has a value ai,j. Grid (i,j) (i<N) has two extra values li,j,ri,j. The values of each line is not decreasing, that is, for any i,j,k(1≤i≤N,1≤j≤k≤M), ai,j≤ai,k, and li,j≤li,k,ri,j≤ri,k if i<N.
If you are at grid (i,j) (i<N), you can go to (i+1,x), where x≥li,j and x≤ri,j. It's guaranteed li,j≤ri,j and li,j+1≤ri,j+1.
Now you walk from some grid (1,x) and end at some grid (N,y), x and y are decided by yourself. When you pass a grid (i,j), you add ai,j to a result array. You need to count hou many different arrays can be obtained. Two arrays are dirrerent if at least one element is different.
First line contains two integers N,M(1≤N,M≤1000).
The next N lines represent a matrix, the j th integer on the i th line is ai,j.
Similarily, the next N−1 lines represent l, and the last N−1 lines represent r.
It's guaranteed 1≤ai,j,li,j,ri,j≤M.
One integer represents the answer, modulo 109+7.
There are 6 different paths, but two of them have the same result array, so the answer is 5.
(1,1)−(2,1)−(3,1):[1,1,1]
(1,1)−(2,1)−(3,2):[1,1,3]
(1,2)−(2,2)−(3,1):[2,1,1]
(1,2)−(2,2)−(3,2):[2,1,3]
(1,3)−(2,3)−(3,2):[2,2,3]
(1,3)−(2,3)−(3,3):[2,2,3]