import sys from operator import itemgetter from collections import defaultdict, deque import heapq import bisect stdin=sys.stdin sys.setrecursionlimit(10 ** 8) ip=lambda: int(sp()) fp=lambda: float(sp()) lp=lambda:list(map(int,stdin.readline().split())) sp=lambda:stdin.readline().rstrip() Yp=lambda:print('Yes') Np=lambda:print('No') inf = 1 << 60 eps = 1e-9 sortkey = itemgetter(0) mod = 10 ** 9+ 7 ############################################################### N = ip() A = lp() ans = 0 ## a1 +- a2... now = 1 for i in range(N - 1): now *= A[i] ans += now * 2 * pow(3, N - 1 - (i + 1), mod) ans %= mod ans += now * A[-1] ans %= mod print(ans)