s=int(input()) mod=10**9+7 for _ in range(s): n,m,x = map(int, input().split()) t = pow(1+m,n,mod) u = pow(1-m,n,mod) if x == 0: ans = (t+u)*pow(2,mod-2,mod) else: ans = (t-u)*pow(2,mod-2,mod) print(ans%mod)