I remember what someone said to me a long time ago, "The path you choose, even if you walk on your knees, you have to finish it." Friends, although the world is getting more and more impetuous, as long as we can persist in our efforts for the sake of our once pure dreams and moving motives, we can remain true to ourselves all the way, no matter what others do.
As a retired contestant, Colin hopes that all participants will always have a young heart and an eternal love for algorithms and competitive programming.
Colin has a tree $$$T$$$ with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Vertex $$$1$$$ is the root.
Vertex $$$u$$$ is an ancestor of vertex $$$v$$$ if and only if $$$u$$$ lies on the path from $$$v$$$ to the root. In particular, every vertex is its own ancestor.
Let $$$\operatorname{subtree}_u$$$ be the set of all vertices $$$v$$$ such that $$$u$$$ is an ancestor of $$$v$$$.
Colin and Eva assign two integer weights, $$$c_u$$$ and $$$e_u$$$, to every vertex $$$u$$$. Define
$$$$$$ \mathit{ans}_u=\left(\sum_{x\in \operatorname{subtree}_u}\sum_{y\in \operatorname{subtree}_u} \min\{|c_x-c_y|,|e_x-e_y|\}\right)\bmod (10^9+7). $$$$$$
The two sums range over ordered pairs $$$(x,y)$$$; consequently, pairs with $$$x\ne y$$$ are counted in both orders. Calculate $$$\mathit{ans}_u$$$ for every vertex $$$u$$$.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5 \times 10^5$$$), the number of vertices.
Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1\le u_i,v_i\le n$$$, $$$u_i\ne v_i$$$), denoting an edge between vertices $$$u_i$$$ and $$$v_i$$$. The given edges are guaranteed to form a tree.
Each of the next $$$n$$$ lines contains two integers $$$c_i$$$ and $$$e_i$$$ ($$$1\le c_i,e_i\le 10^9$$$), the two weights of vertex $$$i$$$.
Output $$$n$$$ lines.
The $$$i$$$-th line must contain the integer $$$\mathit{ans}_i$$$.
5 1 2 1 3 2 4 2 5 9 5 2 8 7 1 4 3 6 6
44 12 0 0 0
| Name |
|---|


