N = int(input()) A = list(map(int, input().split())) X = [0] * 4 ans = 0 for i in reversed(range(N - 1)): X[min(A[i + 1], 3)] += 1 c = N - i - 1 if A[i] == 1: ans += 2 * (X[1] + X[3]) ans += 3 * X[2] elif A[i] == 2: ans += X[2] + X[3] ans += 3 * X[1] else: # A[i] >= 3 ans += X[2] + X[3] ans += 2 * X[1] print(ans)