MOD = 998244353i64 n, k = read_line.split.map(&.to_i) ord = Array.new(n, 0) g = Array.new(n) { [] of Int32 } n.times do u, v = read_line.split.map { |v| v.to_i - 1 } ord[u] += 1 ord[v] += 1 g[u] << v g[v] << u end q = n.times.select { |i| ord[i] == 1 }.to_a qi = 0 ans = 1i64 while qi < q.size cur = q[qi] g[cur].each do |a| ord[a] -= 1 if ord[a] == 1 q << a end end qi += 1 ans *= (k - 1) ans %= MOD end n -= qi dp = Array.new(n) { Array.new(2, 0i64) } dp[0][0] = k 1.upto(n - 1) do |i| dp[i][0] = dp[i - 1][1] dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % MOD end puts ans * dp[-1][1] % MOD