#P6477. [NOI Online #2 提高组] 子序列问题

[NOI Online #2 提高组] 子序列问题

Background

2s 512M

Problem Description

Given a positive integer sequence A1A_1, A2A_2, \cdots, AnA_n of length nn. Define a function f(l,r)f(l,r) as: the number of distinct integers in the subarray whose indices are in the range [l,r][l,r]. In other words, f(l,r)f(l,r) is the size of the set {Al,Al+1,,Ar}\{A_l,A_{l+1},\cdots,A_r\}. This set is not a multiset, meaning all elements in the set are different.

Now, please compute l=1nr=ln(f(l,r))2\sum_{l=1}^n\sum_{r=l}^n (f(l,r))^2. Since the answer may be very large, output the result modulo 109+710^9 +7.

Input Format

The first line contains a positive integer nn, representing the length of the sequence.

The second line contains nn positive integers, separated by spaces, representing the sequence A1A_1, A2A_2, \cdots, AnA_n.

Output Format

Only one line containing a non-negative integer, representing the answer modulo 109+710^9+7.

4
2 1 3 2
43
3
1 1 1
6

Hint

For 10%10\% of the testdata, 1n101 \leq n \leq 10.

For 30%30\% of the testdata, 1n1001 \leq n \leq 100.

For 50%50\% of the testdata, 1n1031 \leq n \leq 10^3.

For 70%70\% of the testdata, 1n1051 \leq n \leq 10^5.

For 100%100\% of the testdata, 1n1061 \leq n \leq 10^6, and each number in the set is in the range [1,109][1,10^9].

Translated by ChatGPT 5