import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable long MOD = 10^^9 + 7; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; long ans = 0; long pro = 1; long co = 2; foreach (i; 0..N-2) co = co * 3 % MOD; foreach (i, a; A) { pro = pro * a % MOD; (ans += pro * co) %= MOD; if (i + 2 == N) { co = co * powmod(2, MOD-2, MOD) % MOD; } else { co = co * powmod(3, MOD-2, MOD) % MOD; } } ans.writeln; } long powmod(long a, long x, long m) { long ret = 1; while (x) { if (x % 2) ret = ret * a % m; a = a * a % m; x /= 2; } return ret; }