QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 256 MB Total points: 100 Difficulty: [show]

#1061. Coloring

Statistics

You are given a $2 \times n$ grid graph. Some nodes have known colors, while the remaining nodes are uncolored. A valid coloring scheme does not allow any two adjacent nodes to have the same color.

There are $c$ different colors available, labeled from $1$ to $c$. How many valid coloring schemes are there for the uncolored nodes?

Input

The first line contains two integers $n$ and $c$, representing the size of the grid and the total number of colors, respectively.

The next two lines each contain $n$ integers. If an integer is $0$, it means the corresponding node is uncolored; otherwise, it is an integer from $1$ to $c$, representing the color already assigned to that node.

Output

Output a single integer representing the total number of valid coloring schemes modulo $10^9 + 9$.

Examples

Input 1

3 5
1 0 1
0 0 0

Output 1

172

Input 2

5 7
1 0 0 0 2
0 0 3 0 0

Output 2

116370

Input 3

10 13
0 2 0 0 1 0 2 0 0 3
0 1 0 1 0 0 0 0 4 0

Output 3

770175525

Subtasks

  • Subtask 1 (44 points): $1 \le n \le 10000$ and $5 \le c \le 10000$; no column has 2 colored nodes; all colored nodes are in the first row; the first and last columns both have at least one colored node.
  • Subtask 2 (32 points): $1 \le n \le 10000$ and $5 \le c \le 10000$; no column has 2 colored nodes; the first and last columns both have at least one colored node.
  • Subtask 3 (12 points): $1 \le n \le 10000$ and $5 \le c \le 10000$; the first and last columns both have at least one colored node.
  • Subtask 4 (8 points): $1 \le n \le 10000$ and $5 \le c \le 10000$.
  • Subtask 5 (4 points): $1 \le n \le 100000$ and $5 \le c \le 100000$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.