Monkey Joe
题号:NC237097
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Monkey Joe likes to eat fruit. As monkey Joe is fond of climbing here and there, rather than eating fruits that are already in the grocery shops, he choose to climb the tree and eat the fruit on the tree.
A tree is a connected graph containing n nodes and n-1 edges.
On every node of the tree, there exists one single fruit. For a fruit on node u, it has a tiredness value of .
Every time Joe eats the fruits, he only travels the simple path from u to v (a simple path is a path that every node and edge is visited only once). When he travels from u to v, he will eat every fruit on the simple path. However, Joe gets very tired of eating all the fruits.
Formally, his tiredness of eating fruit i that lies on the path from u to v is defined as , where denotes the rank of the fruit on node i if you sort all the fruit on the simple path from u to v based on their tiredness values in increasing order. The whole tiredness of path (u, v) is the sum of the tiredness of eating every fruit on the path. It is guaranteed that the tiredness value of each fruit are distinct.
Joe is uncertain which path he would choose to climb, so he would like you to calculate the tiredness sum of all distinct paths of the tree. Since the answer may be very large, you only have to output the answer module

输入描述:

The first line contains one integer n ---the number of nodes on the tree.
The second line contains n integers ()---the tiredness value of every node on the tree.
The i-th of the following n-1 lines contains two integers and () denoting that there exists an edge connecting vertices and . It is guaranteed that the given edges form a tree.

输出描述:

Print a single integer --- the tiredness sum of all paths on the tree module 
示例1

输入

复制
2
1 2
1 2

输出

复制
8
示例2

输入

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

输出

复制
357

备注:

The path from u to v is considered the same as the path from v to u.
Path with the form (u,u) is regarded as a valid simple path.