def solve(area): if len(area) <= 1: return 0 selected_ind = [0] * len(area) max_ind = area.index(max(area)) min_ind = max_ind for i in reversed(range(max_ind)): if area[min_ind] > area[i]: min_ind = i selected_ind[min_ind] = 1 min_ind = max_ind for i in range(max_ind+1, len(area)): if area[min_ind] > area[i]: min_ind = i selected_ind[min_ind] = 1 count = sum(selected_ind) tmp_sum = sum(selected_ind[0:max_ind]) tmp_len = len(selected_ind[0:max_ind]) if tmp_len == tmp_sum and tmp_len > 2: count += tmp_sum * (tmp_sum - 1) / 2 else: count += solve(area[0:max_ind]) tmp_sum = sum(selected_ind[max_ind + 1: len(area)]) tmp_len = len(selected_ind[max_ind + 1: len(area)]) if tmp_len == tmp_sum and tmp_len > 2: count += tmp_sum * (tmp_sum - 1) / 2 else: count += solve(area[max_ind + 1: len(area)]) return int(count) N = int(input()) data = input().split() Input = [int(s) for s in data] print(solve(Input))