#P1370. Charlie 的云笔记序列

Charlie 的云笔记序列

Description

One day, Charlie abstracts his collected problems into a sequence. a=[a1,a2,a3,,an1,an]a = [a_1, a_2, a_3, \cdots, a_{n-1}, a_n].

Let a[l:r]a[l:r] denote the subarray of the sequence {ai}\{a_i\} from the ll-th element to the rr-th element, where 1lrn1 \le l \le r \le n. Formally, $a[l:r] = \{a_l, a_{l+1}, a_{l+2}, \cdots, a_{r-1}, a_r\}$. For example, if a=[9,8,0,3,2,1]a = [9, 8, 0, 3, 2, 1], then a[2:5]=[8,0,3,2]a[2:5] = [8, 0, 3, 2].

Charlie defines a function F(l,r)F(l, r) on the sequence [ai][a_i], which denotes the number of essentially different subsequences of a[l:r]a[l:r]. In particular, the empty sequence is also counted as one essentially different subsequence.

A subsequence of a[l:r]a[l:r] is defined as $[a_{i_1}, a_{i_2}, a_{i_3}, \cdots, a_{i_{k-1}}, a_{i_k}]$, where $l \le i_1 < i_2 < i_3 < \cdots < i_{k-1} < i_k \le r$. For example, if a=[9,8,0,3,2,1]a = [9, 8, 0, 3, 2, 1], then [8,3,2][8, 3, 2] is a subsequence of a[2:5]=[8,0,3,2]a[2:5] = [8, 0, 3, 2].

A sequence aa of length nn and a sequence bb of length mm are called essentially different if and only if nmn \ne m, or there exists ii such that aibia_i \ne b_i. Otherwise, the two sequences are called essentially the same. For example, [9,8][9, 8] and [9,7][9, 7] are essentially different; [9,8][9, 8] and [9,8,7][9, 8, 7] are also essentially different; while [9,8][9, 8] and [9,8][9, 8] are essentially the same.

For example, let a=[1,9,9,8,0,3,2,1]a = [1, 9, 9, 8, 0, 3, 2, 1]. Then F(1,3)=6F(1, 3) = 6, because a[1:3]=[1,9,9]a[1:3] = [1, 9, 9] has 66 subsequences: [],[1],[9],[1,9],[9,9],[1,9,9][], [1], [9], [1, 9], [9, 9], [1, 9, 9].

Now Charlie wants to know the value of 1lrnF(l,r)\sum_{1 \le l \le r \le n} F(l, r). Since this number may be large, please output it modulo 998244353998244353 (7×17×223+17 \times 17 \times 2^{23} + 1, a prime).

Input Format

The first line contains an integer nn, denoting the length of the sequence aa.

The second line contains nn integers, denoting a1,a2,a3,,an1,ana_1, a_2, a_3, \cdots, a_{n-1}, a_n.

Output Format

One line containing a single integer: the value of 1lrnF(l,r)\sum_{1 \le l \le r \le n} F(l, r) modulo 998244353998244353.

8
1 9 9 8 0 3 2 1
814

Hint

  • For 10%10\% of the testdata, 1n101 \le n \le 10.
  • For 30%30\% of the testdata, 1n1001 \le n \le 100.
  • For 50%50\% of the testdata, 1n10001 \le n \le 1000, 0ai1050 \le a_i \le 10^5.
  • For 100%100\% of the testdata, 1n1051 \le n \le 10^5, ai109|a_i| \le 10^9.

oiinhand 3.0 is under development.

This will be a must-have tool for OIers. It not only aggregates resources (problems, solutions) from all major OJs across the web, but also allows users to store code they have been judged on other OJs, and it has a very considerate cloud notes feature to help everyone practice with maximum efficiency.

soha is soliciting feedback here, with rewards! http://www.wenjuan.com/s/M7fqIv/

Translated by ChatGPT 5