N = gets.to_i A = gets.split.map(&:to_i) counter = Hash.new(0) ans = 0 N.times do |i| a = A[i] counter[a] += 1 if a == 1 ans += 3 * counter[2] ans += 2 * (i - counter[2]) elsif a == 2 ans += 3 * counter[1] ans += (i - counter[1]) else ans += 2 * counter[1] ans += (i - counter[1]) end end puts ans