CF BUDDY
← Problems·

1671E · Preorder

2100 · combinatorics, divide and conquer, dp

Problem: You are given a rooted tree of 2n12^n - 1 vertices. Every vertex of this tree has either 00 children, or 22 children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree.

The vertices of the tree are numbered in the following order:

  • the root has index 11;
  • if a vertex has index xx, then its left child has index 2x2x, and its right child has index 2x+12x+1.

Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex xx as sxs_x.

Let the preorder string of some vertex xx be defined in the following way:

  • if the vertex xx is a leaf, then the preorder string of xx be consisting of only one character sxs_x;
  • otherwise, the preorder string of xx is sx+f(lx)+f(rx)s_x + f(l_x) + f(r_x), where ++ operator defines concatenation of strings, f(lx)f(l_x) is the preorder string of the left child of xx, and f(rx)f(r_x) is the preorder string of the right child of xx.

The preorder string of the tree is the preorder string of its root.

Now, for the problem itself...

You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree:

  • choose any non-leaf vertex xx, and swap its children (so, the left child becomes the right one, and vice versa).

Input Format: The first line contains one integer nn (2n182 \le n \le 18).

The second line contains a sequence of 2n12^n-1 characters s1,s2,,s2n1s_1, s_2, \dots, s_{2^n-1}. Each character is either A or B. The characters are not separated by spaces or anything else.

Output Format: Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo 998244353998244353.

Sample Cases

Case 1

Input

4
BAAAAAAAABBABAB

Output

16

Case 2

Input

2
BAA

Output

1

Case 3

Input

2
ABA

Output

2

Case 4

Input

2
AAB

Output

2

Case 5

Input

2
AAA

Output

1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback