Problems
Farmer John once painted a rectangular grid on the ground of his pasture. In each cell, he painted either a
Over time, the paint faded, and Farmer John now remembers the values of only some cells. However, Farmer John does remember one important fact about the original painting:
In every row and every column, the sum of the values in any contiguous subsegment was always between
As an example, consider the row
However, the row
[ - ] + + - sum = -1
[ - + ] + - sum = 0
[ - + + ] - sum = +1
[ - + + - ] sum = 0
- [ + ] + - sum = +1
- [ + + ] - sum = +2
- [ + + - ] sum = +1
- + [ + ] - sum = +1
- + [ + - ] sum = 0
- + + [ - ] sum = -1
Count the number of different grids consistent with Farmer John's memory.
Input
The first line contains
The first line contains
Then following
Additionally, it is guaranteed that neither the sum of
Output
For each test, output the number of grids on a separate line.
Subtask
| # | Score | Condition |
|---|---|---|
| #1 | 10 | |
| #2 | 10 | |
| #3 | 20 | |
| #4 | 20 | |
| #5 | 40 | No additional constraints. |
Example #1
2
1 3 3
+ 1 3
+ 1 1
- 1 2
1 3 3
+ 1 1
+ 1 3
+ 1 2
1
0
Example #2
1
2 2 0
7
Here are the seven grids:
++
++
++
+-
++
-+
+-
++
+-
-+
-+
++
-+
+-