#include #define REP(i,n) for (int i = 0; i < (n); ++i) template inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;} template inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;} using namespace std; using ll = long long; using P = pair; using Pl = pair; using veci = vector; using vecl = vector; using vecveci = vector>; using vecvecl = vector>; const int MOD = 1e9+7; const double pi = acos(-1); const ll INF = 1e18; template struct modint{ unsigned int x; constexpr modint()noexcept:x(){} template constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){} constexpr unsigned int val()const noexcept{return x;} constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;} constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;} constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;} constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;} constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;} constexpr modint&operator-=(const modint&a)noexcept{if(x>=1)if(n&1)r*=x; return r; } constexpr modint inv()const noexcept { int s=x,t=m,x=1,u=0; while(t) { int k=s/t; s-=k*t; swap(s,t); x-=k*u; swap(x,u); } return modint(x); } friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;} friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;} friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;} friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;} friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;} friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;} friend ostream&operator<<(ostream&os,const modint&a){return os<>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;} }; using mint = modint<(int)1e9+7>; int main() { int S; cin >> S; while(S--) { int N,X; mint M; cin >> N >> M >> X; mint A = (M+1).pow(N), B = (1-M).pow(N); if(X == 0) cout << (A+B)/2 << endl; else cout << (A-B)/2 << endl; } }