#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define matrix vector<vector<ll>>
using namespace std;
const int base=trunc(1e9)+7;
matrix a,b;
matrix operator * (const matrix &a, const matrix &b) {
matrix c = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
for (int i = 0; i <=4; i++) {
for (int j = 0; j <=4; j++) {
for (int k = 0; k <=4; k++) {
c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % base) % base;
}
}
}
return c;
}
matrix mu(ll n) {
if (n == 1) return a;
matrix t = mu(n / 2);
t = t * t;
if (n % 2 == 1) t = t * a;
return t;
}
int main()
{
ll n,a1,b1,c1,d1,e1;
cin>>n>>a1>>b1>>c1>>d1>>e1;
a= {{0,0,c1,0,0},{1,0,b1,0,0},{0,1,a1,0,0},{0,0,d1,1,0},{0,0,e1,1,1}};
b= {{1,1,1,4,1},{0,0,0,0,0},{ 0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
matrix d=mu(n-3);
matrix d2=b*d;
cout<<d2[0][2]% base;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgbWF0cml4IHZlY3Rvcjx2ZWN0b3I8bGw+Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgYmFzZT10cnVuYygxZTkpKzc7Cm1hdHJpeCBhLGI7Cm1hdHJpeCBvcGVyYXRvciAqIChjb25zdCBtYXRyaXggJmEsIGNvbnN0IG1hdHJpeCAmYikgewogICAgbWF0cml4IGMgPSB7ezAsMCwwLDAsMH0sezAsMCwwLDAsMH0sezAsMCwwLDAsMH0sezAsMCwwLDAsMH0sezAsMCwwLDAsMH19OwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD00OyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8PTQ7IGorKykgewogICA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