Problem:
Score : 700 points

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

Snuke will perform the following operation zero or more times:

Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1
:
C_N

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

Sample Input 1

5
1
2
1
2
2

Sample Output 1

3

We can make three sequences of colors of stones, as follows:

(1,2,1,2,2), by doing nothing.

(1,1,1,2,2), by choosing the first and third stones to perform the operation.

(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

Sample Input 2

6
4
2
5
4
2
4

Sample Output 2

5

Sample Input 3

7
1
3
1
2
3
3
2

Sample Output 3

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include<cstdio>
#define ll long long int
#define mod 1000000007
#define pi 3.141592653589793
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define vfind(a, e) find(a.begin(), a.end(), e)
#define forr(i, n) for (ll i = 0; i < n; i++)
#define rfor(i, n) for (ll i = n - 1; i >= 0; i--)
#define fors(i, b, e, steps) for(ll i = b; i < e; i += steps)
#define rfors(i, e, b, steps) for(ll i = e; i > b; i -= steps)
#define mp make_pair
using namespace std;


bool isVowel(char c){
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

bool isLower(char c){
    return 97 <= c && c <= 122;
}

void mul(ll a[2][2], ll b[2][2]) {
    ll p = a[0][0] * b[0][0] + a[0][1] * b[1][0];
    ll q = a[0][0] * b[0][1] + a[0][1] * b[1][1];
    ll r = a[1][0] * b[0][0] + a[1][1] * b[1][0];
    ll s = a[1][0] * b[0][1] + a[1][1] * b[1][1];

    a[0][0] = p % mod;
    a[0][1] = q % mod;
    a[1][0] = r % mod;
    a[1][1] = s % mod;
}



int main() {
    // cout << setprecision(30);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    ll n;
    cin >> n;
    vector<ll> a;
    ll e;
    cin >> e;
    a.pb(e);
    forr(i, n - 1) {
        cin >> e;
        if (e != a.back()) {
            a.pb(e);
        }
    }

    n = a.size();
    ll m = *max_element(a.begin(), a.end()) + 1;

    ll d[m], pre[m];
    fill (d, d + m, 0);
    fill (pre, pre + m, 0);
    pre[a[0]] = 1;
    d[a[0]] = 1;
    ll dp[n];
    dp[0] = 1;

    fors (i, 1, n, 1) {
        if (d[a[i]] == 0) {
            dp[i] = dp[i - 1];
        }

        else {
            dp[i] = dp[i - 1] + pre[a[i]];
            dp[i] %= mod;
        }

        d[a[i]]++;
        pre[a[i]] += dp[i - 1];
        pre[a[i]] %= mod;

    }


    cout << dp[n - 1];
}


//WA
//14
//1 2 3 1 4 5 6 7 4 8 9 10 11 8

//6
//1 2 1 3 4 3