#d11404. 子串(数据加强版)

子串(数据加强版)

Substrings (Hard Version)

Problem Description

Given a 0101-string of length nn, count the number of ways to partition it so that the sequence formed by the numbers of 1s in each resulting substring is a palindrome. Output the answer modulo 998244353998244353.

Input Format

The first line contains a positive integer nn, the length of the 0101-string.

The second line contains a 0101-string of length nn.

Output Format

The number of valid partition schemes, modulo 998244353998244353.

Sample Input

3
101

Sample Output

4

Sample Explanation

The valid partitions are: [101],[10,1],[1,01],[1,0,1][101], [10,1], [1,01], [1,0,1]. The sequences of the numbers of 1s are [2],[1,1],[1,1],[1,0,1][2], [1,1], [1,1], [1,0,1], all of which are palindromes.

Constraints

1≤n≤25001 \le n \le 2500

  • Advanced: think about an O(n)O(n) solution.