Forever Young
题号:NC252837
时间限制:C/C++/Rust/Pascal 7秒,其他语言14秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
Special Judge, 64bit IO Format: %lld

题目描述

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.

Given a tree T consisting of n vertices (indexed from 1 to n). Vertex 1 is the root.

We define that vertex u is an ancestor of vertex v if and only if u is on the path from v to the root.

Then we define subtree_u as the set of all vertices v satisfies that u is an ancestor of v .

Now Colin and Eva give each node u two integer weights c_u,e_u , then we define :

ans_u=\sum_{x\in subtree_u}\sum_{y\in subtree_u} \min\{|c_x-c_y|,|e_x-e_y|\} \mod 10^9+7

Please calculate ans_u for each vertex u\in [1, n] .

输入描述:

The first line contains a single integer n\text{ } (1 \le n \le 5 \times 10^5) , representing the number of vertices.

For the following n - 1 lines, each line contains two integers u_i, v_i \text{ } (1 \le u_i , v_i \le n, u_i \neq v_i) , representing that there is an edge connecting vertex u_i and vertex v_i . It's guaranteed that the given edges form a tree.

For the following n lines, the i-th line contains two integers c_i,e_i \text{ } (1 \le c_i, e_i\le 10^9) , representing the two weights of vertex i.

输出描述:

Output n lines.

For i-th line output a single integer ans_i .
示例1

输入

复制
5
1 2
1 3
2 4
2 5
9 5
2 8
7 1
4 3
6 6

输出

复制
44
12
0
0
0