import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 1_000_000_007 def main(A): # 総積がでかいなら、端の 1 以外はまとめてしまう p = 1 INF = 1 << 20 for x in A: p = min(p * x, INF) if p == INF: add = 0 for x in A: if x == 1: add += 1 else: break for x in A[::-1]: if x == 1: add += 1 else: break p = 1 for x in A: p = p * x % MOD return (p + add) % MOD # 以下、総積が小さい場合。 # 途中経過は、"a + b*" or "a+" と書ける。 # b ごとに optimal な a を持つ dp = {} max_v = 0 for x in A: newdp = {} vv = 0 for b, a in dp.items(): # a + b * [x] + vv = max(vv, a + b * x) # a + b * [x] * newdp[b * x] = max(newdp.get(b * x, 0), a) newdp[x] = max(newdp.get(x, 0), max_v) dp = newdp max_v = max(vv, max_v + x) return max_v A = tuple(map(int, read().split()))[1:] print(main(A))