#include #include using namespace std; using mint = atcoder::modint998244353; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fast_io(); int n, k; cin >> n >> k; vector> g(n); vector deg(n, 0); for (int i = 0; i < n; i++) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); deg[u]++; deg[v]++; } deque q; for (int i = 0; i < n; i++) { if (deg[i] == 1) { q.push_back(i); } } while (!q.empty()) { int u = q.front(); q.pop_front(); for (int v : g[u]) { if (deg[v] > 1) { deg[v]--; if (deg[v] == 1) { q.push_back(v); } } } } int loop = 0; for (int i = 0; i < n; i++) { if (deg[i] > 1) { loop++; assert(deg[i] == 2); } } mint ans = mint(k - 1).pow(loop) + (loop % 2 ? -(k - 1) : k - 1); ans *= mint(k - 1).pow(n - loop); cout << ans.val() << endl; }