#P7327. 「MCOI-07」Dream and Discs

「MCOI-07」Dream and Discs

Description

Dream has nn music discs, numbered from 11 to nn. Each disc stores exactly one song, and the song stored on disc ii is represented by a positive integer aia_i. The values aia_i satisfy 1ain1 \le a_i \le n.

Dream plans to uniformly randomly choose an index interval P1P_1 and a song interval S1S_1, where P1[1,n]P_1 \subseteq [1,n], S1[1,n]S_1 \subseteq [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 P1P_1, the songs are in S1S_1, 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 P2P_2 and S2S_2 again, where P2P1P_2 \subseteq P_1 and S2S1S_2 \subseteq S_1. This time, the set of discs he constructs must satisfy that the indices are in P2P_2 and the songs are in S2S_2. 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)(P_1, S_1, P_2, S_2).

Input Format

The first line contains a positive integer nn.
The second line contains nn positive integers, representing a1,a2,,ana_1, a_2, \dots, a_n in order.

Output Format

Suppose the answer can be written as p/qp/q, where pp and qq are coprime. Output pq1(mod109+7)p \cdot q^{-1} \pmod{10^9+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]P_1=[1,1],S_1=[1,1]
    • P2=[1,1],S2=[1,1],Δ=11=0P_2=[1,1],S_2=[1,1],\Delta=1-1=0
  • P1=[1,1],S1=[1,2]P_1=[1,1],S_1=[1,2]
    • P2=[1,1],S2=[1,1],Δ=11=0P_2=[1,1],S_2=[1,1],\Delta=1-1=0
    • P2=[1,1],S2=[1,2],Δ=11=0P_2=[1,1],S_2=[1,2],\Delta=1-1=0
    • P2=[1,1],S2=[2,2],Δ=10=1P_2=[1,1],S_2=[2,2],\Delta=1-0=1
  • P1=[1,1],S1=[2,2]P_1=[1,1],S_1=[2,2]
    • P2=[1,1],S2=[2,2],Δ=00=0P_2=[1,1],S_2=[2,2],\Delta=0-0=0
  • P1=[1,2],S1=[1,1]P_1=[1,2],S_1=[1,1]
    • P2=[1,1],S2=[1,1],Δ=11=0P_2=[1,1],S_2=[1,1],\Delta=1-1=0
    • P2=[1,2],S2=[1,1],Δ=11=0P_2=[1,2],S_2=[1,1],\Delta=1-1=0
    • P2=[2,2],S2=[1,1],Δ=10=1P_2=[2,2],S_2=[1,1],\Delta=1-0=1
  • P1=[1,2],S1=[1,2]P_1=[1,2],S_1=[1,2]
    • P2=[1,1],S2=[1,1],Δ=21=1P_2=[1,1],S_2=[1,1],\Delta=2-1=1
    • P2=[1,1],S2=[1,2],Δ=21=1P_2=[1,1],S_2=[1,2],\Delta=2-1=1
    • P2=[1,1],S2=[2,2],Δ=20=2P_2=[1,1],S_2=[2,2],\Delta=2-0=2
    • P2=[1,2],S2=[1,1],Δ=21=1P_2=[1,2],S_2=[1,1],\Delta=2-1=1
    • P2=[1,2],S2=[1,2],Δ=22=0P_2=[1,2],S_2=[1,2],\Delta=2-2=0
    • P2=[1,2],S2=[2,2],Δ=21=1P_2=[1,2],S_2=[2,2],\Delta=2-1=1
    • P2=[2,2],S2=[1,1],Δ=20=2P_2=[2,2],S_2=[1,1],\Delta=2-0=2
    • P2=[2,2],S2=[1,2],Δ=21=1P_2=[2,2],S_2=[1,2],\Delta=2-1=1
    • P2=[2,2],S2=[2,2],Δ=21=1P_2=[2,2],S_2=[2,2],\Delta=2-1=1
  • P1=[1,2],S1=[2,2]P_1=[1,2],S_1=[2,2]
    • P2=[1,1],S2=[2,2],Δ=10=1P_2=[1,1],S_2=[2,2],\Delta=1-0=1
    • P2=[1,2],S2=[2,2],Δ=11=0P_2=[1,2],S_2=[2,2],\Delta=1-1=0
    • P2=[2,2],S2=[2,2],Δ=11=0P_2=[2,2],S_2=[2,2],\Delta=1-1=0
  • P1=[2,2],S1=[1,1]P_1=[2,2],S_1=[1,1]
    • P2=[2,2],S2=[1,1],Δ=00=0P_2=[2,2],S_2=[1,1],\Delta=0-0=0
  • P1=[2,2],S1=[1,2]P_1=[2,2],S_1=[1,2]
    • P2=[2,2],S2=[1,1],Δ=10=1P_2=[2,2],S_2=[1,1],\Delta=1-0=1
    • P2=[2,2],S2=[1,2],Δ=11=0P_2=[2,2],S_2=[1,2],\Delta=1-1=0
    • P2=[2,2],S2=[2,2],Δ=11=0P_2=[2,2],S_2=[2,2],\Delta=1-1=0
  • P1=[2,2],S1=[2,2]P_1=[2,2],S_1=[2,2]
    • P2=[2,2],S2=[2,2],Δ=11=0P_2=[2,2],S_2=[2,2],\Delta=1-1=0

There are 2525 total choices. The sum of decreases over all choices is 1414, so the answer equals 14/2514/25.

Explanation for Sample 2

The answer is 144/225144/225.

Explanation for Sample 3

The answer is 5921/49005921/4900.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (11 pts): n8n \le 8.
  • Subtask 2 (17 pts): n64n \le 64.
  • Subtask 3 (19 pts): n1024n \le 1024.
  • Subtask 4 (7 pts): ai10a_i \le 10.
  • Subtask 5 (23 pts): n105n \le 10^5.
  • Subtask 6 (23 pts): no special restrictions.

For all testdata, 1n51051 \le n \le 5 \cdot 10^5, 1ain1 \le a_i \le n.

Translated by ChatGPT 5