Description
Dream has n music discs, numbered from 1 to n. Each disc stores exactly one song, and the song stored on disc i is represented by a positive integer ai. The values ai satisfy 1≤ai≤n.
Dream plans to uniformly randomly choose an index interval P1 and a song interval S1, where P1⊆[1,n], S1⊆[1,n], and all interval endpoints are positive integers.
After choosing the two intervals, he will select as many discs as possible such that the disc indices are in P1, the songs are in S1, and all chosen songs are pairwise distinct. He plans to give this set of discs to Tommy.
After constructing the set, Dream finds that he selected too many discs. He decides to use the above method to uniformly randomly choose two intervals P2 and S2 again, where P2⊆P1 and S2⊆S1. This time, the set of discs he constructs must satisfy that the indices are in P2 and the songs are in S2. Dream still selects a maximum-size set with all songs pairwise distinct.
Dream will never choose an empty interval.
Now Tommy feels that Dream gave him too few discs. Please help Tommy compute the average decrease in the size of the set caused by Dream’s second selection.
Here, the average is taken uniformly over all valid choices of (P1,S1,P2,S2).
The first line contains a positive integer n.
The second line contains n positive integers, representing a1,a2,…,an in order.
Suppose the answer can be written as p/q, where p and q are coprime. Output p⋅q−1(mod109+7).
2
1 2
920000007
3
1 1 2
480000004
5
1 2 1 3 2
734081639
Hint
Explanation for Sample 1
- P1=[1,1],S1=[1,1]
- P2=[1,1],S2=[1,1],Δ=1−1=0
- P1=[1,1],S1=[1,2]
- P2=[1,1],S2=[1,1],Δ=1−1=0
- P2=[1,1],S2=[1,2],Δ=1−1=0
- P2=[1,1],S2=[2,2],Δ=1−0=1
- P1=[1,1],S1=[2,2]
- P2=[1,1],S2=[2,2],Δ=0−0=0
- P1=[1,2],S1=[1,1]
- P2=[1,1],S2=[1,1],Δ=1−1=0
- P2=[1,2],S2=[1,1],Δ=1−1=0
- P2=[2,2],S2=[1,1],Δ=1−0=1
- P1=[1,2],S1=[1,2]
- P2=[1,1],S2=[1,1],Δ=2−1=1
- P2=[1,1],S2=[1,2],Δ=2−1=1
- P2=[1,1],S2=[2,2],Δ=2−0=2
- P2=[1,2],S2=[1,1],Δ=2−1=1
- P2=[1,2],S2=[1,2],Δ=2−2=0
- P2=[1,2],S2=[2,2],Δ=2−1=1
- P2=[2,2],S2=[1,1],Δ=2−0=2
- P2=[2,2],S2=[1,2],Δ=2−1=1
- P2=[2,2],S2=[2,2],Δ=2−1=1
- P1=[1,2],S1=[2,2]
- P2=[1,1],S2=[2,2],Δ=1−0=1
- P2=[1,2],S2=[2,2],Δ=1−1=0
- P2=[2,2],S2=[2,2],Δ=1−1=0
- P1=[2,2],S1=[1,1]
- P2=[2,2],S2=[1,1],Δ=0−0=0
- P1=[2,2],S1=[1,2]
- P2=[2,2],S2=[1,1],Δ=1−0=1
- P2=[2,2],S2=[1,2],Δ=1−1=0
- P2=[2,2],S2=[2,2],Δ=1−1=0
- P1=[2,2],S1=[2,2]
- P2=[2,2],S2=[2,2],Δ=1−1=0
There are 25 total choices. The sum of decreases over all choices is 14, so the answer equals 14/25.
Explanation for Sample 2
The answer is 144/225.
Explanation for Sample 3
The answer is 5921/4900.
Constraints
This problem uses bundled testdata.
- Subtask 1 (11 pts): n≤8.
- Subtask 2 (17 pts): n≤64.
- Subtask 3 (19 pts): n≤1024.
- Subtask 4 (7 pts): ai≤10.
- Subtask 5 (23 pts): n≤105.
- Subtask 6 (23 pts): no special restrictions.
For all testdata, 1≤n≤5⋅105, 1≤ai≤n.
Translated by ChatGPT 5