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 int MX = 500005;
int n, a[MX], ptr[MX];
int cn[4 * MX], st[4 * MX];
long long res;
vector<int> pos[MX];
stack<pair<int, int> > ant[MX];
void update(int i, int j, int pos, int a, int b, int k) {
  if (j < a || b < i) return;
  if (a <= i && j <= b) {
    cn[pos] += k;
    if (cn[pos]) {
      st[pos] = (j - i + 1);
    } else if (i < j) {
      st[pos] = st[pos * 2] + st[pos * 2 + 1];
    } else {
      st[pos] = 0;
    }
    return;
  }
  int m = (i + j) / 2;
  update(i, m, pos * 2, a, b, k);
  update(m + 1, j, pos * 2 + 1, a, b, k);
  if (!cn[pos]) st[pos] = st[pos * 2] + st[pos * 2 + 1];
}
void insert(int a, int b, int k) {
  ant[k].emplace(a, b);
  update(1, n, 1, a, b, 1);
}
void insert(int i, int k) {
  if (i == pos[k].size()) return;
  if (i + 2 < pos[k].size())
    insert(pos[k][i], pos[k][i + 2] - 1, k);
  else
    insert(pos[k][i], n, k);
  if (i + 3 < pos[k].size()) insert(pos[k][i + 3], n, k);
}
void clear(int k) {
  while (ant[k].size()) {
    int a = ant[k].top().first;
    int b = ant[k].top().second;
    ant[k].pop();
    update(1, n, 1, a, b, -1);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) insert(0, i);
  for (int i = 1; i <= n; i++) {
    res += n - st[1];
    clear(a[i]);
    update(1, n, 1, i, i, MX);
    ptr[a[i]]++;
    insert(ptr[a[i]], a[i]);
  }
  cout << res << '\n';
  return 0;
}
