CF BUDDY
← Problems·

1767C · Count Binary Strings

2100 · data structures, dp

Problem: You are given an integer nn. You have to calculate the number of binary (consisting of characters 0 and/or 1) strings ss meeting the following constraints.

For every pair of integers (i,j)(i, j) such that 1ijn1 \le i \le j \le n, an integer ai,ja_{i,j} is given. It imposes the following constraint on the string sisi+1si+2sjs_i s_{i+1} s_{i+2} \dots s_j:

  • if ai,j=1a_{i,j} = 1, all characters in sisi+1si+2sjs_i s_{i+1} s_{i+2} \dots s_j should be the same;
  • if ai,j=2a_{i,j} = 2, there should be at least two different characters in sisi+1si+2sjs_i s_{i+1} s_{i+2} \dots s_j;
  • if ai,j=0a_{i,j} = 0, there are no additional constraints on the string sisi+1si+2sjs_i s_{i+1} s_{i+2} \dots s_j.

Count the number of binary strings ss of length nn meeting the aforementioned constraints. Since the answer can be large, print it modulo 998244353998244353.

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

Then nn lines follow. The ii-th of them contains ni+1n-i+1 integers ai,i,ai,i+1,ai,i+2,,ai,na_{i,i}, a_{i,i+1}, a_{i,i+2}, \dots, a_{i,n} (0ai,j20 \le a_{i,j} \le 2).

Output Format: Print one integer — the number of strings meeting the constraints, taken modulo 998244353998244353.

Note: In the first example, the strings meeting the constraints are 001, 010, 011, 100, 101, 110.

In the second example, the strings meeting the constraints are 001, 110.

Sample Cases

Case 1

Input

3
1 0 2
1 0
1

Output

6

Case 2

Input

3
1 1 2
1 0
1

Output

2

Case 3

Input

3
1 2 1
1 0
1

Output

0

Case 4

Input

3
2 0 2
0 1
1

Output

0

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