import typing import sys # import re import math import collections # import decimal import bisect import itertools import fractions # import functools import copy import heapq import decimal # import statistics import queue # import numpy as np # sys.setrecursionlimit(10000001) INF = 10 ** 20 MOD = 10 ** 9 + 7 # MOD = 998244353 def ni(): return int(sys.stdin.buffer.readline()) def ns(): return map(int, sys.stdin.buffer.readline().split()) def na(): return list(map(int, sys.stdin.buffer.readline().split())) # ===CODE=== def main(): s = ni() for _ in range(s): n, m, x = ns() a = pow(1+m, n, MOD) b = pow(-1+m, n, MOD) if n%2: tmp = a+b if x else a-b else: tmp = a-b if x else a+b div = pow(2,MOD-2,MOD) ans = tmp*div % MOD print(ans) if __name__ == '__main__': main()