#P13566. 「CZOI-R5」青蛙的旅行
「CZOI-R5」青蛙的旅行
Background
Little L is a frog, and he is now preparing to travel in City A.
Problem Description
City A is an matrix. There is a given number . There is also a variable , initially . Let denote the cell in row and column .
In this matrix, there are special points. The -th one is at with type (). If , it has an additional attribute . It is guaranteed that there do not exist such that and .
Little L starts at . He can perform any number of one of the following jump operations until he reaches . Suppose he is currently at :
- Choose an such that , and there does not exist such that is a type special point. Then jump to .
- Choose an such that , and there does not exist such that is a type special point. Then jump to .
- Choose an such that , and there does not exist such that is a type special point. Then jump to .
After each jump, suppose it lands at . If is the -th special point, then:
- If , then .
- If , let .
If in some plan, at any moment , or at any moment is not inside the matrix, then this plan is invalid.
Ask for the number of valid plans to reach , modulo . Two plans are different if and only if the sequences of visited each time are different.
Input Format
The first line contains integers .
The next lines each start with an integer , then:
- If , input integers , representing a type special point.
- If , input integers , representing a type special point.
Output Format
Output one integer on the first line, the answer.
3 3 1 2
1 1 3
2 2 3 1
15
3 3 1 0
22
Hint
[Sample Explanation #1]
Note: Each point below represents a cell. A red arrow is one jump, and the tail of the arrow is . Yellow points are special points with . Green points are special points with .
The following plans are valid:

The following plans are invalid, because in these plans, after Little L reaches , :

The following plans are invalid, because in these plans, Little L jumps over a special point with :

[Sample Explanation #2]
Since there are no special points, in sample explanation #1, the valid plans shown and the invalid plans are all valid in sample #2, so the answer is .
[Constraints]
This problem uses bundled testdata.
- Subtask #1 (): .
- Subtask #2 (): .
- Subtask #3 (): .
- Subtask #4 (): no additional constraints.
For of the data, , , , , , .
It is guaranteed that no two pairs are the same, and it is guaranteed that and .
Translated by ChatGPT 5