#P10342. [THUSC 2019] 数列

[THUSC 2019] 数列

Problem Description

Define the value of a sequence of length kk as: $val = \max \limits_{1 \le i \le k}\{i \times f(i,k)\}$.

Here, f(i,j)f(i,j) denotes the number of distinct values within the interval [i,j][i,j].

Given a sequence of length nn, compute the sum of values over all contiguous subintervals. Treat each contiguous subinterval as an independent sequence; the computed value is exactly the value of that subinterval.

Let mm be the number of distinct values appearing in the sequence.

Input Format

The first line contains an integer nn.

The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n, describing the value at each position of the sequence.

For all input data, it holds that 1n1051 \le n \le 10^5.

Output Format

Output one integer in the first line, representing the answer modulo 998 244 353998\ 244\ 353.

4
12 23 23 45
23

Hint

[Sample Explanation 1]

All intervals of length 11 contribute 11 to the answer.

All intervals of length 22 contribute 22 to the answer.

The interval [1,3][1,3] contributes 33 to the answer.

The interval [2,4][2,4] contributes 44 to the answer.

The interval [1,4][1,4] contributes 66 to the answer.

Therefore, the answer is ans=4×1+2×3+3+4+6=23ans = 4 \times 1 + 2 \times 3 + 3 + 4 + 6 = 23.

[Sample 2]

See the attached files 2.in/ans.

[Sample 3]

See the attached files 3.in/ans.

[Sample 4]

See the attached files 4.in/ans.

[Sample 5]

See the attached files 5.in/ans.

[Subtasks]

Subtask ID nn \le mm \le aia_i \le Constraints Score
1 800800 10510^5 None 13
2 10510^5 No equal values will appear 15
3 8×1048\times 10^4 600600 In $[1,m][m+1,2m]\dots[\lfloor\frac{n}{m}\rfloor\times m + 1,n]$, each interval has no repeated values 21
4 10510^5 8080 None 24
5 8×1048\times 10^4 600600 27

Translated by ChatGPT 5