MOD = 10**9 + 7 n = int(input()) a = list(map(int, input().split())) if n == 0: print(0) exit() # Compute the product of all elements modulo MOD product_all = 1 for num in a: product_all = (product_all * num) % MOD # Compute the dynamic programming solution max_sum = a[0] current_product = a[0] for i in range(1, n): option_add = max_sum + a[i] option_multiply = (max_sum - current_product) + (current_product * a[i]) if option_add > option_multiply: new_max_sum = option_add new_current_product = a[i] else: new_max_sum = option_multiply new_current_product = (current_product * a[i]) % MOD max_sum = new_max_sum current_product = new_current_product # The maximum value is the larger of the DP result and the product of all elements max_val = max(max_sum % MOD, product_all) print(max_val % MOD)