#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const ULL M = 1000000007; const int matrix_sz = 2; struct Matrix { ULL A[matrix_sz][matrix_sz] = {}; ULL* operator[](int x) { return A[x]; } const ULL* operator[](int x) const { return A[x]; } }; Matrix MatrixId() { Matrix ans; rep(i, matrix_sz) ans[i][i] = 1; return ans; } Matrix operator*(Matrix a, Matrix b) { Matrix ans; rep(i, matrix_sz) rep(j, matrix_sz) rep(k, matrix_sz) ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % M; return ans; } Matrix operator^(Matrix a, ULL i) { if (i == 0) return MatrixId(); Matrix ans = (a * a) ^ (i / 2); if (i & 1) ans = ans * a; return ans; } int main() { int S; cin >> S; while (S--) { ULL N, g, x; cin >> N >> g >> x; Matrix G; G[0][0] = 1; G[1][1] = 1; G[0][1] = g; G[1][0] = g; G = G ^ N; cout << G[0][x] << endl; } return 0; }