A workshop has n alchemical stations arranged in a fixed line. Every batch of raw essence passes through station 1 first, then station 2, and so on through station n, in that exact order. Each station performs one of three transformations on the essence's magic value x as it passes through:
ADD c — the value becomes x + cMUL c — the value becomes x * cSQR — the value becomes x * xThe workshop is given q separate starting values, one at a time; each one travels through the same chain of stations independently, starting fresh at station 1. Because repeated multiplication and squaring can make the magic value astronomically large, report every final value modulo 1,000,000,007.
ADD c, MUL c, or SQR (with no parameter).Print q lines. The i-th line contains the final magic value, modulo 1,000,000,007, obtained by sending the i-th starting value through the full chain of stations in order.
Example 1
Input
3 ADD 3 MUL 2 SQR 2 1 4
Expected
64 196
Explanation
The chain is ADD 3, MUL 2, SQR. Starting from 1: ADD 3 gives 4, MUL 2 gives 8, SQR gives 64. Starting from 4: ADD 3 gives 7, MUL 2 gives 14, SQR gives 196. Neither result reaches the modulus, so the outputs are 64 and 196.
Example 2
Input
2 ADD 1000000000 SQR 1 1000000000
Expected
196
Explanation
Starting from 1,000,000,000: ADD 1,000,000,000 gives 2,000,000,000, then SQR gives 4,000,000,000,000,000,000. Taking that value modulo 1,000,000,007 gives 196.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →