Under construction

Still being built. Things may change

Breaking the Cipher

Interview

Time limit2sMemory limit1024 MB

Summary
Given a sequence of n numbers, find the sum over all subsegments of the count of distinct subsequences (empty included), modulo 1000000007.
Level

Medium6 of 10

Topics
Dynamic programming, Hash map, Array
Solved
No attempts yet

Statement

Sherlock Holmes can solve any puzzle, but not every mystery yields at once. Half a year ago a courier brought him a letter written in a cipher. The letter had no key. The return address on the envelope was unusually long: instead of a street and house number, it listed nn numbers. Holmes tried everything, and none of it helped.

This morning Inspector Lestrade was waiting at Holmes's door with news. Scotland Yard had caught a member of the gang of the mysterious Professor M., and the man agreed to reveal the cipher used in the criminal network in exchange for a lighter sentence. Like all brilliant things, the cipher turned out to be very simple. The key is the sum, over all subsegments, of the number of distinct subsequences that can be formed using only the numbers of that subsegment.

A subsegment of a sequence of length nn is a pair l,rl, r with 1≤l≤r≤n1 \le l \le r \le n, written [l,r][l, r]. A subsequence of [l,r][l, r] is a collection ai1,ai2,…,aika_{i_1}, a_{i_2}, \ldots, a_{i_k} (possibly empty) with l≤i1<i2<…<ik≤rl \le i_1 < i_2 < \ldots < i_k \le r. Two subsequences aia_i and aja_j are considered different if they have different lengths, or if there is a kk such that aik≠ajka_{i_k} \ne a_{j_k}.

Since the number may be very large, output it modulo 1 000 000 0071\,000\,000\,007. Holmes is not strong in mathematics, so he needs your help finding the key.

Input

The first line contains one integer nn (1≤n≤100 0001 \le n \le 100\,000), the length of the sequence.

The second line contains nn integers aia_i (1≤ai≤100 0001 \le a_i \le 100\,000), the elements of the sequence.

Output

Print the key modulo 1 000 000 0071\,000\,000\,007 on a single line.

Hint

Explanation of the first sample:

For the multiset {1,1,2}\{1, 1, 2\} there are six subsegments.

  • Subsegment [1,1][1, 1] is {1}\{1\} (2 subsequences: {}\{\}, {1}\{1\}).
  • Subsegment [2,2][2, 2] is {1}\{1\} (2 subsequences: {}\{\}, {1}\{1\}).
  • Subsegment [3,3][3, 3] is {2}\{2\} (2 subsequences: {}\{\}, {2}\{2\}).
  • Subsegment [1,2][1, 2] is {1,1}\{1, 1\} (3 subsequences: {}\{\}, {1}\{1\}, {1,1}\{1, 1\}).
  • Subsegment [2,3][2, 3] is {1,2}\{1, 2\} (4 subsequences: {}\{\}, {1}\{1\}, {2}\{2\}, {1,2}\{1, 2\}).
  • Subsegment [1,3][1, 3] is {1,1,2}\{1, 1, 2\} (6 subsequences: {}\{\}, {1}\{1\}, {2}\{2\}, {1,1}\{1, 1\}, {1,2}\{1, 2\}, {1,1,2}\{1, 1, 2\}).

The answer is 2+2+2+3+4+6=192+2+2+3+4+6=19.

Examples2

  1. Example 1

    Input
    3
    1 1 2
    
    Expected output
    19
    
  2. Example 2

    Input
    6
    1 2 2 3 1 2
    
    Expected output
    185