def solve(area): next_area = list(area) selected_ind = [0] * len(area) if len(next_area) <= 1: return 0 elif len(next_area) == 2: return 1 while True: max_ind = next_area.index(max(next_area)) min_ind = next_area.index(min(next_area)) if max_ind == min_ind: break selected_ind[area.index(next_area[min_ind])] = 1 next_area = next_area[0:min_ind] if max_ind < min_ind else next_area[min_ind + 1: len(next_area)] max_ind = area.index(max(area)) 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))