This page is still under construction.

Parts of this page are still being built. What you see may change.

Magic Rectangle

Time limit2sMemory limit128 MB

Summary
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 33 rows and NN columns. You want to write each of the integers from 11 to 3N3N 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 10000071000007.

Input

The first line contains a natural number NN (1≤N≤2001 \le N \le 200).

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 NN integers ai,ja_{i,j} (0≤ai,j≤3N0 \le a_{i,j} \le 3N). A value ai,j=0a_{i,j} = 0 means the corresponding cell is not yet fixed; otherwise the cell already holds the value ai,ja_{i,j}.

Output

Print the number of valid ways to fill the rectangle, modulo 10000071000007, on a single line.

Examples1

  1. Example 1

    Input
    2
    0 0
    0 0
    0 0
    
    Expected output
    5