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 Kotlin (1.3.71) (judge verdict: Time Limit Exceeded)

import java.io.*
import java.util.*
import java.lang.*
import java.lang.Math.*

fun bsCntLte(A: LongArray, X: Long, includeEqual: Boolean = false): Int {
    return bsIdxLte(A,X, includeEqual) + 1
}
fun bsIdxLte(A: LongArray, X: Long, includeEqual:Boolean = false): Int {
    val N = A.size
    if (N == 0) return -1
    val xLess: (Long)->Boolean = if (includeEqual) {
        { Y:Long -> X < Y }
    } else {
        { Y:Long -> X <= Y }
    }
    if (xLess(A[0])) return -1
    var l = 0; var r = N
    while (l < r - 1) {
        val m = (l + r) / 2
        if (xLess(A[m])) {
            r = m
        } else {
            l = m
        }
    }
    return l
}

fun solve() {
    val N = rd.readInt()
    val C = IntArray(N)
    for (i in 0 until N) {
        C[i] = rd.readInt()
    }

    val CMAX = 200_000
    val pos = Array(CMAX + 1){ArrayList<Int>()}
    pos[C[0]].add(0)
    val sgs = ArrayList<IntPair>()
    val rights = ArrayList<Long>()
    for (i in 1 until N) {
        if (C[i] == C[i-1]) continue
        else {
            pos[C[i]].add(i)
            val sz = pos[C[i]].size
            if (sz>1) {
                val l = pos[C[i]][sz-2]; val r = pos[C[i]][sz-1]
                sgs.add(IntPair(l, r))
                rights.add(r.toLong())
            }
        }
    }

    sgs.sortBy { it.second }
    rights.sort()

    var ans = 1L
    for (sg in sgs) {
        ans += 1L + bsCntLte(rights.toLongArray(), sg.first.toLong(), true)
        ans %= MOD
    }

    println(ans)
}


//val rd = debug.Reader("agc", "031", "b", "sample-2")
val rd = Reader()


fun main(args: Array<String>) {
    try {
        var exception: Throwable? = null
        val sth = Thread(null, SolveThread(), "solveThread", 128 * 1024 * 1024)
        sth.setUncaughtExceptionHandler { _, e -> exception = e }
        sth.start(); sth.join()
        if (exception != null) throw exception!!
    } catch (e: Exception) { throw e }
}
const val MOD = 1_000_000_007L; const val INF = Int.MAX_VALUE/2; const val LINF = Long.MAX_VALUE/2
data class IntPair(val first: Int, val second: Int); data class LongPair(val first: Long, val second: Long)
val errPrintln: (Any) -> Unit = { msg -> System.err.println(msg) }
fun rAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("EAssert Error: $msg"); throw Error(msg) }
}
fun tAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("TAssert Error: $msg"); Thread.sleep(3000) }
}
class SolveThread : Runnable {
    override fun run() { solve() }
}
class Reader {
    private val br = BufferedReader(InputStreamReader(System.`in`))
    val readString: () -> String = { br.readLine() }
    val readInt: () -> Int = { br.readLine().toInt() }
    val readLong: () -> Long = { br.readLine().toLong() }
    val readIntArray: () -> IntArray = { br.readLine().split(' ').map { it.toInt() }.toIntArray() }
    val readLongArray: () -> LongArray = { br.readLine().split(' ').map { it.toLong() }.toLongArray() }
    val readListInt: () -> List<Int> = { br.readLine().split(' ').map { it.toInt() } }
    val readListLong: () -> List<Long> = { br.readLine().split(' ').map { it.toLong() } }
}