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

#GL0000262. Gây bất ngờ! (Surprise me!)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Gây bất ngờ! (Surprise me!)

Surprise me!

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Leha and Noora play a game on a tree with nn vertices numbered from 11 to nn. A tree is a connected undirected graph without cycles. Each vertex uu carries an integer aua_u. The numbers a1,a2,…,ana_1,a_2,\ldots,a_n form a permutation of 1,2,…,n1,2,\ldots,n: every integer occurs exactly once.

Noora chooses a vertex uu uniformly at random from all nn vertices. Leha then uniformly chooses a vertex vv among the remaining n−1n-1 vertices, so u≠vu\ne v. Every ordered pair (u,v)(u,v) has the same probability.

The value of this choice is

f(u,v)=φ(au⋅av)⋅d(u,v),f(u,v)=\varphi(a_u\cdot a_v)\cdot d(u,v),

where φ(x)\varphi(x) is the number of integers kk with 1≤k≤x1\le k\le x and gcd⁡(k,x)=1\gcd(k,x)=1, and d(u,v)d(u,v) is the number of edges along the unique simple path between the vertices.

Find the expected value of f(u,v)f(u,v) over all n(n−1)n(n-1) ordered pairs. Express the expectation as a reduced fraction P/QP/Q. Print P⋅Q−1P\cdot Q^{-1} modulo 109+710^9+7, where Q−1Q^{-1} is the multiplicative inverse of QQ modulo 109+710^9+7.

Input

  • The first line contains an integer nn, the number of vertices.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in vertex order.
  • Each of the following n−1n-1 lines contains two integers x,yx,y, describing one undirected edge connecting xx and yy.

Output

Print a single integer representing the expected value modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le12; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n, with all aia_i distinct; 1≤x,y≤n1\le x,y\le n for every edge (x,y)(x,y); the n−1n-1 edges form an undirected tree.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le2000; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n, with all aia_i distinct; 1≤x,y≤n1\le x,y\le n for every edge (x,y)(x,y); the n−1n-1 edges form an undirected tree.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le200000; 1≤ai≤n1\le a_i\le n for every 1≤i≤n1\le i\le n, with all aia_i distinct; 1≤x,y≤n1\le x,y\le n for every edge (x,y)(x,y); the n−1n-1 edges form an undirected tree.

Examples

Example 1

Input

3
1 2 3
1 2
2 3

Output

333333338

Explanation

There are 3⋅2=63\cdot2=6 ordered pairs. Pairs (1,2)(1,2) and (2,1)(2,1) each contribute φ(2)⋅1=1\varphi(2)\cdot1=1. Pairs (1,3)(1,3) and (3,1)(3,1) each contribute φ(3)⋅2=4\varphi(3)\cdot2=4. Pairs (2,3)(2,3) and (3,2)(3,2) each contribute φ(6)⋅1=2\varphi(6)\cdot1=2. The sum equals 2(1+4+2)=142(1+4+2)=14, so the expectation is 14/6=7/314/6=7/3. The modular inverse of 33 is 333333336333333336, which gives 7⋅333333336 mod (109+7)=3333333387\cdot333333336\bmod(10^9+7)=333333338.

Example 2

Input

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

Output

8

Explanation

The edges (3,5),(1,2),(4,3),(2,5)(3,5),(1,2),(4,3),(2,5) form the path 1→2→5→3→41\to2\to5\to3\to4. The values along the path are 5,4,2,3,15,4,2,3,1. The sum of φ(auav)d(u,v)\varphi(a_u a_v)d(u,v) over the 1010 unordered pairs equals 8080; each unordered pair is counted twice in the ordered sum, so the total is 160160. Dividing by 5⋅4=205\cdot4=20 gives the expectation 88, which is also the modular answer.