N = int(input()) A = list(map(int,input().split())) cnt_1 = A.count(1) cnt_2 = A.count(2) cnt_other = len(A)-cnt_1-cnt_2 ans = 0 ans += cnt_1*(cnt_1-1)//2 * 2 ans += cnt_1 * cnt_2 * 3 ans += cnt_1 * cnt_other * 2 ans += cnt_2 * cnt_other ans += cnt_other*(cnt_other-1) print(ans)