Universal Cup Judging System

Universal Cup

Time Limit: 4.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

Consider a tree with $n$ nodes, where node $1$ is the root. Each node $i$ in the tree has a weight $p_i$, such that $p_1, p_2, \dots, p_n$ is a permutation.

Let $L_{u,v}$ denote the maximum weight of any node on the simple path from node $u$ to node $v$ (including $u$ and $v$). Let $\operatorname{subtree}(i)$ represent the set of nodes that make up the subtree of node $i$. We define $S_i = \{u \mid u \in \operatorname{subtree}(i) \land L_{u,i} \le p_i\}$, which is the set of nodes that can be reached starting from $i$, only moving to children and only passing through nodes with weights less than or equal to $p_i$. Let $f_i = |S_i|$, which represents the size of the set of nodes.

We define the value of a node as $q_{f_i}$, where $q$ is a given value sequence $q_1, q_2, \dots, q_n$.

Now we want to compute, for each pair $(i, j)$, the sum of the values of node $i$ for all weight configurations that satisfy $p_i = j$. Since the answer may be very large, please output the result modulo $10^9 + 7$.

Input

The first line contains one integer $n$ ($1 \le n \le 700$), representing the number of nodes in the tree.

The next line contains $n$ integers, representing $q_1, q_2, \dots, q_n$ ($0 \le q_i < 10^9 + 7$).

The following $n - 1$ lines each contain two integers $u, v$, representing an edge in the tree.

Output

Output $n$ lines, each containing $n$ integers. The $i$-th line and the $j$-th integer represent the corresponding answer modulo $10^9 + 7$.

Examples

Input 1

3
1 1 2
1 2
2 3

Output 1

2 2 4
2 2 2
2 2 2

Input 2

4
1 2 3 4
1 2
2 3
1 4

Output 2

6 10 16 24
6 8 10 12
6 6 6 6
6 6 6 6

Input 3

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

Output 3

2160 3120 3168 2952 2160 3720 6480
2160 2640 2640 2412 2208 2280 2880
2160 2640 3120 3600 4080 4560 5040
2160 3120 3648 3744 3408 2640 1440
2160 2160 2160 2160 2160 2160 2160
2160 2160 2160 2160 2160 2160 2160
2160 2160 2160 2160 2160 2160 2160

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1702EditorialOpenAnother SolutionMilmonView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.