import sys input = sys.stdin.readline mod = 10 ** 9 + 7 for _ in range(int(input())): N, M, X = map(int, input().split()) if X: print((pow(M + 1, N, mod) - pow(1 - M, N, mod)) * pow(2, mod - 2, mod) % mod) else: print((pow(M + 1, N, mod) + pow(M - 1, N, mod)) * pow(2, mod - 2, mod) % mod)