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;
struct Hash {
  int P = 1777771, MOD[2];
  long long h[2];
  vector<long long> vp[2];
  vector<int> x;
  Hash(vector<int>& s) {
    x = s;
    MOD[0] = 999727999;
    MOD[1] = 1070777777;
    for (int k = (0); k < (2); k++) vp[k].resize(s.size() + 1);
    for (int k = (0); k < (2); k++) {
      h[k] = 0;
      vp[k][0] = 1;
      long long p = 1;
      for (int i = (1); i < (s.size() + 1); i++) {
        h[k] = (h[k] + p * s[i - 1]) % MOD[k];
        vp[k][i] = p = (p * P) % MOD[k];
      }
    }
  }
  void change(int pos, int val) {
    for (int i = (0); i < (2); i++)
      h[i] = (h[i] + vp[i][pos] * (val - x[pos] + MOD[i])) % MOD[i];
    x[pos] = val;
  }
  long long getHashVal() { return (h[0] << 32) | h[1]; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n), cant(n, 0);
  for (int i = (0); i < (n); i++) {
    cin >> v[i];
    v[i]--;
  }
  vector<vector<int>> posOfVal(n);
  vector<long long> curpos(n, 0), hvalOfPos(n);
  for (int i = (0); i < (n); i++) posOfVal[v[i]].push_back(i);
  Hash h(cant);
  map<long long, int> m;
  assert(h.getHashVal() == 0);
  m[h.getHashVal()]++;
  long long ans = 0;
  int pos = 0;
  bool first = true;
  for (int i = (0); i < (n); i++) {
    if (curpos[v[i]] >= 3) {
      if (first) m[0]--;
      first = false;
      int posto = posOfVal[v[i]][curpos[v[i]] - 3];
      while (pos < posto) m[hvalOfPos[pos++]]--;
    }
    cant[v[i]]++;
    cant[v[i]] %= 3;
    h.change(v[i], cant[v[i]]);
    long long hval = h.getHashVal();
    ans += m[hval]++;
    hvalOfPos[i] = hval;
    curpos[v[i]]++;
  }
  cout << ans << '\n';
  return 0;
}
