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

#CC0000021. So sánh số lần xuất hiện trước và sau (Pashmak and Parmida's problem)

HỌC THEO LỘ TRÌNH

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

3 lộ trình

So sánh số lần xuất hiện trước và sau (Pashmak and Parmida's problem)

Pashmak and Parmida's problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

For every position ii, let LiL_i be the number of occurrences of aia_i in the prefix from 11 through ii. For every jj, let RjR_j be the number of occurrences of aja_j in the suffix from jj through nn. Count pairs i<ji<j such that Li>RjL_i>R_j. These are occurrence counts, not comparisons of aia_i and aja_j.

Input

The first line contains nn. The second line contains nn positive integers a1,…,ana_1,\ldots,a_n.

Output

Print one 64-bit integer: the number of index pairs satisfying the frequency inequality.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤1091\le a_i\le 10^9 for every ii.
  • Subtask 2 (30 points): 1≤n≤12001\le n\le 1200; 1≤ai≤1091\le a_i\le 10^9 for every ii.
  • Subtask 3 (50 points): 1≤n≤10000001\le n\le 1000000; 1≤ai≤1091\le a_i\le 10^9 for every ii.

Examples

Input

7
1 2 1 1 2 2 1

Output

8

Explanation

Compute prefix occurrence counts LL and suffix occurrence counts RR at every position, then count only index pairs i<ji<j with Li>RjL_i>R_j. The supplied sample yields 88 such pairs.