Under construction

Still being built. Things may change

Tree Generators

Time limit2sMemory limit2048 MB

Summary
Given two expressions that build trees by joining subtrees with a random edge, count the distinct trees both expressions can generate, modulo 998244353.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Hash map
Solved
No attempts yet

Problem

Two expressions are given as input. Each one describes a procedure that generates a single tree. The procedure is randomized, so different trees may be generated each time it runs. Count the number of trees that can be generated from both expressions.

The syntax of an expression is as follows.

EE ::= ‘1’ | ‘(’ EE EE ‘)’

A tree is generated from an expression as follows.

  • The expression 1 generates a tree with a single vertex labeled 11.
  • For two expressions E1E_1 and E2E_2, the expression (E1E2E_1E_2) generates a tree as follows.
    • Generate a tree T1T_1 from E1E_1 with n1n_1 vertices, and a tree T2T_2 from E2E_2 with n2n_2 vertices.
    • Increment the labels of all vertices in T2T_2 by n1n_1.
    • Choose one vertex from T1T_1 and one vertex from T2T_2 at random. Adding an edge between them forms a single tree with vertices labeled 11 through (n1+n2)(n_1 + n_2). This is the tree generated by (E1E2E_1E_2).

For example, the expression (11) can generate only the leftmost tree in Figure D.1. The expression (1(11)) can generate the other two trees.

Figure D.1. Trees generated from the two expressions, (11) and (1(11))

The same tree can be generated from different expressions. The middle tree can also be generated from ((11)1).

For two expressions of the same length, count the trees that can be generated from both. The trees generated from the two expressions always have the same number of vertices. Two trees are different if there are two indices ii and jj such that vertices ii and jj are connected by an edge in one tree but not in the other.

Input

The input consists of two lines. Each line contains one expression string. The two strings have the same length, between 11 and 7×1057 \times 10^5, inclusive. Both strings follow the syntax given above.

Output

Print the number of trees that can be generated from both expressions, modulo 998 244 353998\,244\,353.

Hint

For Sample Input 1, the trees that can be generated from the two expressions are shown in Figure D.2. The top six trees come from the first expression, and the bottom four come from the second. Only the leftmost tree in each row can be generated from both.

Figure D.2. Illustration of Sample Input 1

Examples4

  1. Example 1

    Input
    ((1(11))1)
    ((11)(11))
    
    Expected output
    1
    
  2. Example 2

    Input
    (1(11))
    (1(11))
    
    Expected output
    2
    
  3. Example 3

    Input
    (((11)(11))((11)1))
    ((1(11))(1(1(11))))
    
    Expected output
    3
    
  4. Example 4

    Input
    ((11)(((1(11))1)((11)1)))
    (1(((11)((11)(11)))(11)))
    
    Expected output
    4