#include <bits/stdc++.h>
using Int = long long;  // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Tue Aug  4 23:38:25 JST 2020
 **/

int popcount(std::uint32_t x) { return __builtin_popcount(x); }
int popcount(std::uint64_t x) { return __builtin_popcountll(x); }

template <bool select_by_memo = false, class block_type = std::uint32_t> struct BitVector {
    static constexpr int b = sizeof(block_type) * CHAR_BIT;  // blockのサイズ
    int n;
    std::vector<block_type> bit;
    std::vector<int> acc;
    std::vector<int> orig;
    BitVector() {}
    BitVector(int n_) : n(n_), bit(n / b + 1), acc(n / b + 1), orig(n_) {}
    void set(int i, bool x) {
        orig[i] = x;
        if (x)
            bit[i / b] |= (block_type)1 << (i % b);
        else
            bit[i / b] &= ~((block_type)1 << (i % b));
    }
    void build() {
        for (int i = 0; i < (int)acc.size() - 1; i++) {
            acc[i + 1] = acc[i] + popcount(bit[i]);
        }
    }
    // [0, i)内の1の個数
    int rank(int i) {
        // dump(acc.size(), bit.size(), i / b);
        return acc[i / b] + (i % b ? popcount(bit[i / b] << (b - i % b)) : 0);
    }
    int rank(int i, bool x) { return x ? rank(i) : i - rank(i); }
    // int rank(int i, bool x) { return std::count(orig.begin(), orig.begin() + i, x); }
    // j番目のxのindex
    int select(int j, bool x) {
        int ok = n, ng = -1;
        while (std::abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            (rank(mid + 1, x) > j ? ok : ng) = mid;
        }
        return ok;
    }
};

template <class T, int maxlog = 32, bool select_by_memo = false, class block_type = std::uint32_t>
struct WaveletMatrix {
    using bv_type = BitVector<select_by_memo, block_type>;
    std::array<bv_type, maxlog> bvs;      // [maxlog, n]の01行列
    std::array<int, maxlog> offset = {};  // 各列でbitが0になっている要素の数

    WaveletMatrix(const std::vector<T>& a) {
        std::vector<T> cur_data = a;
        for (int k = maxlog - 1; k >= 0; k--) {  // 上位bitから見る
            std::vector<T> zero, one;
            bvs[k] = bv_type(a.size());
            for (int i = 0; i < a.size(); i++) {
                bool bit = cur_data[i] >> k & 1;
                if (bit)
                    one.push_back(cur_data[i]), bvs[k].set(i, 1);
                else
                    zero.push_back(cur_data[i]);
            }
            offset[k] = zero.size();
            cur_data = std::move(zero);
            cur_data.insert(cur_data.end(), one.begin(), one.end());
            bvs[k].build();
        }
    }
    // [l, r)の{x未満の個数, xの個数, xより大の個数}
    std::array<int, 3> rank_3way(int x, int l, int r) {
        dump(x, l, r);
        int lt = 0, eq = r - l, gt = 0;
        for (int k = maxlog - 1; k >= 0; k--) {
            assert(l <= r);
            int prv_num = r - l;
            if (x >> k & 1) {
                l = offset[k] + bvs[k].rank(l, 1);
                r = offset[k] + bvs[k].rank(r, 1);
                lt += prv_num - (r - l);
            } else {
                l = bvs[k].rank(l, 0);
                r = bvs[k].rank(r, 0);
                gt += prv_num - (r - l);
            }
            eq -= prv_num - (r - l);
        }
        return {lt, eq, gt};
    }
};

signed main() {
    Int n;
    std::cin >> n;
    // Int n = 100000;
    std::vector<int> a(n);
    REP(i, n) std::cin >> a[i];
    // REP(i, n) a[i] = i;
    WaveletMatrix<int, 32> wm(a);

    Int ans1 = 0;
    REP(i, n) { ans1 += wm.rank_3way(a[i], 0, i)[2]; }
    Int ans2 = 0;
    REP(i, n) { ans2 += wm.rank_3way(a[i], i + 1, n)[0]; }
    assert(ans1 == ans2);
    std::cout << ans1 << std::endl;
}



Measured memory_bytes: 32556