n = int(input()) seql = list(map(int, input().split())) n -= 2 ans = 0 z = 10**9+7 for i in range(n+1): p = seql[i] if not p: break seql[i+1] = seql[i+1]*p%z ans = (ans+pow(3, n, z)*2*p)%z n -= 1 else: ans += seql[-1] print (ans%z)