#include <bits/stdc++.h>
using namespace std;
const int iinf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5;
int n, a[N], d[N];
long long ans;
vector<int> id[N];
struct line {
  int l, r, t;
  line() {}
  line(int l, int r, int t) : l(l), r(r), t(t) {}
};
vector<line> p[N + 1];
void addmat(int xl, int xr, int yl, int yr) {
  p[xl].push_back(line(yl, yr, 1)), p[xr].push_back(line(yl, yr, -1));
}
const int tN = N << 2;
int mn[tN], mc[tN], mk[tN];
void pushup(int k) {
  if (mn[k * 2 + 1] < mn[k * 2 + 2])
    mn[k] = mn[k * 2 + 1], mc[k] = mc[k * 2 + 1];
  else if (mn[k * 2 + 1] > mn[k * 2 + 2])
    mn[k] = mn[k * 2 + 2], mc[k] = mc[k * 2 + 2];
  else
    mn[k] = mn[k * 2 + 1], mc[k] = mc[k * 2 + 1] + mc[k * 2 + 2];
}
void pushadd(int k, int v) { mn[k] += v, mk[k] += v; }
void pushdown(int k) {
  pushadd(k * 2 + 1, mk[k]), pushadd(k * 2 + 2, mk[k]), mk[k] = 0;
}
void build(int k = 0, int l = 0, int r = n) {
  if (r - l == 1) return mn[k] = 0, mc[k] = 1, void();
  build(k * 2 + 1, l, ((l + r) >> 1)), build(k * 2 + 2, ((l + r) >> 1), r),
      pushup(k);
}
void add(int first, int second, int v, int k = 0, int l = 0, int r = n) {
  if (r <= first || second <= l) return;
  if (first <= l && r <= second) return pushadd(k, v);
  pushdown(k), add(first, second, v, k * 2 + 1, l, ((l + r) >> 1)),
      add(first, second, v, k * 2 + 2, ((l + r) >> 1), r), pushup(k);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = (0), I = (n); i < I; i++)
    id[i].push_back(-1), id[i].push_back(-1), id[i].push_back(-1);
  for (int i = (0), I = (n); i < I; i++)
    cin >> a[i], --a[i], d[i] = int((id[a[i]]).size()), id[a[i]].push_back(i);
  for (int i = (0), I = (n); i < I; i++)
    id[i].push_back(n), id[i].push_back(n), id[i].push_back(n);
  for (int i = (0), I = (n); i < I; i++) {
    addmat(i, i + 1, i + 1, n);
    addmat(i, n, 0, id[a[i]][d[i] - 3] + 1);
    addmat(i, id[a[i]][d[i] + 2], id[a[i]][d[i] - 1] + 1, i + 1);
  }
  build();
  for (int i = (0), I = (n); i < I; i++) {
    for (line u : p[i])
      if (u.r > u.l) add(u.l, u.r, u.t);
    ans += mc[0] * (mn[0] == 0);
  }
  cout << ans << '\n';
  return 0;
}
