Tree Generators
시간 제한2초메모리 제한2048 MB
각각 무작위로 트리를 만드는 두 괄호 표현식이 주어질 때, 두 표현식 모두에서 만들어질 수 있는 트리의 수를 998244353으로 나눈 나머지로 구한다.
문제
One of the problems in the International Parsing Contest caught your attention.
Two expressions are given as input, each representing a procedure to generate a single tree. The generation procedure is randomized, meaning different trees may be generated each time the procedure is performed. You are asked to count the number of trees that can possibly be generated from both of the expressions.
The syntax of an expression is as follows.
::= ‘1’ | ‘(’ ‘)’
A tree is generated from an expression according to the following procedure.
-
The expression
1generates a tree with a single vertex labeled . -
For two expressions and , an expression
()generates a tree as follows:- A tree is generated from with vertices, and from with vertices.
- Then, the labels of all vertices in are incremented by .
- After that, two vertices, one from and the other from , are randomly chosen. Adding an edge connecting them forms a single tree with vertices labeled through , which is the tree generated by
().
For example, the expression (11) can generate only the leftmost tree in Figure D.1, while (1(11)) can generate the remaining two trees.

Figure D.1. Trees generated from the two expressions, (11) and (1(11))
The same tree may be generated from different expressions. The middle tree can also be generated from ((11)1).
For given two expressions of the same length, count the number of trees that can be generated from both of the expressions. Note that the trees generated from them always have the same number of vertices. Two trees are considered different if there exist two indices and such that vertices labeled and are connected by an edge in one tree but not in the other.
입력
The input consists of two lines, each containing an expression string. The two strings have the same length, between and , inclusive, and follow the syntax given above.
출력
Output the number of trees that can be generated from both expressions modulo .
힌트
For Sample Input 1, the trees that can be generated from the two expressions are shown in Figure D.2. The top six trees correspond to the first expression and the bottom four correspond to the second. Only the leftmost tree in each row can be generated from both.

Figure D.2. Illustration of Sample Input 1