#P5359. [SDOI2019] 染色

[SDOI2019] 染色

Description

Given a 2×n2 \times n grid graph. Some vertices already have known colors, and the remaining vertices are not colored yet.

A valid coloring does not allow adjacent vertices to have the same color.

There are cc different colors in total, numbered from 11 to cc.

How many valid colorings are there for the uncolored vertices?

Input Format

The first line contains two integers nn and cc, describing the size of the grid graph and the total number of colors.

Then there are two lines, each containing nn integers: if it is 00, it means the corresponding vertex is uncolored; otherwise, it must be an integer from 11 to cc, meaning the vertex has already been colored with a certain color.

Output Format

Output one integer: the total number of valid colorings modulo 109+910^9+9.

3 5
1 0 1
0 0 0
172
5 7
1 0 0 0 2
0 0 3 0 0
116370
10 13
0 2 0 0 1 0 2 0 0 3
0 1 0 1 0 0 0 0 4 0
770175525

Hint

Subtask 11 (4444 points): 1n100001 \le n \le 10000 and 5c100005 \le c \le 10000; there is no column with 22 colored vertices; all colored vertices are in the first row; both the first column and the last column have a colored vertex.

Subtask 22 (3232 points): 1n100001 \le n \le 10000 and 5c100005 \le c \le 10000; there is no column with 22 colored vertices; both the first column and the last column have a colored vertex.

Subtask 33 (1212 points): 1n100001 \le n \le 10000 and 5c100005 \le c \le 10000; both the first column and the last column have a colored vertex.

Subtask 44 (88 points): 1n100001 \le n \le 10000 and 5c100005 \le c \le 10000.

Subtask 55 (44 points): 1n1000001 \le n \le 100000 and 5c1000005 \le c \le 100000.

Translated by ChatGPT 5