#include <bits/stdc++.h>
using namespace std;
long long hs[505050], cnt[505050], a[505050], pr[505050], ans;
vector<long long> v[505050];
map<long long, long long> mp;
int n;
int main() {
  srand(9904);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    hs[i] = ((long long)rand() << 30) + (rand() << 16) + rand();
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  mp[0] = 1;
  int l = 0;
  for (int i = 1; i <= n; i++) {
    v[a[i]].push_back(i);
    if (v[a[i]].size() > 3) {
      while (l < v[a[i]][v[a[i]].size() - 4]) {
        mp[pr[l]]--;
        l++;
      }
    }
    pr[i] = pr[i - 1] - cnt[a[i]] * hs[a[i]];
    cnt[a[i]]++;
    cnt[a[i]] %= 3;
    pr[i] += cnt[a[i]] * hs[a[i]];
    ans += mp[pr[i]];
    mp[pr[i]]++;
  }
  cout << ans << endl;
  return 0;
}
