#abc371e. E - I Hate Sigma Problems

E - I Hate Sigma Problems

Score : 475475 points

问题陈述

给定一个长度为 NN 的整数序列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N)。定义 f(l,r)f(l, r) 为:

  • 子序列 (Al,Al+1,,Ar)(A_l, A_{l+1}, \ldots, A_r) 中不同值的数量。

计算以下表达式:

i=1Nj=iNf(i,j)\displaystyle \sum_{i=1}^{N}\sum_{j=i}^N f(i,j)

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a sequence of integers A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of length NN. Define f(l,r)f(l, r) as:

  • the number of distinct values in the subsequence (Al,Al+1,,Ar)(A_l, A_{l+1}, \ldots, A_r).

Evaluate the following expression:

i=1Nj=iNf(i,j)\displaystyle \sum_{i=1}^{N}\sum_{j=i}^N f(i,j).

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1AiN1\leq A_i\leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 \ldots ANA_N

Output

Print the answer.

Sample Input 1

3
1 2 2

Sample Output 1

8

Consider f(1,2)f(1,2). The subsequence (A1,A2)=(1,2)(A_1, A_2) = (1,2) contains 22 distinct values, so f(1,2)=2f(1,2)=2.

Consider f(2,3)f(2,3). The subsequence (A2,A3)=(2,2)(A_2, A_3) = (2,2) contains 11 distinct value, so f(2,3)=1f(2,3)=1.

The sum of ff is 88.

Sample Input 2

9
5 4 2 2 3 2 4 4 1

Sample Output 2

111