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;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n + 1);
  vector<vector<int> > val(3, vector<int>(n + 1));
  vector<vector<int> > val2(3, vector<int>(n + 1));
  ll hashhh = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (i % 2 == 0) {
      hashhh = (hashhh ^ (v[i]));
    } else {
      hashhh = (hashhh + v[i]);
    }
  }
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() ^ hashhh);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 2; j++) {
      val[j][v[i]] = rng();
      val2[j][v[i]] = rng();
    }
    val[2][v[i]] = val[0][v[i]] ^ val[1][v[i]];
    val2[2][v[i]] = val2[0][v[i]] ^ val2[1][v[i]];
  }
  map<pii, int> mp[3];
  mp[0][make_pair(0, 0)] = 1;
  vector<int> occ(n + 1), occ_all(n + 1), value(n + 1), pref(n + 1),
      value2(n + 1), pref2(n + 1);
  ll answer = 0;
  int it = 1;
  for (int i = 1; i <= n; i++) {
    occ[v[i]]++;
    occ_all[v[i]]++;
    while (occ[v[i]] > 3) {
      occ[v[it]]--;
      mp[(it - 1) % 3][make_pair(pref[it - 1], pref2[it - 1])]--;
      it++;
    }
    int first = occ_all[v[i]] % 3;
    value[i] = val[first][v[i]];
    value2[i] = val2[first][v[i]];
    pref[i] = pref[i - 1] ^ value[i];
    pref2[i] = pref2[i - 1] ^ value2[i];
    answer += mp[i % 3][make_pair(pref[i], pref2[i])];
    mp[i % 3][make_pair(pref[i], pref2[i])]++;
  }
  cout << answer << "\n";
  return 0;
}
