#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 = 38;   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;   }