using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static List NList => ReadLine().Split().Select(int.Parse).ToList(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, k) = (c[0], c[1]); var map = NMap(n); WriteLine(Color(n, k, map)); } static long Color(int n, int k, int[][] map) { var tree = new List[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List(); foreach (var edge in map) { tree[edge[0]].Add(edge[1]); tree[edge[1]].Add(edge[0]); } var g = new SCCGraph(n); var visited = new bool[n]; CreateSCC(-1, 0, tree, visited, g); var scc = g.SCC(); var loopsize = 0; for (var i = 0; i < scc.Length; ++i) if (scc[i].Count > 1) { loopsize = scc[i].Count; break; } var dp = new long[loopsize][]; for (var i = 0; i < dp.Length; ++i) dp[i] = new long[2]; dp[0][0] = 1; for (var i = 1; i < dp.Length; ++i) { if (i + 1 < dp.Length) dp[i][0] = dp[i - 1][1]; dp[i][1] = (dp[i - 1][0] * (k - 1) % mod + dp[i - 1][1] * (k - 2) % mod) % mod; } var ans = (dp[^1][0] + dp[^1][1]) * k % mod; for (var i = loopsize; i < n; ++i) ans = ans * (k - 1) % mod; return ans; } static int mod = 998_244_353; static void CreateSCC(int prev, int cur, List[] tree, bool[] visited, SCCGraph g) { if (visited[cur]) return; visited[cur] = true; foreach (var next in tree[cur]) { if (prev == next) continue; g.AddEdge(cur, next); CreateSCC(cur, next, tree, visited, g); } } class SCCGraph { private int _n; private List<(int from, int to)> edges; public SCCGraph(int n) { _n = n; edges = new List<(int, int)>(); } public int NumVertices() { return _n; } public void AddEdge(int from, int to) { edges.Add((from, to )); } public (int, int[]) SCCIds() { var g = new CSR(_n, edges); var nowOrd = 0; var groupNum = 0; var visited = new List(_n); var low = new int[_n]; var ord = Enumerable.Repeat(-1, _n).ToArray(); var ids = new int[_n]; void DFS(int v) { low[v] = ord[v] = nowOrd++; visited.Add(v); for (var i = g.Start[v]; i < g.Start[v + 1]; ++i) { var to = g.EList[i]; if (ord[to] == -1) { DFS(to); low[v] = Math.Min(low[v], low[to]); } else low[v] = Math.Min(low[v], ord[to]); } if (low[v] == ord[v]) { while (true) { var u = visited.Last(); visited.RemoveAt(visited.Count - 1); ord[u] = _n; ids[u] = groupNum; if (u == v) break; } ++groupNum; } } for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i); for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i]; return (groupNum, ids); } public List[] SCC() { var (groupNum, list) = SCCIds(); var counts = new int[groupNum]; foreach (var x in list) ++counts[x]; var groups = new List[groupNum]; for (var i = 0; i < groups.Length; ++i) groups[i] = new List(counts[i]); for (var i = 0; i < _n; ++i) groups[list[i]].Add(i); return groups; } } // 有向辺のリスト class CSR { public int[] Start { get; private set; } public E[] EList { get; private set; } public CSR(int n, List<(int, E)> edges) { Start = new int[n + 1]; EList = new E[edges.Count]; foreach (var e in edges) ++Start[e.Item1 + 1]; for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1]; var counter = (int[])Start.Clone(); foreach (var e in edges) { EList[counter[e.Item1]++] = e.Item2; } } } }