#P9208. 虚人「无」
虚人「无」
Background
A phoenix that is not beautiful at all.
Those sharp claws are stained with the innocent blood.
Problem Description
You are given a pair sequence and a rooted tree with root . The weight of node is .
- Define the value of a non-root node as the product of all in its subtree times the product of all outside its subtree.
- Define the value of the root node as the product of all in its subtree.
Formally, if is not the root, then the value of is:
$$f_u=\prod\limits_{v\in \operatorname{substree}(u)} c_v\times \prod\limits_{v\notin \operatorname{substree}(u)} v_v$$Otherwise, its value is:
Find the sum of the values of all nodes in the whole tree, and output the answer modulo . Note: it is guaranteed that is a prime.
Input Format
The first line contains two positive integers .
The next lines each contain two integers , indicating that there is an edge between and .
The next line contains integers, representing .
The next line contains integers, representing .
Output Format
Output one integer, representing the result of the answer modulo .
3 998244853
1 2
1 3
2 1 2
1 2 2
10
5 998244353
1 2
1 3
1 4
4 5
5 5 5 2 3
6 6 1 5 3
4656
Hint
Sample Explanation

(The image is incorrect. The weights should be swapped.)
Constraints and Notes
For of the testdata, , .
Translated by ChatGPT 5