MOD = 10**9 + 7 n = int(input()) a = list(map(int, input().split())) if n == 0: print(0) else: sum_add = 0 product_add = a[0] sum_mult = 0 product_mult = a[0] for i in range(1, n): # Calculate new add state new_sum_add = max(sum_add + product_add, sum_mult + product_mult) new_product_add = a[i] # Calculate options for multiply state option1_sum = sum_add option1_product = product_add * a[i] option1_total = option1_sum + option1_product option2_sum = sum_mult option2_product = product_mult * a[i] option2_total = option2_sum + option2_product if option1_total > option2_total: new_sum_mult = option1_sum new_product_mult = option1_product elif option1_total < option2_total: new_sum_mult = option2_sum new_product_mult = option2_product else: if option1_product > option2_product: new_sum_mult = option1_sum new_product_mult = option1_product else: new_sum_mult = option2_sum new_product_mult = option2_product # Update states for next iteration sum_add, product_add = new_sum_add, new_product_add sum_mult, product_mult = new_sum_mult, new_product_mult # Determine the maximum result result = max(sum_add + product_add, sum_mult + product_mult) % MOD print(result)