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 mod = 1e9 + 7;
const long long INF = 3e18;
const int N = 5e5 + 5;
const int P0 = 437;
const int P1 = 343;
const int d = 400;
vector<int> v[N];
vector<pair<int, int> > tree;
vector<int> push;
void Push(int v) {
  tree[v * 2].first += push[v];
  tree[v * 2 + 1].first += push[v];
  push[v * 2] += push[v];
  push[v * 2 + 1] += push[v];
  push[v] = 0;
}
void Init(int v, int L, int R) {
  if (R - L == 1) {
    tree[v] = {0, R - L};
    return;
  }
  int c = (L + R) / 2;
  Init(v * 2, L, c);
  Init(v * 2 + 1, c, R);
  tree[v] = {0, tree[v * 2].second + tree[v * 2 + 1].second};
  return;
}
void Upd(int v, int L, int R, int l, int r, int del) {
  if (L == l && R == r) {
    tree[v].first += del;
    push[v] += del;
    return;
  }
  Push(v);
  int c = (L + R) / 2;
  if (l < c) Upd(v * 2, L, c, l, min(r, c), del);
  if (c < r) Upd(v * 2 + 1, c, R, max(l, c), r, del);
  if (tree[v * 2].first < tree[v * 2 + 1].first)
    tree[v] = tree[v * 2];
  else if (tree[v * 2].first > tree[v * 2 + 1].first)
    tree[v] = tree[v * 2 + 1];
  else {
    tree[v].first = tree[v * 2].first;
    tree[v].second = tree[v * 2].second + tree[v * 2 + 1].second;
  }
}
int Get(int v, int L, int R, int l, int r) {
  if (L == l && R == r) {
    if (tree[v].first) return 0;
    return tree[v].second;
  }
  Push(v);
  int c = (L + R) / 2, an = 0;
  if (l < c) an += Get(v * 2, L, c, l, min(r, c));
  if (c < r) an += Get(v * 2 + 1, c, R, max(l, c), r);
  return an;
}
void solve() {
  int n;
  cin >> n;
  tree.resize(n * 4);
  push.resize(n * 4);
  vector<int> a(n);
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    v[a[i]].emplace_back(i);
  }
  vector<int> pos(n);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (((int)(v[i]).size())); j++) {
      pos[v[i][j]] = j;
    }
  }
  long long ans = 0;
  Init(1, 0, n);
  for (int i = 0; i < (n); i++) {
    if (pos[i] == 0)
      Upd(1, 0, n, 0, i + 1, 1);
    else
      Upd(1, 0, n, v[a[i]][pos[i] - 1] + 1, i + 1, 1);
    if (pos[i] >= 2) {
      if (pos[i] == 2)
        Upd(1, 0, n, 0, v[a[i]][0] + 1, -1);
      else
        Upd(1, 0, n, v[a[i]][pos[i] - 3] + 1, v[a[i]][pos[i] - 2] + 1, -1);
      if (pos[i] == 3) Upd(1, 0, n, 0, v[a[i]][0] + 1, 1);
      if (pos[i] > 3)
        Upd(1, 0, n, v[a[i]][pos[i] - 4] + 1, v[a[i]][pos[i] - 3] + 1, 1);
    }
    ans += Get(1, 0, n, 0, i + 1);
  }
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int qq = 1;
  while (qq--) solve();
}
