int N; int A[Maxn]; int vis[Maxn]; int L[Maxn], R[Maxn];
inline int fix (int x) { return ((x - 1) % N + N) % N + 1; }
namespace BIT { struct bit { LL sum[Maxn]; inline void add (int x, int val) { for (; x <= N; x += x & (-x)) sum[x] += val; } inline LL query (int x) { LL ans = 0; for (; x; x -= x & (-x)) ans += sum[x]; return ans; } } A, B;
inline void update (int x, int y, int val) { if (x > y) return ; A.add (x, val * x), A.add (y + 1, -val * (y + 1)); B.add (x, val), B.add (y + 1, -val); }
inline void Init () { int r = 0; for (int i = 1; i <= N; ++i) { while (r < N && !vis[A[r + 1]]) ++r, ++vis[A[r]]; R[i] = min (r, i + (N - 1) / 2 - 1); if (vis[A[i]]) --vis[A[i]]; }
memset (vis, 0, sizeof vis); int l = 1; while (!vis[A[fix (l - 1)]]) l = fix (l - 1), ++vis[A[l]]; L[1] = fix (max (l, N - (N - 1) / 2 + 1));
for (int i = 2; i <= (N - 1) / 2; ++i) { while (vis[A[i - 1]]) --vis[A[l]], l = fix (l + 1); if (l < i) break; L[i] = max (l, fix ((i - 1 + N) - (N - 1) / 2 + 1)); ++vis[A[i - 1]]; } }
inline void Solve () { Init ();
LL ans = 0; int p = 1; for (int i = 1; i <= N; ++i) { if (L[i] < i) break; while (p < N && p + 1 <= R[i] + 1) { ++p; BIT :: update (p + 1, R[p] + 1, 1); } ans += BIT :: query (L[i], N); BIT :: update ((i + 1) + 1, R[i + 1] + 1, -1); }
cout << ans << endl; }
inline void Input () { N = read<int>(); for (int i = 1; i <= N; ++i) A[i] = read<int>(); }