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 Python (3.4.3) (judge verdict: Accepted)

import sys,bisect

sys.setrecursionlimit(10000000)
N = int(input())
C = [int(input()) for _ in range(N)] #color
v = 10**9+7
from collections import defaultdict
d = defaultdict(list)
for i,c in enumerate(C):
    d[c].append(i)


def guchoku(l):
    assert isinstance(l,list)
    if len(l) <=2:
        return 1
    top_coor = l[0]
    if top_coor in l[1:] and l[0] != l[1]:
        _index= l[1:].index(top_coor)
        return guchoku(l[_index+1:])+ guchoku(l[1:])
    return guchoku(l[1:])

cache = dict()
cache[N] = 1
cache[N-1] = 1
cache[N-2] = 1

def check(i):
    index = bisect.bisect_left(d[C[i]],i+1)

    if index==len(d[C[i]]):
        return -1
    # print(i, d[C[i]] ,d[C[i]][index])
    return d[C[i]][index]

def guchoku_i(i):
    # print("hoge",i,d[C[i]])
    if i<0:
        raise
    if i in cache:
        return cache[i]
    c = check(i)
    if C[i] != C[i+1] and c !=-1 :
        cache[i]= guchoku_i(i+1) + guchoku_i(c) %v
        return cache[i]
    cache[i]=  guchoku_i(i+1) %v
    return cache[i]

# print(guchoku(C))
print(guchoku_i(0)%v )