list = Hash.new(0) up_list = {} down_list = {} n = gets.to_i s = 0 gets.split.lazy.map(&:to_i).each do |i| up_list.each do |j, k| s += k[0] - k[1][i] if i > j end down_list.each do |j, k| s += k[0] - k[1][i] if i < j end list.each do |j, k| if i > j down_list[i] ||= [0, Hash.new(0)] down_list[i][0] += k down_list[i][1][j] += k elsif i < j up_list[i] ||= [0, Hash.new(0)] up_list[i][0] += k up_list[i][1][j] += k end end list[i] += 1 # p list # p up_list # p down_list end puts s