N = int(input()) A = list(map(int,input().split())) count1 = 0 count2 = 0 count = 0 for i in A: if i == 1:count1 += 1 elif i == 2:count2 += 1 else:count += 1 S = count1 * (count1 - 1) + 3 * count1 * count2 + 2 * count1 * count + count2 * (count2 - 1) // 2 + count2 * count + count * (count-1) // 2 print(S)