아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Master Zhu and Binary Trees

시간 제한1초메모리 제한512 MB

요약
커서 이동과 부분 트리 삽입으로 이루어진 유효한 로그가 주어질 때, 그 로그와 일치하는 서로 다른 이진 트리 모양의 개수를 1e9+7로 나눈 나머지로 구합니다.
난이도

보통10점 중 7점

유형
동적 계획법, 트리, 조합론
정답자
아직 제출이 없습니다

문제

One day, Master Zhu invented an interesting game which he named Tree Maker. In this game, all trees are binary trees.

Initially, there is a tree with only one vertex and a cursor on it. In order to build the tree, the player can control the cursor to perform five operations described below:

  • "0": Jump to the parent of the current vertex (it must exist).
  • "1": Jump to the left child of the current vertex (it must exist).
  • "2": Jump to the right child of the current vertex (it must exist).
  • "3 xx": Generate an arbitrary binary tree with xx vertices and make it the left subtree of the current vertex (before such operation, the current vertex must have no left child).
  • "4 xx": Generate an arbitrary binary tree with xx vertices and make it the right subtree of the current vertex (before such operation, the current vertex must have no right child).

When an operation is performed, the log system writes down a record of it.

Rin played this game for a whole day yesterday. As a forgetful man, although Rin knew the shape of the tree while playing, after a sleep he forgot it. All he has now is the log of operations. Rin wants to know: according to the log, how many possible shapes the tree could have had yesterday after all operations?

Can you answer this question? As the answer may be very large, it is sufficient to find it modulo 109+710^{9} + 7.

입력

The first line of input contains an integer nn denoting the number of lines in the log (1≤n≤5001 \le n \le 500).

Then follow nn lines of the log. The format of the log is as described above.

It is guaranteed that, for every operation of the types "3" and "4", the integer xx is positive, and the total number of vertices in the tree will never exceed 500500. You can also assume that there exists at least one tree such that the given log is valid for that tree.

출력

Print a single line with a single integer: the answer to Rin's question modulo 109+710^{9} + 7.

예제2

  1. 예제 1

    입력
    2
    3 3
    4 3
    
    예상 출력
    25
    
  2. 예제 2

    입력
    2
    3 3
    1
    
    예상 출력
    5