This page is still under construction.

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

DFS

Time limit8sMemory limit1024 MB

Summary
Given a rooted tree with vertex values, sum the expected minimum value on the random DFS stack over all legal pairs (x, y), modulo 998244353.
Level

Hard9 of 10

Topics
Tree, Union-find, Probability, DFS
Solved
No attempts yet

Problem

You are given a rooted tree of nn vertices, and rr is the root of the tree. Each vertex xx has value axa_x.

Let us define the DFS procedure starting from xx to find yy:

  1. Push xx on the stack.
  2. Check ww, the top element of the stack. If w=yw = y, the procedure ends. Otherwise, if there is at least one son of ww which is not visited, choose one such son with equal probability and push it on the stack.
  3. Repeat step 2 until there is no unvisited son.
  4. Pop the top element from the stack.
  5. Repeat step 2 until the stack is empty.

The procedure is legal if and only if yy is in the subtree of xx.

Define f(x,y)f(x,y) as the expectation of the minimum value of all vertices that were pushed on the stack during the DFS procedure starting from xx to find yy.

Now we want to calculate ∑f(x,y)\sum f(x,y) for all legal pairs (x,y)(x,y). The answer can be written as an irreducible fraction xy\frac{x}{y}, where xx and yy are integers and y≢0(mod998 244 353)y\not\equiv 0\pmod{998\,244\,353}. Output the integer equal to x⋅y−1(mod998 244 353)x\cdot y^{-1}\pmod{998\,244\,353}. In other words, output an integer aa such that 0≤a<998 244 3530\leq a<998\,244\,353 and a⋅y≡x(mod998 244 353)a\cdot y\equiv x\pmod{998\,244\,353}.

Input

The first line contains an integer TT (1≤T≤1001 \leq T \leq 100), the number of test cases.

For each test case, the first line contains two integers nn and rr (1≤n≤4⋅1051 \leq n \leq 4 \cdot 10^5, 1≤r≤n1 \leq r \leq n), the number of vertices and the root.

The next line contains nn integers, where the ii-th integer is aia_i (1≤ai≤1091\leq a_i\leq 10^9), the value of vertex ii.

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \leq u,v \leq n), an edge of the tree.

It is guaranteed that ∑n≤8⋅105\sum n \leq 8 \cdot 10^5 and that the given graph is a tree.

Output

For each test case, print the answer on its own line.

Examples1

  1. Example 1

    Input
    4
    1 1
    1
    3 3
    3 3 4
    3 1
    3 2
    6 1
    5 2 4 1 3 6
    1 2
    1 6
    2 3
    2 4
    4 5
    5 1
    5 4 3 2 1
    1 2
    1 3
    3 4
    3 5
    
    Expected output
    1
    16
    34
    499122202