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;
mt19937_64 rang(
    chrono::high_resolution_clock::now().time_since_epoch().count());
long long rng(long long lim) {
  uniform_int_distribution<long long> uid(0, lim - 1);
  return uid(rang);
}
long long rand(long long a, long long b) { return a + rng(b - a + 1); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  vector<long long> hash(n + 1);
  for (long long &x : hash) x = rand(1, 1e18);
  vector<int> f(n + 1, 0);
  map<long long, int> mp;
  long long cur = 0;
  mp[cur] = 1;
  vector<queue<int>> idx(n + 1);
  vector<long long> hash_value(n + 1, 0);
  int l = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (idx[a[i]].size() > 2) {
      int tp = idx[a[i]].front();
      idx[a[i]].pop();
      while (l < tp) {
        mp[hash_value[l]]--;
        l++;
      }
    }
    cur -= f[a[i]] * hash[a[i]];
    f[a[i]] = (f[a[i]] + 1) % 3;
    cur += f[a[i]] * hash[a[i]];
    hash_value[i + 1] = cur;
    idx[a[i]].push(i + 1);
    ans += mp[cur];
    mp[cur]++;
  }
  cout << ans;
  return 0;
}
