Breaking the Cipher
InterviewTime limit2sMemory limit1024 MB
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 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 is a pair with , written . A subsequence of is a collection (possibly empty) with . Two subsequences and are considered different if they have different lengths, or if there is a such that .
Since the number may be very large, output it modulo . Holmes is not strong in mathematics, so he needs your help finding the key.
Input
The first line contains one integer (), the length of the sequence.
The second line contains integers (), the elements of the sequence.
Output
Print the key modulo on a single line.
Hint
Explanation of the first sample:
For the multiset there are six subsegments.
- Subsegment is (2 subsequences: , ).
- Subsegment is (2 subsequences: , ).
- Subsegment is (2 subsequences: , ).
- Subsegment is (3 subsequences: , , ).
- Subsegment is (4 subsequences: , , , ).
- Subsegment is (6 subsequences: , , , , , ).
The answer is .