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 INF = 1e9 + 7, N = 5e5 + 7;
int a[N], lst_occ[N], lst_ptr[N], n;
array<int, 4> b[N];
int lz[N * 4];
pair<int, int> t[N * 4];
pair<int, int> add(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) return max(a, b);
  a.second += b.second;
  return a;
}
void apply(int v, int val) {
  t[v].first += val;
  lz[v] += val;
}
void push(int v) {
  apply(v * 2, lz[v]);
  apply(v * 2 + 1, lz[v]);
  lz[v] = 0;
}
void build(int v = 1, int tl = 0, int tr = n + 1) {
  if (tr - tl == 1) {
    t[v] = {0, 1};
    return;
  }
  int tm = (tl + tr) / 2;
  build(v * 2, tl, tm);
  build(v * 2 + 1, tm, tr);
  t[v] = add(t[v * 2], t[v * 2 + 1]);
}
void upd(int l, int r, int val, int v = 1, int tl = 0, int tr = n + 1) {
  if (l >= r) return;
  if (l == tl && r == tr) {
    apply(v, val);
    return;
  }
  push(v);
  int tm = (tl + tr) / 2;
  upd(l, min(tm, r), val, v * 2, tl, tm);
  upd(max(tm, l), r, val, v * 2 + 1, tm, tr);
  t[v] = add(t[v * 2], t[v * 2 + 1]);
}
pair<int, int> qry(int l, int r, int v = 1, int tl = 0, int tr = n + 1) {
  if (l >= r) return {-INF, -INF};
  if (l == tl && r == tr) return t[v];
  int tm = (tl + tr) / 2;
  push(v);
  return add(qry(l, min(r, tm), v * 2, tl, tm),
             qry(max(l, tm), r, v * 2 + 1, tm, tr));
}
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  cin >> n;
  build();
  for (int i = 1; i <= n; i++) cin >> a[i];
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    upd(1, n + 1, 1);
    b[i] = {0, 0, 1, n + 1};
  }
  for (int i = 1; i <= n; i++) {
    lst_ptr[i] = lst_occ[a[i]];
    upd(b[a[i]][0], b[a[i]][1], -1);
    upd(b[a[i]][2], b[a[i]][3], -1);
    int j = i;
    array<int, 4> v;
    for (int k = 0; k < 4; k++) v[3 - k] = j, j = lst_ptr[j];
    int l1 = v[0] + 1, r1 = v[1] + 1;
    int l2 = v[3] + 1, r2 = n + 1;
    b[a[i]] = {l1, r1, l2, r2};
    upd(l1, r1, 1);
    upd(l2, r2, 1);
    pair<int, int> p = qry(1, i + 1);
    if (p.first == n) ans += p.second;
    lst_occ[a[i]] = i;
  }
  cout << ans << endl;
}
