n = int(input()) a = list(map(int,input().split())) mod = 10**9+7 ans = 0 # 全て*の場合を先に加算 num1 = 1 for i in range(n): num1 *= a[i] num1 %= mod ans += num1 for i in range(n-1): if i == 0: former = a[i] tmp = 2*pow(3,n-i-2,mod) tmp %= mod ans += former*tmp ans %= mod else: former *= a[i] tmp = 2*pow(3,n-i-2,mod) tmp %= mod former %= mod ans += former*tmp ans %= mod print(ans)