#P13597. 『GTOI - 1D』回归空白

『GTOI - 1D』回归空白

Background

Even if I myself will sink into boundless sorrow.

I still have to return to blank, to a blank future……

Problem Description

Lingluo has an unrooted tree with nn nodes. The ii-th edge connects node xix_i and node yiy_i. Node ii has a positive integer pip_i on it.

Sometimes, the numbers on the tree will change. The numbers on the two endpoints of the kk-th edge will be swapped, i.e., pxkp_{x_k} and pykp_{y_k} will be exchanged.

Sometimes Lingluo will ask you: if at the beginning only the nodes on the simple path from node ss to node tt are white and all other nodes are blue, then if you repeatedly perform the following step until all nodes become white, what is the expected number of steps?

Randomly choose one blue node bb and one white node ww. The probability of choosing each node is proportional to the number on that node. Then paint all nodes on the simple path from node bb to node ww to white.

Specifically, if node ii is white, the probability of choosing it is

picj=0pj\frac{p_i}{\sum\limits_{c_j=0}p_j}

If node ii is blue, the probability of choosing it is

picj=1pj\frac{p_i}{\sum\limits_{c_j=1}p_j}

where cic_i denotes the color of node ii: 00 means white, and 11 means blue.

Due to the problem setter’s deliberate design, you need to output the result of the answer mod109+7\text{}\bmod10^9+7.

Can you correctly answer every question from Lingluo?

Input Format

The first line contains two positive integers n,mn,m, representing the number of nodes in the tree and the total number of operations.

The second line contains nn positive integers. The ii-th integer is pip_i, representing the number on node ii.

The next n1n-1 lines describe the edges of the tree. Line ii contains two positive integers xi,yix_i,y_i, describing an edge (xi,yi)(x_i,y_i).

Then follow mm lines, each representing one modification or one query. First read a positive integer tptp indicating the operation type:

  • If tp=1tp = 1, read a positive integer kk, meaning to swap the numbers pxkp_{x_k} and pykp_{y_k} on the two endpoints of the kk-th edge.
  • If tp=2tp = 2, read two positive integers s,ts,t, meaning one query from Lingluo.

Output Format

For each query, output one line with one integer, representing the answer.

5 5
1 2 3 4 5
1 2
1 3
2 4
2 5
2 1 5
2 3 3
1 1
2 4 5
2 1 3
2
719696977
800000007
700000007

Hint

Sample Explanation

In the sample, the answers to the last three queries, written as fractions, are 299132\frac{299}{132}, 75\frac{7}{5}, and 2110\frac{21}{10}.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} n,mn,m\le Special Property Score
11 1212 None 1010
22 20002000 2020
33 10510^5 1in\forall1\le i\le npi=114p_i=114 1010
44 It is guaranteed that in all queries s=1s=1
55 None 2525
66 5×1055\times10^5

For all testdata, it is guaranteed that: 1n,m5×1051\le n,m\le5\times10^5, 1xi,yin1\le x_i,y_i\le n, the input graph is a tree, 1pi1091\le p_i\le10^9, pi<109+7\sum p_i<10^9+7, 1tp21\le tp \le2, in queries 1s,tn1\le s,t\le n, and in modifications 1kn11\le k\le n-1.

Note

Please pay attention to the impact of constant factors on program efficiency.

Translated by ChatGPT 5