n = int(input()) seql = list(map(int, input().split())) import operator, itertools n -= 2 ans = 0 for p in itertools.accumulate(seql, func=operator.mul): if not p: break ans += pow(3, n, 10**9+7) * 2*p if n >= 0 else p n -= 1 print (ans%(10**9+7))