import sys input = sys.stdin.readline N=int(input()) A=list(map(int,input().split())) A.append(A[-1]) A.append(A[-1]) ans = 0 a0 = A[0] flg=0 lst = [] cnt = 0 for i in range(1,N+1): a = A[i] if a0 < a > A[i+1]: flg=1 if a==a0: if flg: for j in lst: ans += cnt-j flg=0 cnt=0 elif a0 < a: lst.append(cnt) else: cnt+=1 a0 = a print(ans)