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