class Range def upper_bound ac, wa = self.begin, self.end wa += 1 unless self.exclude_end? while ac + 1 < wa wj = (ac + wa) / 2 if yield(wj) ac = wj else wa = wj end end ac end end N = gets.to_i A = gets.split.map(&:to_i) counts = [[0, 0]] A.each do |x| a, b = counts[-1] case x when 1 counts << [a + 1, b] when 2 counts << [a, b + 1] else counts << [a, b] end end ans = 0 (0 .. N - 2).each do |l| ones = counts[N][0] - counts[l][0] twos = counts[N][1] - counts[l][1] others = N - l - ones - twos - 1 case A[l] when 1 ans += (ones + others) * 2 + twos * 3 when 2 ans += (twos + others) * 1 + ones * 3 else ans += (twos + others) * 1 + ones * 2 end end puts ans