#P3244. [HNOI2015] 落忆枫音
[HNOI2015] 落忆枫音
Description
Assume there are acupoints on a maple leaf, numbered . Several directed veins connect these acupoints.
The acupoints and veins form a directed acyclic graph—called the vein graph (for example, Figure ). The numbering of the acupoints ensures that acupoint has no incoming veins from other acupoints, that is, acupoint only has outgoing veins. As the story above suggests, this directed acyclic graph contains a tree-shaped subgraph: a tree rooted at acupoint that spans all acupoints—called a vein-tree (for example, the trees in Figure and Figure are both vein-trees of the vein graph given by Figure ).
Note that there may be multiple vein-tree schemes in the vein graph. For example, Figure and Figure are two different vein-tree schemes for the vein graph in Figure .

A formal definition of a vein-tree is as follows: a vein-tree rooted at acupoint consists of all acupoints and veins. There are no cycles in the vein-tree, nor self-loops. Moreover, for every acupoint on the leaf, there exists a unique vein path contained in the vein-tree such that starting from acupoint and following this path you can reach acupoint .
Now we add one new vein, different from all existing veins, to the vein graph (note: veins connecting the same two acupoints but in opposite directions are considered different. For example, the vein from acupoint to is different from the vein from to . Therefore, in Figure , you cannot add a vein from to , but you may add a vein from to ). This new vein may be a self-loop (for example, in Figure 1, a vein from to can be added).
After adding this new vein, cycles formed by veins may appear in the new vein graph. Please compute the number of vein-tree schemes rooted at acupoint in the new vein graph after adding this vein.
Since the number of schemes may be very large, output it modulo .
Input Format
The first line contains four integers , representing the number of acupoints on the leaf, the number of veins, and that the vein to be added goes from acupoint to acupoint .
The next lines each contain two integers separated by a space, representing one existing vein.
In line , the two integers are and , meaning the -th vein goes from acupoint to acupoint .
Output Format
Output one line: the number of vein-tree schemes rooted at acupoint on the leaf after adding the vein from to , modulo .
4 4 4 3
1 2
1 3
2 4
3 2
3
Hint
For all testdata, it is guaranteed that:
- .
- .
- .
Fixed by Starrykiller.
Translated by ChatGPT 5
京公网安备 11011102002149号