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 INF = 1e9 + 7, N = 5e5 + 7;
long long a[N], p[N], mp[N], cnt[N], mn[N];
map<long long, long long> cnth;
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  mt19937 rng(time(0));
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  set<long long> s;
  while (s.size() < n) {
    long long x = rng();
    if (s.count(x) == 0) {
      s.insert(x);
      mp[s.size()] = x;
    }
  }
  memset(mn, 0x3f, sizeof mn);
  for (long long _ = 0; _ < 5; _++) {
    shuffle(mp + 1, mp + n + 1, rng);
    for (long long i = 1; i <= n; i++) cnt[i] = 0;
    cnth.clear();
    for (long long i = 1; i <= n; i++) {
      cnt[a[i]]++;
      if (cnt[a[i]] % 3 == 0)
        p[i] = -2ll * mp[a[i]];
      else
        p[i] = mp[a[i]];
      p[i] += p[i - 1];
    }
    for (long long i = 1; i <= n; i++) cnt[i] = 0;
    long long l = 0;
    cnth[p[0]]++;
    for (long long i = 1; i <= n; i++) {
      cnt[a[i]]++;
      while (cnt[a[i]] > 3) cnt[a[l + 1]]--, cnth[p[l]]--, l++;
      mn[i] = min(mn[i], cnth[p[i]]);
      cnth[p[i]]++;
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) ans += mn[i];
  cout << ans << endl;
}
