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 = (long long)1e18;
const int mod = (int)1e9 + 7;
const int MAXN = (int)5e5 + 5;
long long n;
int a[MAXN];
long long kac[MAXN];
long long pre[MAXN][3];
long long last[MAXN];
unsigned long long rnd[MAXN];
unsigned long long hsh[MAXN];
map<unsigned long long, int> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  mt19937 ar(time(NULL));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    rnd[i] = ((1LL * ar()) << 32) + ar();
  }
  rnd[n] = ((1LL * ar()) << 32) + ar();
  for (int i = 0; i < MAXN; i++) last[i] = -2;
  int start = 0;
  for (int i = 0; i < n; i++) {
    pre[i][0] = last[a[i]];
    last[a[i]] = i;
    if (pre[i][0] == -2) {
      pre[i][1] = -2;
      pre[i][2] = -2;
    } else {
      pre[i][1] = pre[pre[i][0]][0];
      pre[i][2] = pre[pre[i][0]][1];
    }
  }
  long long ans = 0;
  mp[0]++;
  start = 0;
  for (int i = 0; i < n; i++) {
    while (start <= pre[i][2]) {
      mp[hsh[start]]--;
      start++;
    }
    int bef = kac[a[i]];
    kac[a[i]]++;
    kac[a[i]] %= 3;
    hsh[i + 1] = hsh[i] + 1LL * rnd[a[i]] * (kac[a[i]] - bef);
    ans += mp[hsh[i + 1]];
    mp[hsh[i + 1]]++;
  }
  cout << ans << '\n';
}
