PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000090. Nghịch thế bộ ba (Mega Inversions)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nghịch thế bộ ba (Mega Inversions)

Mega Inversions

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given a sequence of positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, a mega inversion is a triple of positions i<j<ki<j<k whose values are strictly decreasing:

ai>aj>ak.a_i>a_j>a_k.

Equal values may occur but never satisfy a strict comparison. Triples are distinguished by their chosen positions. Count all mega inversions; the answer may require a 64-bit integer.

Input

The first line contains the integer nn.

The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the number of triples i<j<ki<j<k with ai>aj>aka_i>a_j>a_k.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 1≤ai≤n1\le a_i\le n for every ii.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤ai≤n1\le a_i\le n for every ii.
  • Subtask 3 (50 points): 1≤n≤5000001\le n\le 500000; 1≤ai≤n1\le a_i\le n for every ii.

Examples

Input

4
3 3 2 1

Output

2

Explanation

The sequence is [3,3,2,1][3,3,2,1]. Exactly two decreasing triples exist: indices (1,3,4)(1,3,4) and (2,3,4)(2,3,4). Both yield the values (3,2,1)(3,2,1).

Any triple containing both occurrences of 33 fails because 3>33>3 is false. Therefore the output is 2.