This page is still under construction.

Parts of this page are still being built. What you see may change.

Tree Designer Hoseok

Time limit1sMemory limit1024 MB

Summary
Count, modulo 1e9+7, the number of nonempty nondecreasing subsequences of vertex labels along any root-to-leaf path of a rooted tree.
Level

Medium7 of 10

Topics
Tree, DFS, Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

Hoseong, who loves trees more than anything, is a bonsai tree expert. Every tree Hoseong grows consists of vertices and edges. The vertices are numbered 11 through NN, and each edge connects two distinct vertices. The number of vertices is exactly one more than the number of edges, and no cycle exists. The root of the tree is one of the vertices, the one at the lowest height among all vertices. Vertex 1 is always guaranteed to be the root, and a leaf is a vertex with at most one incident edge. A vertex closer to the root sits at a lower height, and one farther away sits at a higher height.

The trees Hoseong grows are special: each vertex has an integer from 00 to 99 written on it. One day Hoseong suddenly wanted to make a Christmas tree, and decided to choose one or more vertices to hang lights on. The vertices must be chosen along the path from the root to some leaf. The chosen lights do not have to be consecutive. The vertices with lights light up, and the numbers written on them shine brightly.

Suppose the leftmost picture is the tree Hoseong has. The middle and right pictures are examples of correct light choices. Reading the numbers on the lit lights from bottom to top in each picture gives {0,0,8}\{0, 0, 8\} and {3,6,3}\{3, 6, 3\}.

If the chosen lights are vertices 1, 7, and 10, the result is the left picture. This is not a valid choice because they were not chosen along the path from the root to some leaf. For the same reason, choosing lights 4, 5, 8, and 9 is also invalid.

After hearing Hoseong's requirements, Tree Designer Hoseok thought of a problem. When lights are hung, and the numbers are read from low height to high height, he wondered how many ways there are to hang the lights so that the sequence is nondecreasing. A nondecreasing sequence is one where each number is not smaller than the previous one. That is, a number equal to the previous one still satisfies nondecreasing order. For example, {0,0,8}\{0, 0, 8\} is nondecreasing, but {3,6,3}\{3, 6, 3\} is not.

Given Hoseong's tree, compute the answer Hoseok wants.

Input

The first line gives the number of vertices NN.

The second line gives the numbers written on vertices 11 through NN, separated by spaces. Each number is a natural number from 00 to 99.

From the third line, N−1N-1 lines follow, each giving the numbers of the two vertices connected by an edge.

The given tree is guaranteed to be a single connected graph.

Output

Print the number of ways to hang the lights modulo 1,000,000,007.

Constraints

  • 1≤N≤100,0001 ≤ N ≤ 100,000

Examples2

  1. Example 1

    Input
    4
    1 1 2 2
    1 2
    3 2
    3 4
    
    Expected output
    15
    
  2. Example 2

    Input
    10
    3 3 2 0 2 9 6 0 8 1
    6 1
    1 3
    3 7
    2 7
    4 9
    4 8
    1 8
    10 8
    8 5
    
    Expected output
    22