Party joke sets

Count distinct joke-type sets from root-connected guest groups with unique values where each subtree below a guest forms consecutive numbers.

Hard8Dynamic programmingTreeIntervalsNo attempts yetTime limit1sMemory limit32 MB

Problem

Petar is throwing a birthday party and wants to invite some of the employees of the company he runs. Petar is the CEO, and since the party is his, he always attends.

The NN employees, Petar included, are numbered 1 to NN, and person ii tells jokes of type ViV_i. Every employee except Petar has exactly one direct supervisor.

Petar is the CEO, so he has number 1 and is the direct or indirect supervisor of every employee.

Everyone at the party, Petar included, must follow these rules.

  • No two people at the party tell the same type of jokes.
  • Person XX cannot be invited if the direct supervisor of XX is not invited.
  • Person XX cannot be invited if the joke types told by XX together with the joke types told by the invited people that XX supervises, directly or indirectly, do not form a set of consecutive numbers.

A set is a set of consecutive numbers when, after sorting it in ascending order, the difference between adjacent elements is exactly 1. For example, {3,1,2}\{3, 1, 2\} and {5,1,2,4,3}\{5, 1, 2, 4, 3\} are such sets.

Petar wants to know how many different sets of joke types he can see at his party under these rules.

Input

The first line contains the integer NN. (1N100001 \le N \le 10000)

The second line contains NN integers V1,V2,,VNV_1, V_2, \dots, V_N, where ViV_i is the type of jokes person ii tells. (1Vi1001 \le V_i \le 100)

Each of the following N1N-1 lines contains two integers AA and BB, meaning that person AA is the direct supervisor of person BB. (1A,BN1 \le A, B \le N)

Output

Print the number of different sets of joke types that comply with the rules.

Note

In the first example the party can show these sets of jokes: {2}\{2\}, {2,3}\{2, 3\}, {2,3,4}\{2, 3, 4\}, {1,2,3,4}\{1, 2, 3, 4\}, {1,2}\{1, 2\}, {1,2,3}\{1, 2, 3\}.

In the second example the only possible sets are {3}\{3\}, {3,4}\{3, 4\}, {3,4,5}\{3, 4, 5\}. The person telling joke 6 cannot come to the party, because then the set of jokes {4,6}\{4, 6\} would not be a set of consecutive numbers.