mod = 7+10**9 def ncr(n,c,mod): x = 1 y = 1 for i in range(n-c+1,n+1): x = x*i%mod for j in range(1,c+1): y = y*j%mod return (x * pow(y, mod-2, mod)) % mod S = int(input()) for _ in range(S): n, m, x = map(int, input().split()) count = 0 if x == 1: count = 1 else: count = 2 ans = 0 point = 0 while count <= n: point += (ncr(n, count, mod) * pow(m, count, mod)) point %= mod count += 2 print(point%mod)