This page is still under construction.

Parts of this page are still being built. What you see may change.

K-Calculator

Time limit1.5sMemory limit512 MB

Summary
Evaluate an expression of rational operands by repeatedly applying the operator selected by u_i XOR the modular value of the previous result, removing it and merging its operands.
Level

Hard8 of 10

Topics
Linked list, Math, Number theory, Simulation
Solved
No attempts yet

Problem

Do you know kimchi?

Do you know Lee Guk-ryeol?

Do you know calculators? Perfect

Guk-ryeol has set a calculator problem three times already, and now he has set another one. Let us see how long this keeps going. You have run into yet another calculator problem, which is annoying, but to win the prize you have to evaluate the given expression according to the rules.

Guk-ryeol gave expressions in the same format three times, and since Guk-ryeol, the contestants, and the reviewers all do not want to suffer through parsing, he changed the format. In this input, numbers and operators alternate, with a space between each number and operator. If we write the ii-th number of the expression as XiX_i and the ii-th operator as OpiOp_i, an expression with NN numbers can be written as X1X_1 Op1Op_1 X2X_2 Op2Op_2 ... OpN−1Op_{N-1} XNX_N. The operators are +, -, *, /. The input never ends with an operator, never has a negative XiX_i, and never has unnecessary leading zeros. That is, cases such as −1−1-1-1, 2+−32+-3, and 001+0002001+0002 are not given as input.

An expression made of NN numbers and N−1N-1 positive integers uiu_i are given. You evaluate it according to the following rules.

  1. Repeat the following N−1N-1 times.
  2. Take the most recently computed result, written as the reduced fraction P/QP/Q. Let Q−1Q^{-1} be the multiplicative inverse of QQ modulo 109+710^9+7, and let vv be the remainder of P×Q−1P \times Q^{-1} modulo 109+710^9+7. If there is no previously computed result, v=0v=0.
  3. Evaluate the (ui⊕v)(u_i \oplus v)-th operator and output the result of that evaluation. Here ⊕\oplus means bitwise XOR.
  4. Remove the operator and operands you evaluated, and put the result in their place.

For example, given the expression 3−2×5+103 - 2 \times 5 + 10 and the positive integers u=[1,3,14]u = [1,3,14], you evaluate as follows.

  1. Since 1⊕0=11 \oplus 0 = 1, evaluate the 1st operator. The result is 3−2=13-2=1, so output 11, and the expression becomes 1×5+101 \times 5 + 10.
  2. Since 3⊕1=23 \oplus 1 = 2, evaluate the 2nd operator. The result is 5+10=155+10 =15, so output 1515, and the expression becomes 1×151 \times 15.
  3. Since 14⊕15=114 \oplus 15 = 1, evaluate the 1st operator. The result is 1×15=151 \times 15 =15, so output 1515.

Division in this problem is real division. That is, 5/25/2 is 2.52.5, not 22.

Write a program that evaluates an expression this way.

Input

The first line gives NN, the number of operands. (2≤N≤500 0002 \le N \le 500\,000)

The second line gives an expression made only of operands and +, -, *, /. Operands and operators are separated by spaces, and each operand is an integer between 00 and 109+610^9+6 inclusive.

The third line gives u1u_1, ..., uN−1u_{N-1}, separated by spaces. (1≤ui⊕v≤N−i1 \le u_i \oplus v \le N - i)

No case in the evaluation divides by 00.

Output

For each ii, write the result for uiu_i as the reduced fraction P/QP/Q, let Q−1Q^{-1} be the multiplicative inverse of QQ modulo 109+710^9+7, and print the remainder of P×Q−1P \times Q^{-1} modulo 109+710^9+7 on the ii-th line.

Examples1

  1. Example 1

    Input
    4
    3 - 2 * 5 + 10
    1 3 14
    
    Expected output
    1
    15
    15