TeamsCode Summer 2024 Advanced Division
A. P!=NP
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Count all pairs of integers $$$(n, p)$$$ such that $$$0 \le p \le P$$$, $$$p \neq n \cdot p$$$, and $$$p!=n \cdot p$$$.

Input

The input consists of a single integer $$$P$$$ ($$$1 \le P \le 10^5$$$) — the upper bound on $$$p$$$.

Tests in subtasks are numbered from $$$1 - 10$$$ with samples skipped. Each test is worth $$$\frac{100}{10}=10$$$ points.

Tests $$$1 - 5$$$ will satisfy $$$P \le 1000$$$.

The remaining tests do not satisfy any additional constraints.

Output

Output a single integer — the number of integer values for $$$n$$$ and $$$p$$$ that satisfy the constraints.

Example
Input
4
Output
2
Note

In the sample test, the $$$2$$$ values of $$$(n,p)$$$ that work are $$$(2,3)$$$ and $$$(6,4)$$$.

Problem Idea: willy108

Problem Preparation: xug

Occurrences: Novice A, Advanced A

B. Monkey Arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Yash really likes monkeys and special arrays. Because he was curious, Curios George gave Yash an array $$$A$$$ of length $$$N$$$, and now Yash wonders how many good subarrays exist in this array.

Specifically, Yash deems a subarray good if $$$X$$$, his favorite number, is the maximum value in the subarray, $$$Y$$$, his second favorite number, is the minimum in the subarray, and $$$K$$$, his least favorite number, is not in the subarray. In other words, in a given subarray, $$$X$$$ should be the maximum value, $$$Y$$$ should be the minimum, and $$$K$$$ should not be in the subarray.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ $$$(1 \leq t \leq 5 \cdot 10^4)$$$ — the number of test cases.

The first line of each test case contains four integers $$$N$$$, $$$X$$$, $$$Y$$$, and $$$K$$$ ($$$1 \le N \le 5 \cdot 10^5, 1 \le Y \lt K \lt X \le 10^9$$$).

The second line of each test case contains $$$N$$$ integers $$$A_1,A_2,\ldots,A_N$$$ ($$$1 \le A_i \le 10^9$$$) — the elements of the array $$$A$$$.

It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

Tests in subtasks are numbered from $$$1-20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-5$$$ satisfy that the sum of $$$N$$$ over all test cases does not exceed $$$100$$$.

Tests $$$6-10$$$ satisfy that the sum of $$$N$$$ over all test cases does not exceed $$$1000$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each test case, output a single integer — the number of good subarrays in the array $$$A$$$.

Example
Input
3
8 7 4 5
6 7 4 6 5 4 6 7
6 5 2 4
3 2 4 5 1 3
12 8 3 6
10 4 3 7 4 8 6 5 1 3 8 2
Output
5
0
3
Note

In the first test case of the sample test, the $$$5$$$ subarrays that are good are $$$[6, 7, 4]$$$, $$$[6, 7, 4, 6]$$$, $$$[7, 4]$$$, $$$[7, 4, 6]$$$, and $$$[4, 6, 7]$$$.

In the second test case of the sample test, there are no good subarrays.

Problem Idea: yash_9a3b

Problem Preparation: yash_9a3b, xug

Occurrences: Novice G, Advanced B

C. Monkey Math Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a chain of $$$n$$$ nodes with edges between nodes $$$u$$$ and $$$v$$$ such that $$$|v - u| = 1$$$. For each node $$$i$$$ ($$$1 \le i \le n$$$), you will keep it with probability $$$\frac{1}{i}$$$ or delete it with probability $$$1 - \frac{1}{i}$$$. Find the expected number of connected components after all the deletions, modulo $$$10^9+7$$$.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ $$$(1 \leq t \leq 2 \cdot 10^5)$$$ — the number of test cases.

Each test case contains one line consisting of a single integer $$$n$$$ $$$(1 \leq n \leq 10^6)$$$ — the number of nodes in the chain.

Tests in subtasks are numbered from $$$1-10$$$ with samples skipped. Each test is worth $$$\frac{100}{10}=10$$$ points.

Test $$$1$$$ satisfies $$$1 \leq n \leq 15$$$.

Tests $$$2-3$$$ satisfy $$$1 \leq n \leq 100$$$.

Tests $$$4-5$$$ satisfy $$$t=1$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each test case, print a single integer — the expected number of connected components after all deletions. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{a}{b}$$$, where $$$a$$$ and $$$b$$$ are integers. Output the integer equal to $$$a \cdot b^{-1} \text{ mod } 10^9+7$$$.

Example
Input
4
1
2
3
4
Output
1
1
166666669
333333337
Note

For the first test case in the sample test, $$$n=1$$$, so there is one node that you keep with probability $$$\frac{1}{1}=1$$$, so there can only be $$$1$$$ connected component in the final graph.

In the second test case in the sample test, $$$n=2$$$, so you keep node $$$1$$$ with probability $$$\frac{1}{1}=1$$$ and node $$$2$$$ with probability $$$\frac{1}{2}$$$. Whether node $$$2$$$ is kept or not, there will be $$$1$$$ connected component.

Problem Idea: willy108, Yam

Problem Preparation: Yam

Occurrences: Novice I, Advanced C

D. Kawaii the Rinbot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, two very productive (even peakly productive) Rinbot'ers have been optimizing Rinbot. They realized that instead of buying a sentient supercomputer, they could use hangman strats instead. These two highly productive Rinbot'ers made a giant cloud-hosted database where they stored the title for every anime in Rinbot. Unfortunately, the AC turned off, marking the end of the laptop.

Can you write a program to access their database?

In this link, there is a text file with all anime titles for shows in Rinbot (up until the Spring 2024 season).

For $$$T$$$ titles, determine which line in the file the title is modulo $$$P$$$. For instance, "86" is on line $$$75$$$ and "?" is on line $$$5$$$.

The Teamscode Online Judge has a submission size limit of 50 kB.

Input

The first line of input will contain a single integer $$$T$$$ $$$(1 \leq T \leq 2 \cdot 10^4)$$$ — the number of titles to consider.

The second line of input will contain a single integer $$$P$$$ $$$(P \in {\{2, 10, 10^5\}})$$$ — the modulus applied to the line numbers you find.

The following $$$T$$$ lines will each contain a title. It is guaranteed that the title is present character for character (including punctuation, spaces, and correct uppercase/lowercase distinction) in the above text file.

It is guaranteed that the sum of all inputted title lengths does not exceed $$$10^6$$$.

Tests in subtasks are numbered from $$$1 - 20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1$$$ will only ask for titles within the second sample test.

Tests $$$2 - 3$$$ will only ask for titles within the first $$$100$$$ lines.

Tests $$$4 - 5$$$ will only ask for titles within the first $$$3366$$$ lines and satisfy $$$P = 2$$$.

Tests $$$6 - 8$$$ will only ask for titles within the first $$$3366$$$ lines and satisfy $$$P = 10$$$.

Tests $$$9 - 10$$$ will only ask for titles within the first $$$3366$$$ lines.

Tests $$$11 - 13$$$ will satisfy $$$P = 2$$$.

Tests $$$14 - 16$$$ will satisfy $$$P = 10$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each title, let $$$\texttt{line}$$$ denote the line number that the title occurs at in the text file. Output a single integer — $$$\texttt{line} \text{ mod P}$$$.

Examples
Input
11
100000
86
?
Bakemonogatari
Hayate no Gotoku!
Shuumatsu Nani Shitemasuka? Isogashii Desuka? Sukutte Moratte Ii Desuka?
Shoujo Shuumatsu Ryokou
"Oshi no Ko"
Shiguang Dailiren II
Hajime no Ippo: The Fighting! - New Challenger
Zombie Land Saga
Lycoris Recoil
Output
75
5
666
3535
8480
8424
17
8201
3366
10591
5553
Input
11
100000
Bakemonogatari
Nisemonogatari
Nekomonogatari (Kuro): Tsubasa Family
Monogatari Series: Second Season
Hanamonogatari: Suruga Devil
Kizumonogatari
Tsukimonogatari: Yotsugi Doll
Owarimonogatari (2017)
Koyomimonogatari
Owarimonogatari
Zoku Owarimonogatari
Output
666
6686
6576
6272
3439
4968
9549
7122
5175
7128
10577
Note

Problem Idea: willy108

Problem Preparation: willy108

Occurrences: Novice J, Advanced D

E. Waymo orzorzorz
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jason wants to orz Waymo! To achieve this, he can use the following operations:

1. Type "orz" in $$$A$$$ seconds.

2. Copy all the text in $$$B$$$ seconds.

3. Paste all the copied text in $$$C$$$ seconds.

Since Jason is in a hurry, please help him minimize the amount of time he spends to orz Waymo at least $$$N$$$ times.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$T$$$ ($$$1 \le T \le 10$$$) — the number of test cases.

Each test case contains one line, with four positive integers $$$A$$$, $$$B$$$, $$$C$$$, and $$$N$$$ ($$$1 \le A,B,C \le 10^6, 1 \le N \le 10^9$$$).

Tests in subtasks are numbered from $$$1 - 20$$$ with samples skipped. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1-2$$$ satisfy $$$N \le 2000$$$.

Tests $$$3-6$$$ satisfy $$$N \le 5 \cdot 10^5$$$.

Tests $$$7-12$$$ satisfy $$$N \le 10^7$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each test case, output a single integer — the minimum amount of time to type "orz" at least $$$N$$$ times.

Example
Input
4
1 1 1 20
1 2 3 143
1 4 3 890
3 20 7 650
Output
9
29
51
156
Note

In the test case of the sample test, it is optimal to type orz 3 times, then copy, paste, copy, paste, paste, paste.

Problem Idea: jay_jayjay

Problem Preparation: jay_jayjay

Occurrences: Novice K, Advanced E

F. Stage 4
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a value $$$x$$$, a permutation $$$p$$$ of length $$$n$$$, and an array $$$q$$$ of length $$$n$$$. For every $$$p_i$$$, you must choose to either set $$$x := x + p_i$$$ or set $$$x := x \oplus 2^{d(p_i)}$$$, where $$$\oplus$$$ denotes the bitwise XOR operation and $$$d(a)$$$ denotes the sum of the digits of $$$a$$$ in its decimal representation. For each $$$k$$$ ($$$1 \le k \le n$$$), find the number of possible values of $$$x$$$ that satisfy $$$x \leq q_k$$$ after processing the elements $$$p_1, p_2, \ldots, p_k$$$ in order from $$$1$$$ to $$$k$$$.

Input

The first line of input contains two integers $$$n$$$ and $$$x$$$ $$$(1 \leq n \leq 500, 1 \leq x \leq 5 \cdot 10^6)$$$ — the length of the permutation $$$p$$$ and the starting value of $$$x$$$.

The second line of input contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ $$$(1 \leq p_i \leq n)$$$. It is guaranteed that $$$p$$$ is a permutation.

The third line contains $$$n$$$ integers $$$q_1, q_2, \ldots, q_n$$$ $$$(1 \leq q_i \leq 5 \cdot 10^6)$$$.

Tests in subtasks are numbered from $$$1-20$$$ with samples skipped. Each test is worth $$$\frac{100}{20} = 5$$$ points.

Test $$$1$$$ satisfies $$$n \leq 20$$$, $$$x \leq 100$$$.

Tests $$$2-4$$$ will satisfy $$$n \leq 200$$$, $$$x \leq 10^5$$$.

Tests $$$5-12$$$ will satisfy $$$n \leq 400$$$.

The remaining tests do not satisfy any additional constraints.

Output

Print $$$n$$$ lines, the $$$i$$$-th line containing a single integer denoting the number of possible values of $$$x$$$ that satisfy $$$x \leq q_i$$$ after processing the prefix $$$p_1, p_2, \ldots, p_i$$$.

Example
Input
3 2
1 3 2
2 3 8
Output
1
1
4
Note

In the sample test, you're given the permutation $$$p=[1, 3, 2]$$$ and an initial $$$x$$$ value of 2.

For the prefix $$$[p_1]$$$, the two possible values of $$$x$$$ you can achieve are $$$2 + 1 = 3$$$ and $$$2 \oplus (2^1) = 0$$$. Since only $$$x = 0$$$ satisfies $$$x \leq q_1 = 2$$$, the answer for this prefix is $$$1$$$.

For the prefix $$$[p_1, p_2]$$$, the possible values of $$$x$$$ are $$$3, 6, 8, 11$$$. Since only $$$x=3$$$ satisfies $$$x \leq q_2=3$$$, the answer is $$$1$$$.

For the prefix $$$[p_1, p_2, p_3]$$$, the possible values of $$$x$$$ are $$$2, 5, 7, 8, 10, 12, 13, 15$$$. Four of these satisfy $$$x \leq q_3=8$$$, so the answer is $$$4$$$.

Problem Idea: Yam, willy108

Problem Preparation: Yam

Occurrences: Advanced F

G. Ifrit Tile
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Waymo has been playing too much Arknights. Now he looks for rows of 6 in every gridlike structure he finds and has the urge to commit arson using child lab...

The Rhodes Island operators are scouting out the new map for CCB#2. The map can be represented as a tree with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. Slugs of $$$m$$$ different colors exist, and there is one slug of color $$$i$$$ ($$$1 \le i \le m$$$) for each node on the shortest path from $$$s_i$$$ to $$$t_i$$$. Note that multiple slugs of different colors can be on the same node, and not all colors of slugs are initially on the map.

The slugs have a very particular behavior pattern where sometimes all the slugs of the same color will leave the map, or all the slugs of the same color will return to the map.

Ifrit is tasked with clearing out the slugs, so she will consider different plans of attack. A plan of attack is denoted by two nodes $$$u$$$ and $$$v$$$ in which she hits all the slugs on the shortest path from $$$u$$$ to $$$v$$$. If she hits any slug with color $$$i$$$, she adds $$$v_i$$$ to her score, where $$$v_i$$$ is a predetermined value for color $$$i$$$. Ifrit will not actually perform the attack, but she wants to know her score for each plan of attack.

You will have to process $$$q$$$ events, each one of the following three types:

  1. All the slugs of color $$$i$$$ return to their respective nodes on the tree.
  2. All the slugs of color $$$i$$$ leave the tree.
  3. Calculate the score Ifrit would achieve if she performed an attack by selecting nodes $$$u$$$ and $$$v$$$.
Input

The first line of input contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ $$$(1 \leq n, m, q \leq 3 \cdot 10^5)$$$ — the number of nodes in the tree, the number of colors, and the number of events respectively.

The following $$$n-1$$$ lines each contain two integers $$$u$$$ and $$$v$$$ $$$(1 \leq u, v \leq n, u \neq v)$$$, denoting an edge between nodes $$$u$$$ to $$$v$$$ in the tree. It is guaranteed that the inputted set of edges forms a valid tree.

The following $$$m$$$ lines each contain four integers $$$s_i$$$, $$$t_i$$$, $$$v_i$$$, and $$$c_i$$$ $$$(1 \leq s_i, t_i \leq n, 1 \leq v_i \leq 10^9, c_i \in \{0, 1\})$$$ — the start and end of the shortest path the slugs of the $$$i$$$-th color reside on, the value of hitting color $$$i$$$, and whether or not the slugs of color $$$i$$$ are present initially. If $$$c_i = 0$$$, they are not present; otherwise, if $$$c_i = 1$$$, they are.

The following $$$q$$$ lines outline events, each in one of the following formats:

  • $$$1 \ i$$$ $$$(1 \leq i \leq m)$$$ — all the slugs of color $$$i$$$ return to the map. It is guaranteed that they were not present on the map before this event.
  • $$$2 \ i$$$ $$$(1 \leq i \leq m)$$$ — all the slugs of color $$$i$$$ leave the map. It is guaranteed that they were present on the map before this event.
  • $$$3 \ u \ v$$$ $$$(1 \leq u, v \leq n)$$$ — Ifrit is considering an attack plan of $$$u$$$ and $$$v$$$ and wants to know her score if she were to follow that plan. Note that Ifrit will not actually attack anything.

Tests in subtasks are numbered from $$$1 - 20$$$ with samples skipped. Each test is worth $$$\frac{100}{20} = 5$$$ points.

Tests $$$1 - 2$$$ satisfy $$$n, m, q \leq 100$$$.

Tests $$$3 - 5$$$ satisfy $$$n, m, q \leq 1000$$$.

Tests $$$6 - 12$$$ satisfy $$$v_i = 1$$$ for all $$$i$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each attack plan (event of type $$$3$$$), output the score Ifrit will get if she follows the plan.

Examples
Input
5 3 9
1 2
1 3
2 4
2 5
3 4 1 0
2 5 10 0
1 4 100 0
1 1
3 5 3
1 2
3 1 4
1 3
3 1 5
2 1
3 5 3
3 5 5
Output
1
11
111
110
10
Input
10 4 12
2 10
3 6
4 3
7 9
7 5
5 4
1 5
10 1
10 8
9 9 18 0
3 6 47 0
2 4 91 0
3 3 30 1
1 1
3 6 4
1 2
3 1 5
2 4
3 5 10
3 6 2
3 3 9
3 10 8
1 4
3 3 3
1 3
Output
30
0
0
47
65
0
77
Note

Problem Idea: willy108, hyforces

Problem Preparation: willy108

Occurrences: Advanced G

H. Thomas Sometimes Hides His Feelings in C++
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Thomas has recently begun realizing his dream of being the best mangaka in the world. He sees his future in the equation $$$E = Mc^2 + \text{AI}$$$: $$$\text{Events} = \text{Main character}^2 + \text{love}$$$.

Thomas first asks ChatGPT to generate $$$n$$$ ($$$n$$$ is even) character tropes. Then he makes a matrix $$$a$$$ of size $$$n$$$ by $$$n$$$, where $$$a_{ij}$$$ denotes the tension of someone of trope $$$i$$$ having feelings for someone of trope $$$j$$$. $$$a_{ij} = -1$$$ if the trope $$$i$$$ is unable to have any feelings for trope $$$j$$$. Note that $$$a_{ij}$$$ is not necessarily equal to $$$a_{ji} $$$.

Thomas will assign half of his $$$n$$$ tropes to male characters and the other half to female characters. He will draw feelings from each male to some female such that no two males have feelings for the same female. Then he will draw feelings from each female to each male such that no two females have feelings for the same male. Note that feelings are not necessarily reciprocated.

Note that feelings are not necessarily reciprocated. Let $$$p_i$$$ denote the trope for which the $$$i$$$-th trope has feelings. The value of this assignment (of gender and feelings) is

$$$$$$ \frac{\prod_{i \text{ is male}} a_{ip_i}}{\prod_{i \text{ is female}} a_{ip_i}}$$$$$$

Find the sum of values over all possible assignments of gender and feelings. Since this number may be very large, please find it modulo $$$998244353$$$.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \text{ \lt 3}$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 21$$$, $$$n$$$ is even) — the number of tropes.

The following $$$n$$$ lines each contain $$$n$$$ integers, where the $$$j$$$-th integer of the $$$i$$$-th row is $$$a_{ij}$$$ ($$$-1 \leq a_{ij} \lt 998244353, a_{ij} \neq 0$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$50$$$ and the sum of all $$$a_ij$$$ over all test cases does not exceed lunchbox's IQ.

Tests in subtasks are numbered $$$1 - 20$$$ with samples skipped. Each test is worth $$$\frac{100}{20} = 5$$$ points.

Test $$$1$$$ satisfies $$$n \leq 6$$$.

Test $$$2$$$ satisfies $$$n \leq 10$$$.

Tests $$$3 - 4$$$ satisfy $$$n \leq 14$$$.

Tests $$$5 - 10$$$ satisfy $$$n \leq 16$$$.

Tests $$$11 - 15$$$ satisfy $$$n \leq 18$$$.

The remaining tests do not satisfy any additional constraints.

Output

For each test case, it can be proven that the final sum can always be expressed as $$$\frac{x}{y}$$$ for two integers $$$x$$$ and $$$y$$$. Print $$$E$$$ where $$$y \cdot E \equiv x \text{ mod } 998244353$$$ and $$$0 \leq E \lt 998244353$$$.

Examples
Input
2
2
1 1
1 1
2
1 2
1 1
Output
2
499122179
Input
2
4
1 2 -1 -1
1 1 2 -1
-1 4 1 1
-1 -1 1 1
4
1 2 3 4
5 6 7 8
10 11 12 13
14 15 16 17
Output
5
540772941
Note

In the first test case of the first sample test, Thomas has two character tropes. We will say the character trope assigned with female is Ruby and the character trope assigned with male is Aqua. Thus, the two valid assignments of $$$G$$$ and $$$p$$$ are $$$G = MF$$$, $$$p = [1, 2]$$$ with value $$$1$$$ and $$$G = FM$$$, $$$p = [1, 2]$$$ with value $$$1$$$, which sums to $$$2$$$. Either way, Ruby and Aqua have feelings for each other so everybody is happy.

In the second test case of the first sample test, there are two valid assignments of $$$G$$$ and $$$p$$$: $$$G = MF$$$, $$$p = [1, 2]$$$ with value $$$\frac{2}{1}$$$ and $$$G = FM$$$, $$$p = [1, 2]$$$ with value $$$\frac{1}{2}$$$, which sums to $$$\frac{5}{2}$$$. $$$499122179 \cdot 2 \equiv 5 \text{ mod } 998244353$$$, so we output $$$499122179$$$.

Problem Idea: Yam, oursaco, willy108

Problem Preparation: willy108

Problem Occurrences: Advanced H

I. Flappy Deer
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Noko-tan is playing the hit game "Flappy Deer". In this game, Noko-tan controls a flying deer who moves in an infinite 2-D grid. The deer is initially located at some cell. Every second, the deer will move to the right by a single unit (increase $$$x$$$ coordinate by one), and Noko-tan can choose whether to move the deer up by one unit (increase $$$y$$$ coordinate by one), keep the deer's current height, or move the deer down by one unit (decrease $$$y$$$ coordinate by one). There are also $$$N$$$ full water bottles that Noko-tan is deathly afraid of. Thus, if her flying deer ever shares a cell with a water bottle, the game will immediately end. The $$$i$$$-th water bottle is a rectangle occupying the cells in between $$$(x_i, a_i)$$$ and $$$(x_i, b_i)$$$. Finally, to make things interesting, there are also deer crackers floating around. The $$$j$$$-th is located at cell $$$(x_j, y_j)$$$. Noko-tan collects a deer cracker if her flying deer occupies the same cell as that deer cracker. Since Noko-tan has antlers for brains, she doesn't know how to play in order to collect the maximum number of deer crackers. Thus, she asks you to help her find this value for $$$Q$$$ starting positions. Note that Noko-tan encountering a water bottle ends the game, but does not reset the number of crackers she has collected. Also, the grid is infinite, so there is no bounds on Noko-tan's maximum $$$x$$$ or $$$y$$$ coordinates.

Input

The first line contains two integers $$$N$$$, $$$M$$$, and $$$Q$$$ ($$$1 \le N, M, Q \le 10^5$$$).

The next $$$N$$$ lines will contain three integers $$$x_i$$$, $$$a_i$$$, and $$$b_i$$$ ($$$0 \le x_i \le 10^8$$$, $$$-10^8 \le a_i \le b_i \le 10^8$$$) — the locations of the water bottles.

The next $$$M$$$ lines will contain two integers $$$x_j$$$ and $$$y_j$$$ ($$$0 \le x_j \le 10^8$$$, $$$-10^8 \le y_j \le 10^8$$$) — the locations of the deer crackers.

The next $$$Q$$$ lines will contain two integers $$$x_k$$$ and $$$y_k$$$ ($$$0 \le x_k \le 10^8$$$, $$$-10^8 \le y_k \le 10^8$$$) — the starting positions.

There are $$$20$$$ tests, not including samples. Each test is worth $$$\frac{100}{20}=5$$$ points.

For tests $$$1$$$ — $$$2$$$, it is guaranteed that all given coordinates are between $$$0$$$ and $$$1000$$$.

For tests $$$3$$$ — $$$4$$$, it is guaranteed that all given coordinates are between $$$0$$$ and $$$10000$$$.

For tests $$$5$$$ — $$$10$$$, it is guaranteed that all given coordinates are between $$$0$$$ and $$$100000$$$ and $$$Q = 1$$$.

For tests $$$11$$$ — $$$15$$$ it is guaranteed that all given coordinates are between $$$0$$$ and $$$100000$$$.

For tests $$$16$$$ — $$$20$$$ there are no additional constraints.

Output

For each starting position, output a single integer — the maximum number of deer crackers Noko-tan can collect.

Example
Input
3 78 2
8 7 9
24 12 14
24 7 9
5 7
6 7
7 7
7 8
7 9
7 10
5 10
5 11
5 12
5 13
6 13
7 13
9 7
9 8
9 9
9 10
9 11
9 12
9 13
10 10
11 10
12 7
12 8
12 9
12 10
12 11
12 12
12 13
14 7
15 7
16 7
17 7
15 8
15 9
15 10
15 11
15 12
16 8
16 9
16 10
16 11
16 12
14 13
15 13
16 13
17 13
19 7
19 8
19 9
19 10
19 11
19 12
19 13
20 10
21 11
22 12
23 13
21 9
22 8
23 7
25 7
25 8
25 9
25 10
25 11
25 12
25 13
26 13
27 13
26 10
27 10
28 7
28 8
28 9
28 10
28 11
28 12
28 13
0 0
0 10
Output
15
19
Note

Image depicting one of Noko-tan's optimal paths starting from $$$(0, 0)$$$ in the sample test:

Problem Idea: oursaco

Problem Preparation: oursaco

Occurrences: Advanced I

J. Grid Product
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rectangular grid $$$B$$$ with $$$N$$$ rows and $$$M$$$ columns. Consider some rectangular grid $$$A$$$ with $$$N$$$ rows and $$$M$$$ columns. $$$A$$$ is considered mediocre if $$$A_{i, j}$$$ is an integer and $$$0 \leq A_{i, j} \leq B_{i, j}$$$ for all $$$(i, j)$$$.

Let $$$F(A) = \displaystyle\left(\prod_{i = 1}^{N}\left(\sum_{j = 1}^{M}A_{i, j}\right)\right) \displaystyle\left(\prod_{j = 1}^{M}\left(\sum_{i = 1}^{N}A_{i, j}\right)\right)$$$.

Calculate the sum of $$$F(A)$$$ over all mediocre $$$A$$$ modulo $$$998244353$$$.

Input

The first line contains two positive integers $$$N$$$ and $$$M$$$ $$$(1 \leq N \cdot M \leq 300)$$$.

Each of the next $$$N$$$ lines contains $$$M$$$ integers $$$B_{i, j}$$$ $$$(0 \leq B_{i, j} \leq 10^9)$$$.

There are $$$20$$$ tests, not including samples. Each test is worth $$$\frac{100}{20}=5$$$ points.

Tests $$$1 - 2$$$ satisfy $$$N = 1$$$.

Tests $$$3 - 12$$$ satisfy $$$N \cdot M \leq 100$$$.

The remaining tests do not satisfy any additional constraints.

Output

Print one integer — the sum of $$$F(A)$$$ over all mediocre $$$A$$$ modulo $$$998244353$$$.

Examples
Input
1 3
1 2 1
Output
11
Input
2 2
1 1
0 1
Output
5
Input
2 3
1 1 1
1 1 1
Output
312
Input
3 3
69 420 37
666 777 888
998244353 123456789 987654321
Output
57820980
Note

Problem Idea: HaccerKat

Problem Preparation: HaccerKat

Occurrences: Advanced J

K. The Astral Express
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Astral Express is a special train that traverses the galaxy. The galaxy initially consists of $$$2 \cdot n$$$ planets represented as an array $$$a_1, a_2, \ldots, a_{2 \cdot n}$$$, where the $$$i$$$-th planet initially has a gravitational coefficient of $$$a_i = 1$$$ if $$$i \le n$$$ and $$$a_i = -1$$$ if $$$i \gt n$$$.

The Astral Express moves in steps, and the direction of the movement is determined by its velocity $$$v$$$. At the beginning of each step, if $$$v \gt 0$$$, then the Express moves one index to the right. If $$$v \lt 0$$$, then the Express moves one index to the left. If $$$v = 0$$$, then the crew moves in the direction they moved previously. At the end of each step, if the Express is at position $$$i$$$, then $$$a_i$$$ is added to $$$v$$$. If the Astral Express goes past the bounds of the array, then its journey will end.

Since the galaxy is unstable, the planets are constantly breaking apart and merging. A planet with $$$|a_i| = 2$$$ may split into two adjacent planets with gravitational coefficients of $$$\frac{a_i}{2}$$$, and two identical adjacent planets with $$$a_i = a_{i + 1}$$$ and $$$|a_i|= 1$$$ may merge into a single planet with gravitational coefficient $$$a_i + a_{i + 1}$$$.

The Astral Express crew is curious about how their train will move in this unstable galaxy, so you are given $$$q$$$ queries of $$$3$$$ types:

  • "1 x" — The Astral Express begins moving at the rightmost positive $$$a_i$$$ with their given initial velocity equal to $$$x$$$. Output how many steps the Astral Express will take before their journey ends, or output $$$-1$$$ if their journey will go on forever.
  • "2 x" — Merge $$$a_x$$$ and $$$a_{x + 1}$$$ into a new planet with gravitational coefficient $$$a_x + a_{x + 1}$$$. Formally, set $$$a_x$$$ to $$$a_x + a_{x + 1}$$$, and remove $$$a_{x + 1}$$$ from the array. Note that the indices of all elements right of $$$a_{x + 1}$$$ will decrease by one. It is guaranteed that $$$a_x = a_{x + 1}$$$ and $$$|a_x| = 1$$$.
  • "3 x" — Split $$$a_x$$$ into two new planets both with gravitational coefficients $$$\frac{a_x}{2}$$$. Formally, set $$$a_x$$$ to $$$\frac{a_x}{2}$$$ and insert a new planet with gravitational coefficient $$$a_x$$$ at index $$$x + 1$$$. Note that the indices to the right of the inserted element will increase by one. It is guaranteed that $$$|a_x| = 2$$$.

For example, given $$$n = 3$$$, our initial array will look like $$$[1, 1, 1, -1, -1, -1]$$$, and we can perform an operation "2 2", merging $$$a_2$$$ and $$$a_3$$$ together, resulting in an array of $$$[1, 2, -1, -1, -1]$$$. We can then perform an operation "3 2", splitting $$$a_2$$$ into two elements, resulting in an array of $$$[1, 1, 1, -1, -1, -1]$$$.

Input

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le q \le 2 \cdot 10^5$$$).

The next $$$q$$$ lines each contain two integers $$$t$$$ and $$$x$$$ ($$$1 \le t \le 3$$$). If $$$t = 1$$$, then it is guaranteed that $$$1 \le x \le 10^9$$$. If $$$t = 2$$$, then it is guaranteed that $$$a_x = a_{x + 1}$$$ and $$$|a_x| = 1$$$. If $$$t = 3$$$, then it is guaranteed that $$$|a_x| = 2$$$.

There are $$$20$$$ tests, not including samples. Each test is worth $$$\frac{100}{20}=5$$$ points.

In the $$$i$$$-th test, $$$n \le \max(2, \min(10^5, 10^{\lfloor \frac{i}{2} \rfloor})))$$$.

Output

For each query of type $$$1$$$, output a single integer denoting the number of steps the Astral Express will take before their journey ends, or $$$-1$$$ if the journey will go on forever.

Examples
Input
3 7
1 2
2 5
1 2
2 1
1 2
3 4
1 3
Output
9
11
-1
4
Input
1000 20
2 417
3 417
2 695
1 985
3 695
1 995
1 240
2 1594
2 528
3 1593
1 794
2 3
2 1044
2 373
1 114
3 373
1 680
1 424
1 891
1 69
Output
30761
10976
943401
370359
988509
539948
817182
204906
997710
Note

Problem Idea: oursaco

Problem Preparation: oursaco

Occurrences: Advanced K