Magic Rectangle
Time limit2sMemory limit128 MB
Count the ways to fill a 3 by N grid with 1 to 3N so each row and column increases, matching the prefilled cells, modulo 1000007.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
You are given a rectangle with rows and columns. You want to write each of the integers from to exactly once into its cells so that the numbers increase along every row from left to right and along every column from top to bottom. In how many ways can this be done?
Some cells may already be filled in. Because the number of ways can be very large, output it modulo .
Input
The first line contains a natural number ().
The next three lines describe the rectangle row by row from top to bottom, and within each row from left to right. Each of these lines contains integers (). A value means the corresponding cell is not yet fixed; otherwise the cell already holds the value .
Output
Print the number of valid ways to fill the rectangle, modulo , on a single line.