N = int(input()) l = list(map(int, input().split())) md = 1000000007 p = l[0] ans = 0 for i in range(N-1): ans += (2 * 3**(N-i-2) * p) % md p = p * l[i+1] % md ans += p print(ans%md)