#000651

Bombone - Državno

Little Djole saw n sweets in a candy shop's window. The sweets are lined up and each one of them is represented with a single natural number - different numbers indicate different types of sweets, and same numbers indicate the same kind of sweets. He plans to grab some of them, eventually pay for them and later sweeten himself up.


In order to do it faster, he wants to grab only a sequence of successive sweets, i.e. he wants to select two integers i and j (1 ≤ ijn) and grab all the sweets on the positions i, i + 1, ..., j - 1, j. Also, since he doesn't like diversity, there may not be more than three different kinds of sweets in that subsequence. For example, the subsequence 12434 is not a good one because it contains 4 different kinds of candy.


Determine the amount of ways Djole can grab the sweets.


InputFrom the first line of the standard input read one natural number n, which represents the amount of sweets in the window (1 ≤ n ≤ 10^5). From the next line input n natural numbers (the numbers will not exceed 10^9) which represent the different kinds of candy in the window.

OutputIn the first and only line of the standard output write the number of successive subsequences where at most three different kinds of sweets appear.

Input:
5
1 2 4 3 4

Output:
13
Explanation: We have 13 possible subsequences that satisfy the given constraints: (12434), (12434), (12434), (12434), (12434), (12434), (12434), (12434), (12434), (12434), (12434), (12434) and (12434).

Input:
6
10 20 10 30 20 20

Output:
21
Explanation: Since we have only three different kinds of sweets overall (10, 20 and 30), every subsequence of successive elements (and there are 21 of them) satisfies the constraints.

Submit solution

Coming later

The grading service will be connected in a later migration step. You can inspect the task and your previous results now.