Under construction

Still being built. Things may change

Bracket Sequence

Time limit2sMemory limit512 MB

Summary
Evaluate a bracket sequence where nesting alternates between addition and multiplication, printing the result modulo 10^9+7.
Level

Medium5 of 10

Topics
Stack, Implementation, Math, Simulation
Solved
No attempts yet

Problem

Two great friends, Eddie John and Kris Cross, are attending the Brackets Are Perfection Conference. They wholeheartedly agree with the main message of the conference and they are delighted with all the new things they learn about brackets.

One of these things is a bracket sequence. If you want to do a computation with + and ×, you usually write it like so:

(2 × (2 + 1 + 0 + 1) × 1) + 3 + 2.

The brackets are only used to group multiplications and additions together. This means that you can remove all the operators, as long as you remember that addition is used for numbers outside any parentheses! A bracket sequence can then be shortened to

( 2 ( 2 1 0 1 ) 1 ) 3 2.

That is much better, because it saves on writing all those operators. Reading bracket sequences is easy, too. Suppose you have the following bracket sequence

5 2 ( 3 1 ( 2 2 ) ( 3 3 ) 1 ).

You start with addition, so this is the same as the following:

5 + 2 + ( 3 1 ( 2 2 ) ( 3 3 ) 1 ).

You know the parentheses group a multiplication, so this is equal to

5 + 2 + (3 × 1 × ( 2 2 ) × ( 3 3 ) × 1).

Then there is another level of parentheses: that groups an operation within a multiplication, so the operation must be addition.

5 + 2 + (3 × 1 × (2 + 2) × (3 + 3) × 1) = 5 + 2 + (3 × 1 × 4 × 6 × 1) = 5 + 2 + 72 = 79.

Since bracket sequences are so much easier than normal expressions with operators, it should be easy to evaluate some big ones. We will even allow you to write a program to do it for you.

Note that ( ) is not a valid bracket sequence, nor a subsequence of any valid bracket sequence.

Input

  • One line containing a single integer 1≤n≤3⋅1051 \le n \le 3 \cdot 10^5.
  • One line consisting of nn tokens, each being either (, ), or an integer 0≤x<109+70 \le x < 10^9 + 7. It is guaranteed that the tokens form a bracket sequence.

Output

Output the value of the given bracket sequence. Since this may be very large, you should print it modulo 109+710^9 + 7.

Examples5

  1. Example 1

    Input
    2
    2 3
    
    Expected output
    5
    
  2. Example 2

    Input
    8
    ( 2 ( 2 1 ) ) 3
    
    Expected output
    9
    
  3. Example 3

    Input
    4
    ( 12 3 )
    
    Expected output
    36
    
  4. Example 4

    Input
    6
    ( 2 ) ( 3 )
    
    Expected output
    5
    
  5. Example 5

    Input
    6
    ( ( 2 3 ) )
    
    Expected output
    5