Tree Labeling

No attempts yetTime limit1sMemory limit128 MB

Problem

Consider a tree T=(V,E)T = (V, E), where VV is a set of vertices and EE is a set of edges. A vertex joined to vv by an edge is a neighbor of vv in TT, and the set of all neighbors of vv is written N(v)N(v).

A labeling of TT is a one-to-one and onto function f:V{1,2,,V}f : V \to \{1, 2, \dots, |V|\}.

Two labelings ff and gg are equivalent when the following holds: for every vertex uVu \in V there is a vertex vVv \in V such that f(u)=g(v)f(u) = g(v) and {f(u)uN(u)}={g(v)vN(v)}\{f(u') \mid u' \in N(u)\} = \{g(v') \mid v' \in N(v)\}. By this definition, a labeling ff is equivalent to itself.

The figure below shows two equivalent labelings.

Two equivalent labelings

Given a tree TT and a labeling ff of TT, write a program that counts the labelings equivalent to ff. Since ff is equivalent to itself, include ff in the count.

Input

Your program reads from standard input. The first line contains the number of test cases TT (1T201 \le T \le 20).

The first line of each test case contains the number of vertices NN (1N10001 \le N \le 1000) of the tree. Each of the next N1N-1 lines contains two integers ii and jj (1i,jN1 \le i, j \le N), which describe an edge joining vertex ii and vertex jj. The next line contains NN integers describing a labeling, where the iith number is the label of vertex ii. All integers are separated by a single space.

Output

Your program writes to standard output. For each test case, print the number of labelings equivalent to ff on one line. This number can grow very large, so print the exact value without taking a modulo.