#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; #define fst first #define snd second /* clang-format off */ template struct _vec { using type = vector::type>; }; template struct _vec { using type = T; }; template using vec = typename _vec::type; template vector make_v(size_t size, const T& init) { return vector(size, init); } template auto make_v(size_t size, Ts... rest) { return vector(size, make_v(rest...)); } template inline void chmin(T &a, const T& b) { if (b < a) a = b; } template inline void chmax(T &a, const T& b) { if (b > a) a = b; } /* clang-format on */ template struct fenwick_tree { vector x; fenwick_tree(int n) : x(n + 1) { } void add(int k, T a) { for (++k; k < x.size(); k += k & -k) x[k] += a; } T sum(int k) { T s = 0; for (; k > 0; k &= k - 1) s += x[k]; return s; } }; ll solve(const vector& A) { int N = A.size(); set st(A.begin(), A.end()); ll res = 0; for (int x : st) { vector S(N + 1, 0); for (int i = 0; i < N; i++) S[i + 1] = S[i] + (A[i] == x); for (int i = 0; i <= N; i++) S[i] = 2 * S[i] - i + N; // S[i] は -N ~ +N まで動く // 0 ~ 2N fenwick_tree bit(2 * N + 1); for (int x : S) { res += bit.sum(x); bit.add(x, 1); } } return res; } int main() { #ifdef DEBUG ifstream ifs("in.txt"); cin.rdbuf(ifs.rdbuf()); #endif int N; while (cin >> N) { vector A(N); for (int& x : A) cin >> x; cout << solve(A) << endl; } return 0; }