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>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("-ffloat-store")
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403() {}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return !(a.first < b.first || (a.first == b.first && a.second <= b.second));
  }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt == m.end())
    m.insert({x, cnt});
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
const long long int L = 39;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
vector<long long int> a;
vector<pair<long long int, pair<long long int, long long int>>> hashes,
    prefHash;
long long int getHash() {
  long long int a = 0;
  for (long long int i = 0; i < L; ++i) a = a * 3 + rnd(0, 2);
  return a;
}
void getVec(vector<long long int> &a, long long int n) {
  for (long long int i = 0; i < L; ++i) {
    a.push_back(n % 3);
    n /= 3;
  }
}
long long int getXOR(const long long int &a1, const long long int &b1) {
  vector<long long int> a, b;
  getVec(a, a1);
  reverse(begin(a), end(a));
  getVec(b, b1);
  reverse(begin(b), end(b));
  long long int ans = 0;
  for (long long int i = 0; i < L; ++i) ans = ans * 3 + ((a[i] + b[i]) % 3);
  return ans;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    cin >> n;
    hashes.resize(n + 1);
    for (auto &v : hashes) {
      v.first = getHash();
      v.second.first = getHash();
      v.second.second = getHash();
    }
    a.clear();
    a.reserve(n + 1);
    prefHash.resize(n + 1);
    a.push_back(0);
    prefHash[0] = hashes[0];
    for (i = (1); i <= (n); ++i) {
      cin >> in;
      a.push_back(in);
      prefHash[i].first = getXOR(prefHash[i - 1].first, hashes[in].first);
      prefHash[i].second.first =
          getXOR(prefHash[i - 1].second.first, hashes[in].second.first);
      prefHash[i].second.second =
          getXOR(prefHash[i - 1].second.second, hashes[in].second.second);
    }
    vector<long long int> freq(n + 1, 0);
    map<pair<long long int, pair<long long int, long long int>>, long long int>
        hval;
    long long int l = 0;
    long long int ans = 0;
    hval[prefHash[0]]++;
    for (long long int i = 1; i <= n; ++i) {
      freq[a[i]]++;
      while (l < i && freq[a[i]] > 3) {
        hval[prefHash[l]]--;
        l++;
        freq[a[l]]--;
      }
      ans += hval[prefHash[i]];
      hval[prefHash[i]]++;
    }
    cout << ans << "\n";
  }
  aryanc403();
  return 0;
}
