Su and Crazy summation

5

1 votes
Segment Trees, Lazy-propgation, Trees, Easy-Medium
Problem

Once again, Su was caught sleeping in an algorithms class. This was the Yth time (The teacher lost track of Y). The teacher is very angry and decided to punish him and gave an extra assignment.

The assignment is as follows:

You are given a tree having N nodes rooted at 1. Each node is numbered from 1 to N. Each node contains a number Xi i[1,N]. You have to perform Q operations which are one among the 3 types mentioned below

  • 1 V α β - Let T denote the subtree rooted at V. Perform the below operation: Xi=αXi+β iT
  • 2 V - Let T denote the subtree rooted at V. Find the value of the below expression. (iTjTkTlT(Xi+Xj+Xk+Xl)2)  %  M
  • 3 V - Let T denote the subtree rooted at V. Find the value of the below expression. (iTjTkTlT(Xi+Xj+Xk+Xl)3)  %  M

Input format

The first line contains two space separated integers N and Q.
Next line contains N space separated integers, ith of them is value Xi.
Then follows N1 lines, each containing two space separated integers A and B, denoting an undirected edge between nodes A and B.
Then follows Q lines, each line describing a query as given in the problem statement.

Output format

For each query of type 2 and type 3, print a single line containing the answer.

Constraints

1N5105
1Q2105
1X[i]108
1α,β104
1V,A,BN
AB
M=1000000007
The given graph denotes an undirected tree rooted at 1.

Note : This problem has binary scoring. You will receive points only if you pass all test files successfully.

Time Limit: 10
Memory Limit: 1024
Source Limit:
Editor Image

?