#include #include using namespace std; const long long md = 1000000007; long long c[200009], l[200009]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; l[0] = c[0] = 1; for (int i = 1; i <= n; i++) { int a; cin >> a; l[i] = (l[i - 1] * a) % md; c[i] = (c[i - 1] * 3) % md; } long long ans = l[n]; for (int i = 1; i < n; i++) ans = (ans + l[i] * c[n - i - 1] * 2) % md; cout << ans << '\n'; return 0; }