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

#CC0000015. Đếm đội ba người hợp lệ (Count Number of Teams)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Đếm đội ba người hợp lệ (Count Number of Teams)

Count Number of Teams

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn people standing in a line, each with a distinct rating. Choose positions i<j<ki<j<k. A team is valid when the ratings are strictly increasing (ai<aj<aka_i<a_j<a_k) or strictly decreasing (ai>aj>aka_i>a_j>a_k). Count all valid teams; a person may belong to several teams.

Input

The first line contains the number nn of people in a row. The next line contains their nn ratings a1,…,ana_1,\ldots,a_n in standing order.

Output

Print one integer: the number of valid three-person teams.

Subtasks

  • Subtask 1 (20 points): 3≤n≤103\le n\le 10; 1≤ai≤1051\le a_i\le 10^5 for every ii; all aia_i are pairwise distinct.
  • Subtask 2 (30 points): 3≤n≤1203\le n\le 120; 1≤ai≤1051\le a_i\le 10^5 for every ii; all aia_i are pairwise distinct.
  • Subtask 3 (50 points): 3≤n≤10003\le n\le 1000; 1≤ai≤1051\le a_i\le 10^5 for every ii; all aia_i are pairwise distinct.

Examples

Input

5
2 5 3 4 1

Output

3

Explanation

The increasing team has ratings (2,3,4)(2,3,4). The decreasing teams have ratings (5,3,1)(5,3,1) and (5,4,1)(5,4,1). No other ordered triple is strictly monotone, giving 33 teams.