# はじにある1は足したほうがよい N = int(input()) A = list(map(int,input().split())) from collections import deque q = deque(A) DIV = 10 ** 9 + 7 ans = 0 while q: found = False if q[0] == 1: ans += 1 q.popleft() found = True elif q[-1] == 1: ans += 1 q.pop() found = True if not found: break def f(X): res = 1 for x in X: res *= x res %= DIV return res print((ans + f(q)) % DIV)