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) p *= l[i+1] ans += p print(ans%md)