#include using namespace std; const int kN = 100'000 + 5; const int kM = kN + kN; long long st[kM * 4], cf[kM * 4], ct[kM * 4]; int pref[kN + kN], a[kN], l[kN], r[kN]; vector pos[kN]; vector> hist; void RollBack() { while (!hist.empty()) { *hist.back().first = hist.back().second; hist.pop_back(); } hist.clear(); } void Assign(long long *p, long long v) { hist.emplace_back(p, *p); *p = v; } void Apply(int o, long long f, long long t, int l, int r) { Assign(&cf[o], cf[o] + f); Assign(&ct[o], ct[o] + t); Assign(&st[o], st[o] + f * (l + r - 1) * (r - l) / 2); Assign(&st[o], st[o] + t * (r - l)); } void Push(int o, int l, int r) { int m = (l + r) >> 1; Apply(o * 2 + 1, cf[o], ct[o], l, m); Apply(o * 2 + 2, cf[o], ct[o], m, r); Assign(&cf[o], 0); Assign(&ct[o], 0); } long long Query(int l, int r, int ql, int qr, int o = 0) { if (l >= qr || ql >= r) return 0; if (l >= ql && r <= qr) return st[o]; Push(o, l, r); int m = (l + r) >> 1; return Query(l, m, ql, qr, o * 2 + 1) + Query(m, r, ql, qr, o * 2 + 2); } void Modify(int l, int r, int ql, int qr, long long f, long long t, int o = 0) { if (l >= qr || ql >= r) return; if (l >= ql && r <= qr) return Apply(o, f, t, l, r); int m = (l + r) >> 1; Modify(l, m, ql, qr, f, t, o * 2 + 1); Modify(m, r, ql, qr, f, t, o * 2 + 2); st[o] = st[o * 2 + 1] + st[o * 2 + 2]; } long long Solve(int v, int n) { int m = pos[v].size(); for (int i = 0; i < m; ++i) { r[i] = pos[v][i] - 1; l[i + 1] = pos[v][i]; } r[m] = n; long long res = 0; for (int i = 0; i <= m; ++i) { int ql = 2 * i - r[i] + kN, qr = 2 * i - l[i] + kN; res += Query(0, kM, ql - 1, qr); Modify(0, kM, qr + 1, kM, 0, qr - ql + 1); Modify(0, kM, ql, qr + 1, 1, 1 - ql); } RollBack(); return res; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); pos[a[i]].push_back(i); } long long ans = 0; for (int i = 0; i < kN; ++i) { if (!pos[i].empty()) ans += Solve(i, n); } printf("%lld\n", ans); return 0; }