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