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;
void cmax(int& x, const int& y) { x = x > y ? x : y; }
void cmin(int& x, const int& y) { x = x < y ? x : y; }
template <class T>
istream& operator>>(istream& in, vector<T>& V) {
  for (auto& x : V) in >> x;
  return in;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& V) {
  for (auto x : V) out << x << ' ';
  return out;
}
template <class T>
void sort(vector<T>& V) {
  sort(V.begin(), V.end());
}
template <class T>
void reverse(vector<T>& V) {
  reverse(V.begin(), V.end());
}
template <class T>
int SZ(const vector<T>& V) {
  return (int)V.size();
}
void debug() { cerr << "whxorz" << '\n'; }
int n;
const int N = 5e5 + 1;
int a[N];
int nxt[N];
int pre[N];
int mn[N << 2];
int cnt[N << 2];
void up(int p) {
  cmin(mn[p] = mn[p << 1], mn[p << 1 | 1]);
  cnt[p] = 0;
  if (mn[p] == mn[p << 1]) cnt[p] += cnt[p << 1];
  if (mn[p] == mn[p << 1 | 1]) cnt[p] += cnt[p << 1 | 1];
}
void build(int l, int r, int p) {
  if (l == r) {
    mn[p] = N;
    cnt[p] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, p << 1);
  build(mid + 1, r, p << 1 | 1);
  up(p);
}
int t[N << 2];
void upd(int p, int v) {
  t[p] += v;
  mn[p] += v;
}
void down(int p) {
  if (!t[p]) return;
  upd(p << 1, t[p]);
  upd(p << 1 | 1, t[p]);
  t[p] = 0;
}
void mdf(int ql, int qr, int l, int r, int p, int v) {
  if (ql <= l && r <= qr) {
    upd(p, v);
    return;
  }
  down(p);
  int mid = l + r >> 1;
  if (ql <= mid) mdf(ql, qr, l, mid, p << 1, v);
  if (qr > mid) mdf(ql, qr, mid + 1, r, p << 1 | 1, v);
  up(p);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1, I = n + 1; i < I; ++i) cin >> a[i];
  for (int i = 1, I = n + 1; i < I; ++i) {
    pre[i] = nxt[a[i]];
    nxt[a[i]] = i;
  }
  build(1, n, 1);
  ll ans = 0;
  for (int i = 1, I = n + 1; i < I; ++i) {
    mdf(i, i, 1, n, 1, -N);
    int k1 = pre[i], k2 = pre[k1], k3 = pre[k2], k4 = pre[k3];
    if (k1) {
      mdf(1, k1, 1, n, 1, -1);
    }
    if (k2) {
      mdf(k3 + 1, k2, 1, n, 1, -1);
    }
    if (k3) {
      mdf(k4 + 1, k3, 1, n, 1, 1);
    }
    mdf(1, i, 1, n, 1, 1);
    if (mn[1] == 0) {
      ans += cnt[1];
    }
  }
  cout << ans << '\n';
  return 0;
}
