CF BUDDY
← Problems·

1738E · Balance Addicts

2300 · combinatorics, dp, math

Problem: Given an integer sequence a1,a2,,ana_1, a_2, \dots, a_n of length nn, your task is to compute the number, modulo 998244353998244353, of ways to partition it into several non-empty continuous subsequences such that the sums of elements in the subsequences form a balanced sequence.

A sequence s1,s2,,sks_1, s_2, \dots, s_k of length kk is said to be balanced, if si=ski+1s_{i} = s_{k-i+1} for every 1ik1 \leq i \leq k. For example, [1,2,3,2,1][1, 2, 3, 2, 1] and [1,3,3,1][1,3,3,1] are balanced, but [1,5,15][1,5,15] is not.

Formally, every partition can be described by a sequence of indexes i1,i2,,iki_1, i_2, \dots, i_k of length kk with 1=i1<i2<<ikn1 = i_1 < i_2 < \dots < i_k \leq n such that

  1. kk is the number of non-empty continuous subsequences in the partition;
  2. For every 1jk1 \leq j \leq k, the jj-th continuous subsequence starts with aija_{i_j}, and ends exactly before aij+1a_{i_{j+1}}, where ik+1=n+1i_{k+1} = n + 1. That is, the jj-th subsequence is aij,aij+1,,aij+11a_{i_j}, a_{i_j+1}, \dots, a_{i_{j+1}-1}.

Let s1,s2,,sks_1, s_2, \dots, s_k denote the sums of elements in the subsequences with respect to the partition i1,i2,,iki_1, i_2, \dots, i_k. Formally, for every 1jk1 \leq j \leq k, sj=i=ijij+11ai=aij+aij+1++aij+11.s_j = \sum_{i=i_{j}}^{i_{j+1}-1} a_i = a_{i_j} + a_{i_j+1} + \dots + a_{i_{j+1}-1}. For example, the partition [12,34,5,6][1\,|\,2,3\,|\,4,5,6] of sequence [1,2,3,4,5,6][1,2,3,4,5,6] is described by the sequence [1,2,4][1,2,4] of indexes, and the sums of elements in the subsequences with respect to the partition is [1,5,15][1,5,15].

Two partitions i1,i2,,iki_1, i_2, \dots, i_k and i1,i2,,iki'_1, i'_2, \dots, i'_{k'} (described by sequences of indexes) are considered to be different, if at least one of the following holds.

  • kkk \neq k',
  • ijiji_j \neq i'_j for some 1jmin{k,k}1 \leq j \leq \min\left\{ k, k' \right\}.

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1n1051 \leq n \leq 10^5), indicating the length of the sequence aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai1090 \leq a_i \leq 10^9), indicating the elements of the sequence aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, output the number of partitions with respect to which the sum of elements in each subsequence is balanced, modulo 998244353998244353.

Note: For the first test case, there is only one way to partition a sequence of length 11, which is itself and is, of course, balanced.

For the second test case, there are 22 ways to partition it:

  • The sequence [1,1][1, 1] itself, then s=[2]s = [2] is balanced;
  • Partition into two subsequences [11][1\,|\,1], then s=[1,1]s = [1, 1] is balanced.

For the third test case, there are 33 ways to partition it:

  • The sequence [0,0,1,0][0, 0, 1, 0] itself, then s=[1]s = [1] is balanced;
  • [00,10][0 \,|\, 0, 1 \,|\, 0], then s=[0,1,0]s = [0, 1, 0] is balanced;
  • [0,010][0, 0 \,|\, 1 \,|\, 0], then s=[0,1,0]s = [0, 1, 0] is balanced.

For the fourth test case, there are 44 ways to partition it:

  • The sequence [1,2,3,2,1][1, 2, 3, 2, 1] itself, then s=[9]s = [9] is balanced;
  • [1,232,1][1, 2 \,|\, 3 \,|\, 2, 1], then s=[3,3,3]s = [3, 3, 3] is balanced;
  • [12,3,21][1 \,|\, 2, 3, 2 \,|\, 1], then s=[1,7,1]s = [1, 7, 1] is balanced;
  • [12321][1 \,|\, 2 \,|\, 3 \,|\, 2 \,|\, 1], then s=[1,2,3,2,1]s = [1, 2, 3, 2, 1] is balanced.

For the fifth test case, there are 22 ways to partition it:

  • The sequence [1,3,5,7,9][1, 3, 5, 7, 9] itself, then s=[25]s = [25] is balanced;
  • [1,3,579][1, 3, 5 \,|\, 7 \,|\, 9], then s=[9,7,9]s = [9, 7, 9] is balanced.

For the sixth test case, every possible partition should be counted. So the answer is 2321150994942(mod998244353)2^{32-1} \equiv 150994942 \pmod {998244353}.

Sample Cases

Case 1

Input

6
1
1000000000
2
1 1
4
0 0 1 0
5
1 2 3 2 1
5
1 3 5 7 9
32
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Output

1
2
3
4
2
150994942

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