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: Accepted)

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    };
    ($($args:expr),*) => {
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($($args),*), ($($args),*));
    };
}

#[allow(unused_macros)]
macro_rules! put {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stdout(), "{}", $var);
    };
    ($var:expr, $($args:expr),*) => {
        let _ = write!(&mut std::io::stdout(), "{} ", $var);
        put!($($args),*);
    };
}

const M: usize = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let mut cs = vec![0; n];
    {
        let mut m = 0;
        let mut last = 0;
        for _ in 0..n {
            let c: usize = sc.cin();
            if c != last {
                last = c;
                cs[m] = c;
                m += 1;
            }
        }
        while cs.len() > m {
            cs.pop();
        }
    }

    // trivial case
    {
        let mut count = vec![0; 200002];
        let mut uniq = true;
        for &c in cs.iter() {
            count[c] += 1;
            if count[c] > 1 {
                uniq = false;
                break;
            }
        }
        if uniq {
            put!(1);
            return;
        }
    }

    // trace!(cs);
    let mut f = vec![0; cs.len() + 1];
    f[0] = 1;
    // f[i] = f(0..i)
    let mut e = vec![0; cs.iter().max().unwrap() + 2];
    // e[i] = sum f(0..k-1) where cs[k] == i
    for i in 1 .. cs.len() + 1 {
        let alphabet = cs[i-1];
        f[i] = e[alphabet] + f[i-1];
        e[alphabet] += f[i-1];
        f[i] %= M;
        e[alphabet] %= M;
        // println!("ends with {} is {}", alphabet, e[alphabet]);
        // trace!(f);
        // trace!(e);
    }
    // trace!(f);
    // trace!(e);
    put!(f[f.len() - 1]);
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
}