This page is still under construction.

Parts of this page are still being built. What you see may change.

Variable Subsequences

Time limit2sMemory limit512 MB

Summary
Count distinct nonempty subsequences, identified by position sets, in which every two consecutive terms differ.
Level

Medium7 of 10

Topics
Dynamic programming, Combinatorics, Array
Solved
No attempts yet

Problem

We are looking for variable subsequences of a given sequence a=(a1,a2,…,an)a = (a_1, a_2, \dots, a_n). A subsequence is obtained by removing any number of terms (possibly none). Formally, a subsequence of aa is any sequence (ai1,ai2,…,aik)(a_{i_1}, a_{i_2}, \dots, a_{i_k}) with 1≤i1<i2<⋯<ik≤n1 \le i_1 < i_2 < \dots < i_k \le n.

A variable subsequence is a subsequence in which every two consecutive terms are different. For example, (1,3,1,2)(1, 3, 1, 2) is a variable subsequence of (1,2,3,1,3,2,2)(1, 2, 3, 1, 3, 2, 2).

We want to count how many distinct, nonempty variable subsequences the sequence has. Two subsequences are distinct if their sets of positions in aa are different. For instance, (1,2,3,1,3,2,2)(1, 2, 3, 1, 3, 2, 2) contains two distinct variable subsequences of the form (1,3,1,2)(1, 3, 1, 2).

Input

The first line contains one integer nn (2≤n≤500,0002 \le n \le 500{,}000), the length of the sequence aa. The second line contains nn integers aia_i (1≤ai≤500,0001 \le a_i \le 500{,}000) separated by spaces.

Output

Print a single integer on one line: the number of nonempty variable subsequences of the input sequence modulo 109+710^9 + 7.

Hint

For the sequence (1,2,1,1)(1, 2, 1, 1), the counted variable subsequences are:

  • (1)(1): counted three times (positions 1, 3, and 4)
  • (2)(2): counted once
  • (1,2)(1, 2): counted once
  • (2,1)(2, 1): counted twice
  • (1,2,1)(1, 2, 1): counted twice

In total there are 3+1+1+2+2=93 + 1 + 1 + 2 + 2 = 9 of them.

Examples3

  1. Example 1

    Input
    4
    1 2 1 1
    
    Expected output
    9
    
  2. Example 2

    Input
    2
    5 5
    
    Expected output
    2
    
  3. Example 3

    Input
    2
    3 7
    
    Expected output
    3