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 long long mod1 = 999999937;
const long long mod2 = 1e9 + 9;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
  long long n;
  cin >> n;
  long long id1[n + 1], id2[n + 1];
  for (long long i = 0; i <= n; i++) {
    id1[i] = abs((long long)rng());
    id2[i] = abs((long long)rng());
  }
  long long sig1[n + 1], sig2[n + 1], a[n + 1], freq[n + 1], cur[n + 1];
  for (long long i = 0; i <= n; i++)
    cur[i] = freq[i] = a[i] = sig1[i] = sig2[i] = 0;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  long long hsh1 = 0, hsh2 = 0;
  for (long long i = 1; i <= n; i++) {
    hsh1 = (hsh1 - ((freq[a[i]] * id1[a[i]]) % mod1)) % mod1;
    while (hsh1 < 0) hsh1 += mod1;
    hsh2 = (hsh2 - ((freq[a[i]] * id2[a[i]]) % mod2)) % mod2;
    while (hsh2 < 0) hsh2 += mod2;
    freq[a[i]] = (freq[a[i]] + 1) % 3;
    hsh1 = (hsh1 + ((freq[a[i]] * id1[a[i]]) % mod1)) % mod1;
    sig1[i] = hsh1;
    hsh2 = (hsh2 + ((freq[a[i]] * id2[a[i]]) % mod2)) % mod2;
    sig2[i] = hsh2;
  }
  map<pair<long long, long long>, long long> cnt;
  cnt[{0, 0}]++;
  long long l = 1;
  long long res = 0;
  for (long long r = 1; r <= n; r++) {
    cur[a[r]]++;
    while (cur[a[r]] > 3) {
      cur[a[l]]--;
      cnt[{sig1[l - 1], sig2[l - 1]}]--;
      l++;
    }
    res += cnt[{sig1[r], sig2[r]}];
    cnt[{sig1[r], sig2[r]}]++;
  }
  cout << res << '\n';
}
