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