Under construction

Still being built. Things may change

Best Parenthesis

Interview

Time limit1sMemory limit256 MB

Summary
Given a balanced parenthesis string encoded as 0/1 values, compute its recursively defined score modulo 12345678910.
Level

Medium5 of 10

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

Problem

Recently the cows have been competing with strings of balanced parentheses, comparing them to see whose string is best.

A balanced parenthesis string is scored by the following rules (every string considered here is balanced):

  • The string () has score 11.
  • If a string A has score s(A)s(A), then (A) has score 2⋅s(A)2 \cdot s(A).
  • If strings A and B have scores s(A)s(A) and s(B)s(B), then their concatenation AB has score s(A)+s(B)s(A) + s(B).

For example, s((())())=s((()))+s(())=2⋅s(())+1=2⋅1+1=3s(\text{(())()}) = s(\text{(())}) + s(\text{()}) = 2 \cdot s(\text{()}) + 1 = 2 \cdot 1 + 1 = 3.

Bessie wants to beat every other cow, so she must be able to compute the score of a given string. Given a balanced parenthesis string of length NN (2≤N≤100,0002 \le N \le 100{,}000), compute its score.

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains one integer describing the ii-th character of the string — 00 if it is (, or 11 if it is ).

Output

  • Line 1: the score of the string. Because this value can be very large, output it modulo 1234567891012345678910.

Hint

The input encodes the string one character at a time: each value 00 means an opening parenthesis ( and each value 11 means a closing parenthesis ). Concatenate these characters in order to reconstruct the original string, then apply the scoring rules.

Examples4

  1. Example 1

    Input
    6
    0
    0
    1
    1
    0
    1
    
    Expected output
    3
    
  2. Example 2

    Input
    2
    0
    1
    
    Expected output
    1
    
  3. Example 3

    Input
    4
    0
    1
    0
    1
    
    Expected output
    2
    
  4. Example 4

    Input
    6
    0
    0
    0
    1
    1
    1
    
    Expected output
    4