#include<iostream>
#include<vector>
#include<algorithm>
#define SZ(X) ((int)(X).size())
using namespace std;
long long dc(vector<int> d) {
int mi = *min_element(d.begin(), d.end());
int ma = *max_element(d.begin(), d.end());
if(mi == ma) return 0;
int mid = mi + (ma - mi) / 2;
vector<int> a, b;
int now = 0;
long long ret = 0;
for(int i = 0; i < SZ(d); i++) {
if(d[i] <= mid) {
a.push_back(d[i]);
ret += SZ(b);
} else b.push_back(d[i]);
}
cout << ret << endl;
return ret + dc(a) + dc(b);
}
int main() {
int n;
cin >> n;
vector<int> d(n);
for(int &x: d) cin >> x;
cout << dc(d) << '\n';
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8YWxnb3JpdGhtPgojZGVmaW5lIFNaKFgpICgoaW50KShYKS5zaXplKCkpCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmxvbmcgbG9uZyBkYyh2ZWN0b3I8aW50PiBkKSB7CiAgICBpbnQgbWkgPSAqbWluX2VsZW1lbnQoZC5iZWdpbigpLCBkLmVuZCgpKTsKICAgIGludCBtYSA9ICptYXhfZWxlbWVudChkLmJlZ2luKCksIGQuZW5kKCkpOwogICAgaWYobWkgPT0gbWEpIHJldHVybiAwOwogICAgaW50IG1pZCA9IG1pICsgKG1hIC0gbWkpIC8gMjsgCiAgICB2ZWN0b3I8aW50PiBhLCBiOwogICAgaW50IG5vdyA9IDA7CiAgICBsb25nIGxvbmcgcmV0ID0gMDsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBTWihkKTsgaSsrKSB7CiAgICAgICAgaWYoZFtpXSA8PSBtaWQpIHsKICAgICAgICAgICAgYS5wdXNoX2JhY2soZFtpXSk7CiAgICAgICAgICAgIHJldCArPSBTWihiKTsKICAgICAgICB9IGVsc2UgIGIucHVzaF9iYWNrKGRbaV0pOwogICAgfQogICAgY291dCA8PCByZXQgPDwgZW5kbDsgCiAgICByZXR1cm4gcmV0ICsgZGMoYSkgKyBkYyhiKTsKfQppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICB2ZWN0b3I8aW50PiBkKG4pOwogICAgZm9yKGludCAmeDogZCkgY2luID4+IHg7CiAgICBjb3V0IDw8IGRjKGQpIDw8ICdcbic7Cn0=