#include using namespace std; using ll = long long; const ll MOD = 1e9 + 7; ll modPow(ll a, ll x) { if (x == 0) return 1; if (x == 1) return a; ll temp = modPow(a, x / 2); ll ans = (temp * temp) % MOD; if (x % 2 == 1) ans = (ans * a) % MOD; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll S; cin >> S; for (ll i = 0; i < S; i++) { ll N, M, X; cin >> N >> M >> X; ll A = modPow(1 + M, N); ll B = modPow(1 - M + MOD, N); ll ans; if (X == 1) { ans = ((A - B + MOD) % MOD) * 500000004; } else { ans = ((A + B + MOD) % MOD) * 500000004; } ans %= MOD; cout << ans << '\n'; } return 0; }