M=10**9+7 for _ in range(int(input())):n,m,x=map(int,input().split());print(((1-(x+n)%2*2)*pow(m-1,n,M)+pow(m+1,n,M))*pow(2,M-2,M)%M)