k=int(input()) num_list=[map(int,input().split()) for i in range(k)] mod=10**9+7 for i in range(k): [n,m,x]=num_list[i] if(x==0): a=pow(1+m,n,mod)+pow(1-m,n,mod) if(a%2==1): a+=mod a=int(a/2-1) else: a=pow(1+m,n,mod)-pow(1-m,n,mod) if(a%2==1): a+=mod a=int(a/2) print(a%mod)