import Matrix.{inPlaceTimes, tabulate} import scala.annotation.tailrec import scala.io.StdIn.* import scala.math.* import scala.util.chaining.* object ModInt: opaque type ModInt = Long val zero: ModInt = 0 val one: ModInt = 1 extension (value: Long) inline def asModInt(using inline mod: Int): ModInt = value % mod extension (value: Int) inline def asModInt(using inline mod: Int): ModInt = value % mod trait Converter[T]: inline def convert(value: T)(using inline mod: Int): ModInt inline given Converter[Int] with override inline def convert(value: Int)(using inline mod: Int) = value.asModInt inline given Converter[Long] with override inline def convert(value: Long)(using inline mod: Int) = value.asModInt inline given Converter[ModInt] with override inline def convert(value: ModInt)(using inline mod: Int) = value extension (modInt: ModInt) inline def asLong(using inline mod: Int): Long = (modInt + mod) % mod inline def inverse(using inline mod: Int): ModInt = modInt.powMod(mod - 2) inline def powMod(exp: Long)(using inline mod: Int): ModInt = var result = 1L var base = modInt var e = exp while e > 0 do if (e & 1) == 1 then result = result * base % mod base = base * base % mod e >>= 1 result inline def powMod(exp: Int)(using inline mod: Int): ModInt = powMod(exp.toLong) extension [L: Converter, R: Converter] (left: L) inline def +(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) + summon[Converter[R]].convert(right)).asModInt inline def -(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) - summon[Converter[R]].convert(right)).asModInt inline def *(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right)).asModInt inline def /(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right).inverse).asModInt import ModInt.* inline given mod: Int = 998244353 class Matrix private(val row: Int, val column: Int, private val matrix: Array[Array[ModInt]]): def this(row: Int, column: Int) = this(row, column, Array.fill(row){Array.fill(column){ModInt.zero}}) def apply(i: Int, j: Int): ModInt = matrix(i)(j) def update(i: Int, j: Int, value: ModInt) = matrix(i)(j) = value def *(right: Matrix): Matrix = val newRow = row val newColumn = right.column val mid = column Matrix.tabulate(newRow, newColumn){(i, j) => (0 until mid).foldLeft(ModInt.zero){(acc, k) => acc + matrix(i)(k) * right.matrix(k)(j) } } def pow(exp: Long): Matrix = require(row == column) var temp = Matrix(row, row) var result = Matrix.e(row, row) var base = this var e = exp while e > 0 do if (e & 1) == 1 then inPlaceTimes(result, base, temp) val t = temp temp = result result = t inPlaceTimes(base, base, temp) val t = temp temp = base base = t e >>= 1 result object Matrix: def tabulate(row: Int, column: Int)(generator: (Int, Int) => ModInt): Matrix = Matrix(row, column, Array.tabulate(row){i => Array.tabulate(column){j => generator(i, j)}}) def e(row: Int, column: Int): Matrix = Matrix.tabulate(row, column){(i, j) => if i == j then ModInt.one else ModInt.zero } private def inPlaceTimes(left: Matrix, right: Matrix, dest: Matrix) = val newRow = left.row val newColumn = right.column val mid = left.column for i <- 0 until newRow j <- 0 until newColumn do var sum = ModInt.zero for k <- 0 until mid do sum += left(i, k) * right(k, j) dest(i, j) = sum @main def main = val (n, m, t) = readLine().split(' ').pipe{case Array(n, m, t) => (n.toInt, m.toInt, t.toLong)} val roads = Array.fill(m){ val Array(u, v) = readLine().split(' ').map(_.toInt) (u, v) } val matrix = Matrix(n, n) for (u, v) <- roads do matrix(u, v) = ModInt.one matrix(v, u) = ModInt.one val initial = Matrix(n, 1).tap(m => m(0, 0) = ModInt.one) val result = matrix.pow(t) * initial println(result(0, 0))