An Animal Contest 7 P6 - S-Squirrel?

View as PDF

Points: 20 (partial)
Time limit: 1.5s
Memory limit: 256M

Author:
Problem types

Just as the squirrel counterattack starts you wake up to the noise of your alarm clock ringing beside your bed. You feel sweat rolling down your back and trembling plagues your hands as a deep chill permeates throughout your body. What a weird dream!

To shake off the lingering aftereffects (you can't look at squirrels the same way anymore — they give off a threatening aura) you decide to visit your favourite competitive programming site (DMOJ of course) and join a contest from your favourite contest series (AAC of course) to do the last problem:

There are multisets numbered from to that are all initially empty and a graph with nodes initially with edges. We define the value of the graph as the sum of the squares of the sizes of all of its connected components. You have to process queries in the form . There are types of queries:

1. If , an edge is added between node and all the nodes present within the multisets numbered where . Then, node is added to all of these multisets. Output the value of the graph.

2. If , output the expected value of the graph if a node was chosen uniformly at random and an edge was added between it and all of the nodes present within the multisets numbered where . Afterwards, all of these multisets are cleared of nodes.

Constraints

There will only be queries of type .

Input Specification

The first line will contain four space separated integers , , , .

The next lines will contain two space separated integers , representing an edge in the graph.

The next lines contain three space separated integers , , .

Output Specification

Output lines with the -th line containing one integer: the answer to the -th query.

For queries of type , output the expected value modulo . More specifically, if the expected value is , output .

Sample Input

10 10 10 1
8 9
8 8 5
10 10 6
2 2 7
3 6 2
4 6 0
1 8 7
3 3 2
5 10 0
1 8 6
1 10 2

Sample Output

12
12
12
12
400000017
18
18
800000036
24
24