#P8950. [YsOI2022] 道路修建
[YsOI2022] 道路修建
Background
Ysuperman is preparing a template test for the children in his kindergarten. Below is a template problem, and he hopes you can help him verify it.
Problem Description
A place has newly built cities, and it is planned to build directed roads. For the -th road, the start is , the end is , and the construction cost is a positive integer .
However, before construction starts, an emergency happens. You must immediately choose some of these roads to build so that people in every city can reach some set of cities (that is, people in every city can reach at least one of these cities).
You want to know: if these cities are chosen uniformly at random among the cities, what is the expected minimum construction cost.
To avoid fraction input/output, you only need to output the result modulo .
Input Format
The first line contains three non-negative integers , representing the number of cities, the number of planned roads, and the number of cities to be chosen.
The next lines each contain three positive integers , describing a planned directed road.
Output Format
In particular, if there exists a way to choose cities such that no matter how you build roads, there will always be some city that cannot reach any of these cities, output -1.
Otherwise, output one integer in a single line, representing the answer modulo .
3 4 1
1 2 1
2 1 2
2 3 3
3 1 4
5
4 6 2
1 2 1
1 3 3
2 3 2
3 4 5
4 1 4
4 2 6
6
8 16 3
5 6 7
7 2 10
4 6 4
5 7 5
8 4 12
1 3 8
2 3 6
4 1 8
1 7 2
8 3 1
2 5 3
6 4 11
7 3 14
3 8 9
8 1 13
6 7 16
160432162
4 1 3
2 4 1
-1
Hint
Sample 1 Explanation
There are three ways to choose the set of cities:
-
Choose city . Then building roads has the minimum cost, which is .
-
Choose city . Then building roads has the minimum cost, which is .
-
Choose city . Then building roads has the minimum cost, which is .
So the expected minimum cost is .
Sample 2 Explanation
There are ways to choose the set of cities:
-
Choose cities , minimum cost .
-
Choose cities , minimum cost .
-
Choose cities , minimum cost .
-
Choose cities , minimum cost .
-
Choose cities , minimum cost .
-
Choose cities , minimum cost .
So the expected minimum cost is .
Sample 3 Explanation
It is too small to write here, so here is a diagram:

Sample 4 Explanation
When the set of cities is chosen as , city cannot reach any of no matter what, so the answer is .
Constraints
For of the testdata, , .
For of the testdata, , .
For another of the testdata, all are equal.
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For of the testdata, , , , , .
Input Format
Output Format
Hint
Translated by ChatGPT 5