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: Wrong Answer)

/*
オーダー
10**6	余裕を持って間に合う	
10**7	おそらく間に合う	余裕を持って間に合う
10**8	非常にシンプルな処理でない限り厳しい	おそらく間に合う
10**9		非常にシンプルな処理でない限り厳しい
logn	:OK
n		:10^7
nlogn	:10^6
n**2	:10^4
n**3	:300
2**n	:20
n!		:10



// 各桁の和を計算する関数
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

sort(a, a + N, greater<int>());  // a[0:N] を大きい順にソート

int num[110] = {0};  // バケット
for (int i = 0; i < N; ++i) {
    num[d[i]]++;  // d[i] が 1 個増える
}

map<string, int> mp; // 連想配列 map<キー型, 値型> オブジェクト名
for (int i = 0; i < N; ++i) {
    auto itr = mp.find(s[i]);        // s[i] が設定されているか？
    if(itr != mp.end() ) {
         mp[s[i]] += 1;
    }
    else {
         mp[s[i]] += 1 ;
    }
}
stack<int> s;	//intをデータとするスタックを用意
s.push(1);		//{} -> {1}
printf("%d\n", s.top());	// 3
s.pop();

queue<int> que;	//intをデータとするキューを用意
que.push(1);		//{} -> {1}
printf("%d\n", que.front());	// 1
que.pop();
*/

#include <iostream>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <string.h>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll,ll> P;
#define REP(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18
// int 
// // 各桁の和を計算する関数
// int findSumOfDigits(int n) {
//   int amari = 0;
//   int keta = 0;
//   while (n > 0) { // n が 0 になるまで
//     amari += n % 2;
//     if (keta%2==0)
//     n /= 10;
//   }
//   return sum;
// }
 
ll gcd(ll a, ll b) {
	if(b == 0) return a;
	return gcd(b, a % b);
}

vector<P> C_pair;
const ll n_div = 1e9+7;
const ll MAX_N = 2*1e5;
ll N;
ll num_pair;
ll C[MAX_N];
ll dp[MAX_N+1]; // dp[i ~ num_pairまでのC_pairを使った組み合わせ]
 
// i番目以降の品物から重さの総和がj以下になるように選ぶ
ll dfs(ll i){
	if (dp[i]!=-1){
		return dp[i];
	}
	else if (i==num_pair-1){
		dp[i] = 2;
		return dp[i];
	}
	else{
		ll idx_no_overlap = -1;
		for (ll j = i+1; j < num_pair; ++j){
			if (C_pair[i].second<=C_pair[j].first){
				// printf("!!!\n");
				idx_no_overlap = j;
			}
		}
		if (idx_no_overlap==-1){
			printf("i: %lld all overlap\n", i);
			dp[i] = dfs(i+1) + 1;
			dp[i] %= n_div;
		}
		else{
			// printf("i: %lld no overlap to %lld\n", i, idx_no_overlap);
			dp[i] = dfs(i+1) + dfs(idx_no_overlap);
			dp[i] %= n_div;
		}
		return dp[i];
	}
}
 
int main() {
	// 入力
	cin >> N;
	for (ll i = 0; i < N; ++i) cin >> C[i];
 
	// 解法
	// ペアのベクトルを作ってソート
	ll last[MAX_N];
	REP(i,MAX_N)last[i]=-1;
	
	REP(i, N){
		if (last[C[i]]!=-1 & last[C[i]]!=i-1){
			C_pair.pb(make_pair(last[C[i]],i));
		}
		last[C[i]] = i;
	}
	sort(C_pair.begin(), C_pair.end());
	num_pair = C_pair.size();
	
	// REP(i, C_pair.size()){
	// 	printf("i: %lld, %lld -> %lld\n", i, C_pair[i].first, C_pair[i].second);
	// }
	
	// DFS
	memset(dp, -1, sizeof(dp));
	
	// REP(i,num_pair){
	// 	printf("i: %lld, dp: %lld\n", i, dp[i]);
	// }
	if (num_pair==0) dp[0] = 1;
	for (ll i = num_pair-1; i>=0; --i){
		dfs(0);
	}
	// REP(i,num_pair){
	// 	printf("i: %lld, dp: %lld\n", i, dp[i]);
	// }
	
	cout << dp[0] << endl;
}