Magic Donut Game
Time limit2sMemory limit512 MB
Given a circular board with rotate, flip, and range-add cards, output the sum of the first X cells from the reference cell for each sum card, modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Segment tree, Array, Simulation
- Solved
- No attempts yet
Problem
Albert plays the Magic Donut Game on a donut-shaped board made of cells of equal size.
First, one cell is chosen as the "reference cell" and placed at the 12 o'clock position. Then the integers are written into the cells in clockwise order. The figure below shows the case , . The arrow marks the reference cell.

Next, instruction cards are drawn in order, and each card's instruction is carried out. Each card has three integers. For the -th card, the integers are called in that order. There are five kinds of instructions, and the first integer decides which one applies ().
- : Rotate the board counterclockwise by cells. is always .
- : Rotate the board clockwise by cells. is always .
- : Flip the board over the axis through 12 o'clock and 6 o'clock. and are always .
- : Starting from the reference cell and moving clockwise, add to each of the next cells.
- : Starting from the reference cell and moving clockwise, add up the values in the next cells and record the sum on a memo. The -th recorded value is called .
For example, take , , , and . The steps are shown in the table below.
There are four type 5 cards, so .
Input
The first line contains the number of test cases .
The first line of each test case contains and , separated by a space. The second line contains the elements of array . The third line contains the elements of array . The fourth line contains the elements of array . The fifth line contains the elements of array . All values on each line are separated by spaces.
Output
For each test case, print the elements of array on one line, separated by spaces. Since the values can become very large, print each element of modulo .
Constraints
- For each with :
- For each with : . If , then . If , then . If , then . If , then .
- For each with :
- At least one element of equals . So the output array is never empty.




