import std; int readInt() { int x; readf!" %d"(x); return x; } const long P = (1e9 + 7).to!long; long qpow(long a, long b, long m = P) { long r = 1 % P, t = a % P; for(; b; b /= 2) { if(b & 1) r = r * t % P; t = t * t % P; } return r; } void main() { int n = readInt; auto a = new int[](n).map!(i => readInt); long ans = 0, mul = 1; foreach(i; 0 .. n) { mul = mul * a[i] % P; if(i + 1 < n) { ans += mul * 2 * qpow(3, n - i - 2) % P; } else { ans += mul; } ans %= P; } writeln(ans); }