import sys input = sys.stdin.readline import heapq N=int(input()) A=list(map(int,input().split())) MIN=[] heapq.heappush(MIN,(A[0],0)) heapq.heappush(MIN,(A[1],1)) MAX=[] heapq.heappush(MAX,(-A[0],0)) heapq.heappush(MAX,(-A[1],1)) ANS=0 now=1 for i in range(N): #print(i,now) while True: if now==N: ANS+=now-i-1 break while MIN[0][1]k: ANS+=now-i-1 heapq.heappush(MIN,(x,xi)) heapq.heappush(MIN,(y,yi)) heapq.heappush(MAX,(l,li)) break else: now+=1 heapq.heappush(MIN,(x,xi)) heapq.heappush(MIN,(y,yi)) heapq.heappush(MAX,(l,li)) if now==N: continue heapq.heappush(MIN,(A[now],now)) heapq.heappush(MAX,(-A[now],now)) #print(i,now,k,l,MIN,MAX) print(ANS)