#P10219. [省选联考 2024] 虫洞
[省选联考 2024] 虫洞
Problem Description
Country E has cities, numbered from to . To make travel between cities more convenient, the Ministry of Transportation of Country E wants to build some wormholes among the cities. Each wormhole is a directed passage from one city to another. The start and end of a passage are allowed to be the same city, and there may also be multiple wormholes connecting the same pair of cities.
To distinguish the construction time of wormholes, the ministry assigns each wormhole a positive integer label.
We call a wormhole construction plan good if it satisfies the following four conditions:
- There exists a non-negative integer such that each city is the start of exactly wormholes and also the end of exactly wormholes.
- For each city, among the labels of wormholes that start from it, to each appear exactly once.
- For each city, among the labels of wormholes that end at it, to each appear exactly once.
- Pick any city and positive integers . Starting from , first go through a wormhole labeled , then go through a wormhole labeled , and arrive at city . Starting from , first go through a wormhole labeled , then go through a wormhole labeled , and arrive at city . Then it must always hold that .
In particular, the plan that builds no wormholes is also good.
Now, the builder has already built wormholes and assigned them labels , and the current construction plan is good. He wants to additionally build wormholes and assign them labels . He must ensure that all these wormholes still form a good construction plan. He wants to know how many ways there are to build the new wormholes such that the resulting plan of wormholes is good.
Since the answer can be very large, you only need to output it modulo .
Input Format
The first line contains four non-negative integers , where denotes the test point ID. In the samples, means that the Constraints of that sample are the same as those of test point .
In the next lines, each line contains three positive integers , representing a wormhole labeled , with start city and end city .
Output Format
Output one integer, the number of plans modulo .
1 4 1 1
1 2 1
2 1 1
3 4 1
4 3 1
8
Hint
[Sample 1 Explanation]
In this sample, the already-built wormholes with label are . To make the wormholes form a good construction plan, there are possible choices for the newly built wormholes with label :
[Sample 2]
See the attached wormhole2.in/ans.
This sample has , and it satisfies the constraints of test point 2.
[Sample 3]
See the attached wormhole3.in/ans.
This sample has , and it satisfies the constraints of test point 5.
[Sample 4]
See the attached wormhole4.in/ans.
This sample has , and it satisfies the constraints of test point 7.
[Sample 5]
See the attached wormhole5.in/ans.
This sample has , and it satisfies the constraints of test point 9.
[Sample 6]
See the attached wormhole6.in/ans.
This sample has , and it satisfies the constraints of test point 11.
[Sample 7]
See the attached wormhole7.in/ans.
This sample has , and it satisfies the constraints of test point 15.
[Sample 8]
See the attached wormhole8.in/ans.
This sample has , and it satisfies the constraints of test point 17.
[Sample 9]
See the attached wormhole9.in/ans.
This sample has , and it satisfies the constraints of test point 20.
[Sample 10]
See the attached wormhole10.in/ans.
This sample has , and it satisfies the constraints of test point 22.
[Subtasks]
For all test points,
- , , ;
- , ;
- It is guaranteed that the initially built wormholes form a good construction plan.
| Test point ID | |||
|---|---|---|---|
[Hint]
Some test points of this problem have large input sizes, so we recommend using a faster input method.
Translated by ChatGPT 5