#include<bits/stdc++.h>
#define FNAME "DAYSO"
#define xd '\n'
const int MAXN = (int)3e5 + 1;
using namespace std;

void HuuThien() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FNAME".inp", "r")) {
        freopen(FNAME".inp", "r", stdin);
        freopen(FNAME".out", "w", stdout);
    }
}

int n;
vector<int> a;
long long BIT[MAXN];

void PreBuild() {
    vector<int> compress(a.begin() + 1, a.end());
    sort(compress.begin(), compress.end());

    for(int i = 1; i <= n ; i++) {
        a[i] = lower_bound(compress.begin(), compress.end(), a[i]) - compress.begin() + 1;
    }
}

struct BuildBig{
    vector<long long> preLeft;
    vector<long long> preRight;

    void Update(int pos, int val) {
        while(pos <= n) {
            BIT[pos] += val;
            pos += (pos & -pos);
        }
    }

    long long getSum(int pos) {
        long long ans = 0;
            while(pos >= 1) {
                ans += BIT[pos];
                pos -= (pos & -pos);
            }

            return ans;
        }

    long long GetBig() {
        preLeft.assign(n + 1, 0);
        preRight.assign(n + 1, 0);

        for(int i = 1; i <= n ; i++) {
            preLeft[i] = getSum(a[i]);
            Update(a[i], 1);
        }

        memset(BIT, 0, sizeof(BIT));

        for(int i = n; i >= 1; i--) {
            preRight[i] = getSum(a[i]);
            Update(a[i], 1);
        }


        long long ans = 0;
        for(int i = 1; i <= n ; i++) {
            ans += 1ll * preLeft[i] * preRight[i];
        }

        return ans;
    }
};

struct BuildSmall{
    vector<long long> preLeft;
    vector<long long> preRight;

    void Update(int pos, int val) {
        while(pos <= n) {
            BIT[pos] += val;
            pos += (pos & -pos);
        }
    }

    long long getSum(int pos) {
        long long ans = 0;
        while(pos >= 1) {
            ans += BIT[pos];
            pos -= (pos & -pos);
        }

        return ans;
    }

    long long GetSmall() {
        preLeft.assign(n + 1, 0);
        preRight.assign(n + 1, 0);

        memset(BIT, 0, sizeof(BIT));
        for(int i = 1; i <= n ; i++) {
            preLeft[i] = getSum(n) - getSum(a[i] - 1);
            Update(a[i], 1);
        }

        memset(BIT, 0, sizeof(BIT));

        for(int i = n; i >= 1; i--) {
            preRight[i] = getSum(n) - getSum(a[i] - 1);
            Update(a[i], 1);
        }

        long long ans = 0;
        for(int i = 1; i <= n ; i++) {
            ans += 1ll * preLeft[i] * preRight[i];
        }

        return ans;
    }
};

void Init() {
    cin >> n;
    a.assign(n + 1, 0);

    for(int i = 1; i <= n ; i++) {
        cin >> a[i];
    }
}

void Solve() {
    PreBuild();
    long long res = 0;

    BuildBig B;
    BuildSmall S;

    res += B.GetBig();
    res += S.GetSmall();

    vector<int> cnt(n + 1, 0);

    for(int i = 1; i <= n ; i++) cnt[a[i]]++;
    for(int i = 1; i <= n ; i++) res -= 1ll * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6;
    cout << res;
}

signed main() {
    HuuThien();
    Init();
    Solve();
}
