#include long long int h[400005], l; int comp_h(long long int a, long long int b) { if (h[a] > h[b]) return 1; else return -1; } void swap_h(long long int a, long long int b) { long long int f = h[a]; h[a] = h[b]; h[b] = f; return; } void push(long long int ne) { h[l] = ne; long long int p = l; l++; for (; p > 0; p = (p - 1) / 2) if (comp_h((p - 1) / 2, p) > 0) swap_h((p - 1) / 2, p); return; } long long int pop() { l--; swap_h(0, l); long long int p = 0; for (;;) { if (2 * p + 2 < l) { if (comp_h(2 * p + 1, 2 * p + 2) > 0) { if (comp_h(p, 2 * p + 2) > 0) swap_h(p, 2 * p + 2); p = 2 * p + 2; } else { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } } else if (2 * p + 1 < l) { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } else break; } return h[l]; } long long int n; long long int s[400005]; long long int fined_idx(long long int v) { long long int min, mid, max; min = -1; max = 2 * n; while (max - min > 1) { mid = (max + min) / 2; if (s[mid] < v) min = mid; else max = mid; } return max; } long long int a[200005]; long long int dp[400005]; int main() { scanf("%lld", &n); long long int i; for (i = 0; i < n; i++) scanf("%lld", &a[i]); long long int p = 998244353; l = 0; for (i = 0; i < n; i++) push(a[i]); for (i = 0; i < n; i++) push(a[i] - 1); for (i = 0; i < 2 * n; i++) s[i] = pop(); for (i = 0; i < 2 * n; i++) dp[i] = 0; long long int ans = 0; for (i = 0; i < n; i++) { ans += dp[fined_idx(a[i] - 1)]; dp[fined_idx(a[i])] += dp[fined_idx(a[i] - 1)] + 1; } printf("%lld\n", ans); return 0; }