N = int(input()) A = list(map(int, input().split())) A.append(0) all = N * (N + 1) // 2 N += 1 def search_one(): a = 0 b = len(A) while True: j = a + (b - a) // 2 if '1' * j in A: a = j else: b = j if b - a < 3: break return b onecnt = 0 now = 'zero' for i in range(N): if A[i] == 1: if now == 'zero': cnt = 1 now = 'one' else: cnt += 1 else: if now == 'one': onecnt += (cnt * (cnt + 1) // 2) #print(i, cnt) now = 'zero' print(all - onecnt)