National Olympiad in Informatics, China, 2001
In the year 11380 CE, an enormous meteorite crashes into the south pole. Then, disaster breaks out. A series of abnormal phenomena begins to take place on earth. Just when earthlings are in the midst of panicking, an Antarctic expedition team made up of Chinese scientists rushed to the site of the incident. After researching, the scientists have discovered that the meteorite contains several lines of a mysterious text. Each line contains 5 integers, for example:
1 1 1 1 6
0 0 6 3 57
8 0 11 3 2845
The famous scientist SS has discovered that the text is actually the result of a complex computation. To make it easier for everyone to understand the computation, he defines a new type of SS expressions.
- SS expressions consist of only the characters
{
,}
,[
,]
,(
, and)
. - An empty sequence is an SS expression.
- If is an SS expression, and does not contain the characters
{
,}
,[
, or]
, then(
)
is an SS expression. - If is an SS expression, and does not contain the characters
{
, or}
, then[
]
is an SS expression. - If is an SS expression, then
{
}
is an SS expression. - If and are both SS expressions, then is also an SS expression.
For example:
()(())[]
{()[()]}
{{[[(())]]}}
are all SS expressions, but:
()([])()
[()
are not SS expressions.
The depth of an SS expression is equal to:
- , if is an empty sequence.
- , if is
{
}
or[
]
or(
)
, where is an SS expression. - , if is , and and are both SS expressions.
For instance, (){()}[]
has a depth of .
The complex computation in the secret text is performed as follows:
Let the first four integers of each line in the text be , ,
, and . Find the number of SS expressions with depth ,
containing exactly pairs of {}
, pairs of []
, and
pairs of ()
. Take this number modulo the current year . The
remainder is the fifth number of each line, which we will call the
"secret number".
For many lines of this secret text, the secret number has been blurred out. These numbers are in fact the key to revealing the secret behind the meteorite. Now, the scientists are inviting you to help them determine the secret number.
Input Specification
The input consists of one line, with 4 space-separated integers , , , and .
Output Specification
The output should consist of a single integer - the secret number.
Sample Input
1 1 1 2
Sample Output
8
Problem translated to English by .
Comments