PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000256. Cây thừa số (Factor Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cây thừa số (Factor Tree)

Factor Tree

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

ChefLand consists of nn cities numbered from 11 to nn, connected by n−1n-1 bidirectional roads forming a tree: there is exactly one simple path between any two cities.

City ii has a positive integer house value aia_i. A query specifies cities u,vu,v (possibly equal). Consider the product of all axa_x for cities xx on the unique simple path from uu to vv, including both endpoints.

For each query, output the number of positive divisors of that product, modulo 109+710^9+7. There are qq independent queries, with no updates to the tree or its values.

Input

The first line contains nn, the number of cities.

Each of the next exactly n−1n-1 lines contains two integers xi,yix_i,y_i specifying a bidirectional road. These edges form a tree.

The following line contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, the city values.

The next line contains qq, the number of queries.

Each of the last exactly qq lines contains two city indices u,vu,v, the endpoints of the requested path.

Output

Print exactly qq lines. Line jj contains the number of positive divisors of the product of values on the path specified by query jj, modulo 109+710^9+7.

Subtasks

  • Subtask 1 (10 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000; 1≤ai≤1061\le a_i\le 10^6 for all 1≤i≤n1\le i\le n; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; the edges form a tree.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤1051\le q\le 10^5; 1≤ai≤1061\le a_i\le 10^6 for all 1≤i≤n1\le i\le n; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; the edges form a tree.
  • Subtask 3 (60 points): 1≤n≤1051\le n\le 10^5; 1≤q≤1051\le q\le 10^5; 1≤ai≤1061\le a_i\le 10^6 for all 1≤i≤n1\le i\le n; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; the edges form a tree.

Examples

Input

5
1 2
1 3
2 4
2 5
2 6 4 3 5
2
1 4
2 2

Output

9
4

Explanation

The tree edges are 1−21-2, 1−31-3, 2−42-4, 2−52-5, and node values are [2,6,4,3,5][2,6,4,3,5].

  • Query (1,4)(1,4) traverses 1→2→41\to2\to4. The product is 2⋅6⋅3=36=22⋅322\cdot6\cdot3=36=2^2\cdot3^2. Its divisor count is (2+1)(2+1)=9(2+1)(2+1)=9.
  • Query (2,2)(2,2) contains only node 22, whose value is 6=2⋅36=2\cdot3, with (1+1)(1+1)=4(1+1)(1+1)=4 positive divisors.

Both values are below the modulus, so the output is 99 followed by 44.