#include <bits/stdc++.h>
using namespace std;
int a[500010];
vector<int> all[500010];
vector<pair<pair<int, int>, int> > op[500010];
pair<int, int> rng[500010];
void add_op(int l, int r, int L, int R) {
  if (l > r || L > R) return;
  op[l].push_back(make_pair(make_pair(L, R), 1));
  op[r + 1].push_back(make_pair(make_pair(L, R), -1));
}
pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
  return a.first == b.first  ? make_pair(a.first, a.second + b.second)
         : a.first < b.first ? b
                             : a;
}
struct Segment_tree {
  pair<int, int> T[2000010];
  int lazy[2000010];
  void pushup(int now) { T[now] = merge(T[now << 1], T[now << 1 | 1]); }
  void pushdown(int now) {
    if (!lazy[now]) return;
    lazy[now << 1] += lazy[now], lazy[now << 1 | 1] += lazy[now];
    T[now << 1].first += lazy[now], T[now << 1 | 1].first += lazy[now];
    lazy[now] = 0;
  }
  void Build(int now, int l, int r) {
    lazy[now] = 0;
    if (l == r) {
      T[now] = make_pair(0, 1);
      return;
    }
    int mid = l + r >> 1;
    Build(now << 1, l, mid), Build(now << 1 | 1, mid + 1, r);
    pushup(now);
  }
  void Update(int now, int l, int r, int L, int R, int delta) {
    if (l == L && r == R) {
      lazy[now] += delta;
      T[now].first += delta;
      return;
    }
    pushdown(now);
    int mid = l + r >> 1;
    if (R <= mid)
      Update(now << 1, l, mid, L, R, delta);
    else if (L > mid)
      Update(now << 1 | 1, mid + 1, r, L, R, delta);
    else
      Update(now << 1, l, mid, L, mid, delta),
          Update(now << 1 | 1, mid + 1, r, mid + 1, R, delta);
    pushup(now);
  }
  pair<int, int> Query(int now, int l, int r, int L, int R) {
    if (l == L && r == R) return T[now];
    pushdown(now);
    int mid = l + r >> 1;
    if (R <= mid) return Query(now << 1, l, mid, L, R);
    if (L > mid) return Query(now << 1 | 1, mid + 1, r, L, R);
    return merge(Query(now << 1, l, mid, L, mid),
                 Query(now << 1 | 1, mid + 1, r, mid + 1, R));
  }
} seg;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    all[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= all[i].size(); j++) {
      int pre = j == 0 ? 0 : all[i][j - 1],
          suc = j == all[i].size() ? n + 1 : all[i][j];
      rng[j] = make_pair(pre + 1, suc - 1);
    }
    for (int j = 0; j <= all[i].size(); j++) {
      add_op(rng[j].first, rng[j].second, rng[j].first, rng[j].second);
      if (j + 3 <= all[i].size()) {
        add_op(rng[j].first, rng[j].second + 1, rng[j + 3].first - 1,
               rng[j + 3].second);
      }
    }
  }
  seg.Build(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < op[i].size(); j++) {
      int l = op[i][j].first.first, r = op[i][j].first.second,
          delta = op[i][j].second;
      seg.Update(1, 1, n, l, r, delta);
    }
    pair<int, int> tmp = seg.Query(1, 1, n, i, n);
    if (tmp.first == n) ans += tmp.second;
  }
  printf("%lld\n", ans);
  return 0;
}
