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)

use std::io;
use std::io::BufRead;


fn main() {
    let mut sc = Scanner::new(std::io::stdin());
    let n: usize = sc.next().unwrap();
    let mut cs: Vec<usize> = vec![0; n];
    let mut prev = vec![-1i32; n];
    let mut nxt = vec![-1i32; n];
    for i in 0..n {
        cs[i] = sc.next().unwrap();
        if prev[cs[i]] >= 0 {
            nxt[prev[cs[i]] as usize] = i as i32;
        }
        prev[cs[i]] = i as i32;
    }
    
    let mut dp = vec![0i32; n+1];
    dp[n] = 1;
    for i in (0..n).rev() {
        dp[i] += dp[i + 1];
        if i + 1 < n && cs[i] != cs[i+1] && nxt[i] >= 0 {
            dp[i] += dp[nxt[i] as usize];
        }
        dp[i] %= MOD as i32;
    }
    // let res = recur(0, &mut memo, &nxt, &cs);
    println!("{}", dp[0]);
}

const MOD: i64 = 1_000_000_007;

fn recur(i: usize, memo: &mut Vec<i64>, nxt: &Vec<i32>, cs: &Vec<usize>) -> i64 {
    if i >= nxt.len() {
        return 1;
    }
    if memo[i] >= 0 {
        return memo[i];
    }
    let mut res = 0i64;
    res += recur(i + 1, memo, nxt, cs);
    if i + 1 < nxt.len() && cs[i] != cs[i + 1] && nxt[i] >= 0 {
        res += recur(nxt[i] as usize, memo, nxt, cs);
    }
    memo[i] = res % MOD;
    return memo[i];
}

/// Scanner
///
/// # Example
/// ```
/// use contest::io::scanner;
/// let mut sc = scanner::Scanner::new("1 2 \n\n \r\t \n 3.5".as_bytes());
/// assert_eq!("1".to_string(), sc.next::<String>().unwrap());
/// assert_eq!(2, sc.next());
/// assert_eq!(3.5, sc.next());
/// assert_eq!(None, sc.next::<i32>());
/// ```

pub struct Scanner<R: io::Read> {
    br: io::BufReader<R>,
    // Read tokens are stored in reversed order per line.
    buf: Vec<String>,
}

impl<R: io::Read> Scanner<R> {
    pub fn new(r: R) -> Scanner<R> {
        Scanner {
            br: io::BufReader::new(r),
            buf: vec![],
        }
    }
    #[inline]
    pub fn next<T>(&mut self) -> Option<T>
        where T: std::str::FromStr,
              T::Err: std::fmt::Debug
    {
        self.next_string().map(|s| s.parse::<T>().expect("Parse failed: "))
    }
    fn next_string(&mut self) -> Option<String> {
        self.buf.pop().or_else(|| match self.update() {
            true => self.next_string(),
            false => None,
        })
    }
    #[inline]
    fn update(&mut self) -> bool {
        let mut s = String::new();
        let res = self.br.read_line(&mut s);
        match res.expect("I/O error.") {
            0 => false,
            _ => {
                self.buf = s.split_whitespace().map(|x| x.to_string()).rev().collect();
                true
            }
        }
    }
}