MOD = 10**9 + 7 n = int(input()) A = list(map(int, input().split())) groups = [] i = 0 while i < n: # Skip leading 1s while i < n and A[i] == 1: groups.append(1) i += 1 if i >= n: break # Find the next number >1 after i j = i + 1 while j < n and A[j] == 1: j += 1 if j < n and A[j] > 1: # There are at least two numbers >1, start forming a group k = j while True: # Look for the next number >1 after k l = k + 1 while l < n and A[l] == 1: l += 1 if l < n and A[l] > 1: k = l else: break # Group from i to k inclusive group = A[i:k+1] product = 1 sum_group = 0 for num in group: product *= num sum_group += num if product >= sum_group: groups.append(product % MOD) else: for num in group: groups.append(num % MOD) i = k + 1 else: # Only one number >1 in this segment groups.append(A[i] % MOD) i += 1 total = sum(groups) % MOD print(total)