You are given an array a consisting of n integers. We denote the subarray a[l..r] as the array [a_l, a_{l + 1}, ..., a_r] (1 ≤ l ≤ r ≤ n).

A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array [1, 2, 2, 2, 1, 1, 2, 2, 2] has three good subarrays:

  * a[1..6] = [1, 2, 2, 2, 1, 1]; 
  * a[2..4] = [2, 2, 2]; 
  * a[7..9] = [2, 2, 2]. 



Calculate the number of good subarrays of the given array a.

Input

The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).

Output

Print one integer — the number of good subarrays of the array a.

Examples

Input


9
1 2 2 2 1 1 2 2 2


Output


3


Input


10
1 2 3 4 1 2 3 1 2 3


Output


0


Input


12
1 2 3 4 3 4 2 1 3 4 2 1


Output


1
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
const int base = 131;
const int base2 = 37;
map<long long, int> mp;
map<unsigned long long, int> mp2;
unsigned long long hs2, pw2[N], pre2[N];
long long hs, ans, pre[N], pw[N];
int n, a[N], l, num[N], buc[N];
void add(int v) {
  if (num[v] < 2)
    hs = (hs + pw[v]) % mod, hs2 += pw2[v];
  else
    hs = (hs - 2 * pw[v] % mod + mod) % mod, hs2 -= 2 * pw2[v];
  num[v] = (num[v] + 1) % 3, buc[v]++;
  while (buc[v] > 3) mp[pre[l]]--, mp2[pre2[l]]--, buc[a[++l]]--;
}
int main() {
  cin >> n, mp[0] = mp2[0] = 1, pw[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * base % mod, pw2[i] = pw2[i - 1] * base2;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    add(a[i]), ans += min(mp2[hs2], mp[hs]), mp[pre[i] = hs]++,
        mp2[pre2[i] = hs2]++;
  }
  cout << ans << endl;
  return 0;
}
