#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 {(vi,ci)}\{(v_i,c_i)\} and a rooted tree with root 11. The weight of node ii is (vi,ci)(v_i,c_i).

  • Define the value of a non-root node as the product of all cc in its subtree times the product of all vv outside its subtree.
  • Define the value of the root node as the product of all cc in its subtree.

Formally, if uu is not the root, then the value fuf_u of uu 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 fuf_u is:

fu=v=1ncvf_u=\prod\limits_{v=1}^n c_v

Find the sum of the values of all nodes in the whole tree, and output the answer modulo mm. Note: it is guaranteed that m\bm m is a prime.

Input Format

The first line contains two positive integers n,mn,m.

The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an edge between uu and vv.

The next line contains nn integers, representing c1,2,,nc_{1, 2, \dots, n}.

The next line contains nn integers, representing v1,2,,nv_{1, 2, \dots, n}.

Output Format

Output one integer, representing the result of the answer modulo mm.

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 v,cv,c should be swapped.)

Constraints and Notes

For 100%100\% of the testdata, 1n3×1051\le n\leq 3\times 10^5, 1vi,ci,m1091\leq v_i,c_i,m\leq 10^9.

Translated by ChatGPT 5