#P14476. 矩阵绘画

矩阵绘画

Problem Description

Elfin likes painting. One day, she started studying how to paint matrices.

More specifically, Elfin wants to paint an n×nn \times n 0101 matrix. She starts from an all-zero matrix, and each time she can choose one row or one column to paint. If she chooses row ii, she adds 11 to all entries in columns 11 to aia_i of that row. If she chooses column ii, she adds 11 to all entries in rows 11 to bib_i of that column. She will not paint any cell more than once, because then the value in that cell would become greater than 11. Here, {ai}\{a_i\} and {bi}\{b_i\} are two sequences of length nn.

Elfin also has an n×nn \times n draft matrix, whose entries may be 00, 11, or the character ?. If a cell in the draft matrix is 00 or 11, then the corresponding cell in the matrix she paints must be that value. If a cell is ?, then there is no restriction on that cell.

Now Elfin wants to know how many different 0101 matrices she can paint. Two matrices are considered different if and only if there exists at least one cell where their values differ. Since the answer may be very large, output it modulo 109+710^9 + 7.

Input Format

Note: Due to Luogu's judging limitations, when submitting this problem on Luogu, please use standard input/output. Do not use file input/output.

The first line contains a positive integer nn.

The next line contains nn non-negative integers, representing aia_i.

The next line contains nn non-negative integers, representing bib_i.

The next nn lines each contain a string of length nn, representing the given draft matrix.

Output Format

Output one integer, the answer.

3
1 2 3
1 2 3
?11
???
?0?
2
1
0
0
1
0

Hint

Sample Explanation 1

There are 22 possible matrices:

111 011
011 011
001 001

Sample Explanation 2

No matrix satisfies the requirements.

Constraints

All testdata satisfy: 1n50001 \leq n \leq 5000, 0ai,bin0 \leq a_i, b_i \leq n, and each element in the draft matrix {0,1,?}\in \{0, 1, ?\}.

::cute-table{tuack}

Test Point ID nn \leq Special Property
131 \sim 3 88
464 \sim 6 2020
797 \sim 9 100100
101310 \sim 13 500500
141714 \sim 17 50005000 a1=0a_1 = 0
182118 \sim 21 ai,biia_i, b_i \leq i
222522 \sim 25

For each group of test points, the first two test points satisfy: the given matrix contains only question marks.

Translated by ChatGPT 5