2069C.md (1795B)
1 Source: https://codeforces.com/problemset/problem/2069/C 2 3 C. Beautiful Sequence 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Let's call an integer sequence beautiful if the following conditions hold: 22 23 * its length is at least $3$; 24 * for every element except the first one, there is an element to the left less than it; 25 * for every element except the last one, there is an element to the right larger than it; 26 27 For example, $[1, 4, 2, 4, 7]$ and $[1, 2, 4, 8]$ are beautiful, but $[1, 2]$, $[2, 2, 4]$, and $[1, 3, 5, 3]$ are not. 28 29 Recall that a subsequence is a sequence that can be obtained from another sequence by removing some elements without changing the order of the remaining elements. 30 31 You are given an integer array $a$ of size $n$, where every element is from $1$ to $3$. Your task is to calculate the number of beautiful subsequences of the array $a$. Since the answer might be large, print it modulo $998244353$. 32 33 Input 34 35 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 36 37 The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$). 38 39 The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3$). 40 41 Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. 42 43 Output 44 45 For each test case, print a single integer — the number of beautiful subsequences of the array $a$, taken modulo $998244353$. 46 47 Example 48 49 Input 50 51 ``` 52 4 53 54 7 55 56 3 2 1 2 2 1 3 57 58 4 59 60 3 1 2 2 61 62 3 63 64 1 2 3 65 66 9 67 68 1 2 3 2 1 3 2 2 3 69 ``` 70 71 Output 72 73 ``` 74 3 75 0 76 1 77 22 78 ``` 79 80 Note 81 82 In the first test case of the example, the following subsequences are beautiful: 83 84 * $[a_3, a_4, a_7]$; 85 * $[a_3, a_5, a_7]$; 86 * $[a_3, a_4, a_5, a_7]$.