#P5860. 「SWTR-3」Counting Trees
「SWTR-3」Counting Trees
Description
There are vertices, and each vertex has a weight .
Little wants Little to choose some vertices to form a set. Let the set be .
Of course, Little also needs to ensure that these vertices can form a tree, and the degree of is .
- Degree of a vertex: the number of vertices adjacent to it.
Little asks Little how many ways there are that satisfy the conditions.
Little knows he definitely cannot solve this problem, so he asks you for help.
Since the number of ways may be very large, output it modulo .
Input Format
The first line contains an integer .
The second line contains integers .
Output Format
Output one integer in one line, representing the number of valid ways.
3
1 1 1
3
5
1 2 1 3 1
8
8
1 2 1 2 4 1 3 1
44
50
8 1 10 2 2 1 2 1 1 2 5 1 11 6 13 13 10 4 1 13 11 2 2 11 13 10 1 1 4 3 4 2 15 2 2 1 1 2 1 7 14 2 2 4 13 2 7 5 6 10
176873472
Hint
Sample Explanation
-
For sample , it is enough to choose any two vertices among the three vertices. The answer is .
-
For sample , as shown in the figure, there are ways to choose vertices.

Constraints and Notes
This problem uses bundled testdata.
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| None | |||
| Random testdata | |||
| None |
For of the testdata, and .
In , “random testdata” means: for every , it equals with probability , and with probability it is chosen uniformly at random from .
For the first s, the time limit is .
For the th , the time limit is .
For the last s, the time limit is .
For all test points, the memory limit is .
Translated by ChatGPT 5
京公网安备 11011102002149号