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 Rust (1.15.1) (judge verdict: Runtime Error)

#![allow(non_snake_case,unused_imports,dead_code)]
use std::io::Read;
use std::cmp::max;
use std::cmp::min;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::BinaryHeap;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut buf_it = buf.split_whitespace();
    let N = buf_it.next().unwrap().parse::<usize>().unwrap();
    let C = (0..N).map(|_| buf_it.next().unwrap().parse::<usize>().unwrap()).collect::<Vec<_>>();

    let MOD = 1_000_000_007;
    let mut ans = ModInt::new(1,MOD);
    let mut cnt = (0..N).map(|_| ModInt::new(0,MOD)).collect::<Vec<_>>();
    cnt[C[0]-1] = ModInt::new(1,MOD);
    for i in 1..N{
        if C[i-1]==C[i]{continue;}
        let c = C[i] -1;
        ans = ans + cnt[c];
        cnt[c] = ans.clone();
    }
    println!("{}",ans.n);

}

use std::ops::{Add, Sub, Mul, Div};

#[derive(Copy, Clone)]
struct ModInt{
    n: isize,
    m: isize
}

impl ModInt {
    fn new(n:isize,m:isize) -> ModInt{
        ModInt {n:(n%m+m)%m, m:m}
    }
    fn pow_mod(a:isize,b:isize,m:isize) -> isize{
        if b==0{ return 1;}
        if b%2==1{ return a*ModInt::pow_mod(a,b-1,m)%m}
        ModInt::pow_mod(a*a%m,b/2,m)
    }
    fn inv(self) -> ModInt{
        let n = ModInt::pow_mod(self.n,self.m-2,self.m);
        ModInt::new(n,self.m)
    }
}
impl Add for ModInt{
    type Output = ModInt;
    fn add(self, other:ModInt) -> ModInt
    {
        ModInt::new(self.n+ other.n,self.m)
    }
}
impl Add<isize> for ModInt{
    type Output = ModInt;
    fn add(self, other:isize) -> ModInt
    {
        ModInt::new(self.n+ other,self.m)
    }
}
impl Sub for ModInt{
    type Output = ModInt;
    fn sub(self, other:ModInt) -> ModInt
    {
        ModInt::new(self.n-other.n,self.m)
    }
}
impl Sub<isize> for ModInt{
    type Output = ModInt;
    fn sub(self, other:isize) -> ModInt
    {
        ModInt::new(self.n-other,self.m)
    }
}
impl Mul for ModInt{
    type Output = ModInt;
    fn mul(self, other:ModInt) -> ModInt
    {
        ModInt::new(self.n*other.n,self.m)
    }
}
impl Mul<isize> for ModInt{
    type Output = ModInt;
    fn mul(self, other:isize) -> ModInt
    {
        ModInt::new(self.n*other,self.m)
    }
}
impl Div for ModInt{
    type Output = ModInt;
    fn div(self, other:ModInt) -> ModInt
    {
        other.inv() * self
    }
}
impl Div<isize> for ModInt{
    type Output = ModInt;
    fn div(self, other:isize) -> ModInt
    {
        ModInt::new(other,self.m).inv() * self
    }
}