MOD = 10**9 + 7 n = int(input()) A = list(map(int, input().split())) blocks = [] current_block = 1 count_ones = 0 for num in A: if num == 1: count_ones += 1 # If there was a current block (product >1), add it to blocks if current_block > 1: blocks.append(('block', current_block)) current_block = 1 else: if count_ones > 0: blocks.append(('ones', count_ones)) count_ones = 0 current_block *= num # Add remaining ones or block after loop ends if count_ones > 0: blocks.append(('ones', count_ones)) if current_block > 1: blocks.append(('block', current_block)) stack = [] for typ, val in blocks: if typ == 'block': # Try to merge with previous blocks if possible while len(stack) >= 2 and stack[-1][0] == 'ones': # Pop the 'ones' and previous block ones_typ, ones_count = stack.pop() prev_typ, prev_val = stack.pop() # Check if merging is better a = prev_val b = val k = ones_count if (a - 1) * (b - 1) > k + 1: val = a * b else: # Not better, push back and break stack.append((prev_typ, prev_val)) stack.append(('ones', ones_count)) break stack.append(('block', val)) else: stack.append((typ, val)) # Calculate the result by summing all blocks and ones result = 0 for typ, val in stack: result += val print(result % MOD)