Олимпиадный тренинг

Задача 27217. COWBASIC


Задача

Темы:
Besi has invented a new programming language, but since there is no compiler, she needs your help to run her programs.
COWBASIC is a simple, elegant language. It has two main features: addition and cycles. To solve the overflow problem, Besi performs all addition operations modulo 109+7. An MOO loop executes a block of code a fixed number of times. Loops and additions can be nested.
 
You are given a COWBASIC program, determine the result of its execution - the number that it will return.
 
INPUT FORMAT:
 
You are given a COWBASIC program with a maximum length of 100 lines, each line with a maximum length of 350 characters. A COWBASIC program is a list of statements.
There are three types of operators:
 
<variable> = <expression>
 
<literal> MOO {
  <list of statements>
}
 
RETURN <variable>
There are three types of expressions:
 
<literal>
 
<variable>
 
( <expression> ) + ( <expression> )
 
A literal is a positive integer no greater than 100,000.
 
A variable is a string of no more than 10 small Latin letters.
 
It is guaranteed that a variable will never be used or returned by a RETURN statement before it has been defined. It is guaranteed that the RETURN statement will only occur once on the last line of the program.
 
OUTPUT FORMAT:
 
Print one positive integer - the value of the variable returned by the RETURN operator.
ASSESSMENT
 
20% of tests do not have MOO loops nested.
In the other 20% of all tests, the program will have only one variable. MOO loops can be nested
There are no restrictions in other tests.

Enter Output Note
x=1
10 MOO
  x = ( x ) + ( x )
}
RETURN x
1024 This COWBASIC program calculates 210
n=1
nsq = 1
100000 MOO
  100000 MOO
    nsq = ( nsq ) + ( ( n ) + ( ( n ) + ( 1 ) ) )
    n = ( n ) + ( 1 )
  }
}
RETURN nsq
4761 This program calculates (105∗105+1)2 (modulo 109+ 7).
.