Under construction

Still being built. Things may change

Complaint Sort

Interview

Time limit1sMemory limit256 MB

Summary
Given a sequence of n values, count the strictly decreasing subsequence triples (i < j < k with a_i > a_j > a_k).
Level

Medium6 of 10

Topics
Array, Combinatorics, Prefix sum, Bit manipulation
Solved
No attempts yet

Problem

The upper bound on the number of swaps a sorting algorithm needs is n2n^2, which is easy to prove. Consider picking two out-of-order elements — a pair with i<ji < j but ai>aja_i > a_j — and swapping their positions. Such an out-of-order pair is called an inversion, and there are at most n(n−1)/2n(n-1)/2 inversions.

Hyeonju is a child with many complaints about the world. He is unhappy even about picking just two elements when sorting. Instead, he picks three elements with i<j<ki < j < k and ai>aj>aka_i > a_j > a_k, and rewrites their positions in the order ak,aj,aia_k, a_j, a_i.

Hyeonju named this method the complaint sort algorithm and now wants to find its upper bound. Write a program that counts how many triples of elements Hyeonju can pick — that is, count the number of index triples (i,j,k)(i, j, k) with i<j<ki < j < k and ai>aj>aka_i > a_j > a_k.

Input

The first line contains the length nn of the sequence. (1≤n≤1051 \le n \le 10^5)

The second line contains the elements of the sequence separated by spaces. Each element is an integer between 11 and nn, inclusive.

Output

Print, on the first line, the number of index triples (i,j,k)(i, j, k) that satisfy i<j<ki < j < k and ai>aj>aka_i > a_j > a_k.

Examples2

  1. Example 1

    Input
    4
    3 3 2 1
    
    Expected output
    2
    
  2. Example 2

    Input
    3
    1 2 3
    
    Expected output
    0