e-olymp
Competitions

22-26 февраля. Старшая лига - Дерево Фенвика

Mega Inversions

prb6021 The n2 upper bound for any sorting algorithm is easy to obtain: just take two elements that are misplaced with respect to each other and swap them. Conrad conceived an algorithm that proceeds by taking not two, but three misplaced elements. That is, take three elements ai >aj >ak with i < j < k and place them in order ak, aj, ai. Now if for the original algorithm the steps are bounded by the maximum number of inversions n (n - 1) / 2, Conrad is at his wits' end as to the upper bound for such triples in a given sequence. He asks you to write a program that counts the number of such triples.

Input

The first line is the length of the sequence n (1n105). The next line contains the integer sequence a1,a2, ..., an (1ain).

Output

Print the number of inverted triples.

Time limit 1 second
Memory limit 128 MiB
Input example #1
3
1 2 3
Output example #1
0
Input example #2
4
3 3 2 1
Output example #2
2
Author Serikzhan S. Kazi
Source 2011 Nordic Collegiate Programming Contest, Problem B