#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long N = 5e5 + 10;
const long long IM = 1e15 + 37;
const long double PI = 3.1415926535897932384;
inline void PP(long long n) { cout << n << " "; }
void PV(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << "\n";
}
void PVV(vector<pair<long long, long long> > v) {
  for (long long i = 0; i < v.size(); i++)
    cout << v[i].first << " " << v[i].second << "\n";
}
void PA(long long v[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cout << v[i] << ' ';
  cout << "\n";
}
void IN(long long a[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cin >> a[i];
}
inline void op() {}
auto random_address = [] {
  char *p = new char;
  delete p;
  return uint64_t(p);
};
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() *
                      (random_address() | 1);
mt19937_64 rng(SEED);
long long p = 107;
long long power[N];
void pre(long long n) {
  for (long long i = 0; i <= n; i++) power[i] = rng();
}
long long hh[N];
deque<long long> q[N];
long long hash_prev[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  op();
  long long n;
  cin >> n;
  pre(n);
  long long a[n];
  IN(a, n);
  map<long long, long long> m;
  long long h = 0;
  m[0] = 1;
  long long ans = 0;
  bool f = 1;
  long long v = 0;
  for (long long i = 0; i < n; i++) {
    long long x = a[i];
    h = (h - hh[x] * power[x]);
    hh[x] = (hh[x] + 1) % 3;
    if (q[x].size() >= 3) {
      if (f) m[0]--;
      f = 0;
      long long u = q[x][0];
      q[x].pop_front();
      while (v < u) {
        m[hash_prev[v]]--;
        v++;
      }
    }
    q[x].push_back(i);
    h = (h + hh[x] * power[x]);
    ans += m[h];
    m[h]++;
    hash_prev[i] = h;
  }
  cout << (ans) << "\n";
  return 0;
}
