This page is still under construction.

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

Shipura

Time limit8sMemory limit512 MB

Summary
Evaluate expressions that combine floor division by powers of two with squaring modulo 1,000,000,007 and nested brackets.
Level

Medium4 of 10

Topics
Stack, Recursion, Bit manipulation
Solved
No attempts yet

Problem

Dr. Suposupo built a programming language called Shipura. Shipura has one binary operator, >>, and one unary function, S< >, and nothing else.

xx >> yy evaluates to ⌊x/2y⌋\lfloor x / 2^y \rfloor, the greatest integer that does not exceed x/2yx / 2^y. S< xx > evaluates to x2 mod 1,000,000,007x^2 \bmod 1{,}000{,}000{,}007, the remainder of x2x^2 divided by 1,000,000,0071{,}000{,}000{,}007.

The operator >> is left associative. For example, xx >> yy >> zz is read as (x(x >> y)y) >> zz, not as xx >> (y(y >> z)z). These parentheses never appear in an actual Shipura expression.

The syntax of Shipura in BNF is the following.

expr   ::= term | expr sp ">>" sp term
term   ::= number | "S" sp "<" sp expr sp ">"
sp     ::= "" | sp " "
number ::= digit | number digit
digit  ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

The start symbol is expr, which represents a Shipura expression. A number is an integer between 00 and 1,000,000,0001{,}000{,}000{,}000 inclusive, written without extra leading zeros.

Write a program that evaluates Shipura expressions.

Input

The input is a sequence of datasets. Each dataset is one line, and that line holds one valid Shipura expression.

A line holding a single # ends the input. There are at most 100 datasets, and the whole input file is at most 2,000,000 bytes.

Output

For each dataset, print the value of the expression on one line.

Examples3

  1. Example 1

    Input
    S< S< 12 >> 2 > >
    123 >> 1 >> 1
    1000000000   >>129
    S<S<S<S<S<2>>>>>
    S  <S< S<2013    >>> 11 >>> 10 >
    #
    
    Expected output
    81
    30
    0
    294967268
    14592400
    
  2. Example 2

    Input
    0
    1000000000
    S<0>
    S<1>
    12 >> 0
    #
    
    Expected output
    0
    1000000000
    0
    1
    12
    
  3. Example 3

    Input
    S<3>>>1
    S<S<3>>>>1
    1000000000 >> S<2>
    S<1000000000>
    #
    
    Expected output
    4
    40
    62500000
    49