#include "bits/stdc++.h" #include "atcoder/all" using namespace std; using namespace atcoder; using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } vector> mul(vector>A, vector>B) { vector> C(A.size(), vector(B[0].size())); rep(i, A.size()) rep(j, B[0].size()) rep(k, A[0].size()) C[i][j] += A[i][k] * B[k][j]; return C; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, a, b, k; cin >> n >> a >> b >> k; vector> A(2, vector(2)); vector> B(2, vector(2)); mint q = -1; mint p = ((mint)a*a + b * b + k) / (a*b); A[0][0] = p; A[0][1] = q; A[1][0] = 1; A[1][1] = 0; B[0][0] = b; B[0][1] = 0; B[1][0] = a; B[1][1] = 0; if (1 == n) { cout << a << endl; return 0; } if (2 == n) { cout << b << endl; return 0; } n -= 2; while (n > 0) { if (1 == n % 2) { B = mul(A, B); } n /= 2; A = mul(A, A);//B*B } mint ans = B[0][0]; cout << ans.val() << endl; return 0; }