import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); int mod = 1000000007; long[] p3 = new long[n]; p3[0] = 1; for (int i = 1; i < n; i++) { p3[i] = p3[i - 1] * 3; p3[i] %= mod; } long ans = 0; long b = 1; for (int i = 0; i < n - 1; i++) { b *= a[i]; b %= mod; long val = b * p3[n - 2 - i] * 2 % mod; ans += val; } b *= a[n - 1]; b %= mod; ans += b; ans %= mod; System.out.println(ans); } }