Jim and the Skyscrapers#542

Jim and the Skyscrapers

1 second
128 megabytes
Hard (≈1650)

Given an array h_1, h_2, ..., h_n. Find the number of pairs of indices (i, j)~(1 \leq i < j \leq n) that satisfy the following conditions: h_i = h_j and \underset{i < k < j}{\max} h_k \leq h_j.

Input

The first line contains one integer n~(1 \le n \le 3 \cdot 10^5). The second line contains integers h_1, h_2, ..., h_n~(1 \le h_i \le 10^6).

Output

Print the number of such pairs of indices.

Examples

In the first example, the desired pairs are: (1, 5), (1, 6) (5, 6) and .

Example 1

Loading
Loading

Example 2

Loading
Loading