Under construction

Still being built. Things may change

Simple Permutation Problem

Interview

Time limit1sMemory limit1024 MB

Summary
Count position pairs i < j in a permutation where both values are larger than every element strictly between them.
Level

Medium5 of 10

Topics
Stack, Array
Solved
No attempts yet

Problem

A permutation PP of length NN is given. A permutation of length NN uses each integer from 11 to NN exactly once, arranged in any order. Count the pairs (i,j)(i, j) that satisfy both conditions below.

  • 1≤i<j≤N1 \le i < j \le N
  • min⁡(Pi,Pj)>max⁡(0,Pi+1,Pi+2,⋯ ,Pj−1)\min(P_i, P_j) > \max(0, P_{i+1}, P_{i+2}, \cdots, P_{j-1})

PiP_i is the ii-th element of PP.

Input

The first line contains NN, the length of the permutation.

The second line contains NN integers P1,P2,⋯ ,PNP_1, P_2, \cdots, P_N, separated by spaces.

Output

Print the answer to the problem.

Constraints

1≤N≤200,0001 \le N \le 200{,}000

Examples3

  1. Example 1

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

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

    Input
    2
    2 1
    
    Expected output
    1