m = 10**9+7 n, k, x = map(int, input().split()) last_one, last_other = 1, 0 for i in range(1, n-1): last_one, last_other = (k-1) * last_other, (k-2) * last_other + last_one # print(last_one, last_other) r = (k-1) * last_other + last_one - (last_one if x == 1 else last_other) #r = pow(k-1, n-3) # up to previous-to-last layer #same = r//(k-1) # same number as in last place #r = (k-1) * same + (k-2) * (r - same) print(r % m)