use std::ops::{Mul, MulAssign}; #[derive(Clone)] struct Matrix(Vec>); impl Matrix { const MOD: usize = 1_000_000_007; fn pow(self, mut n: usize) -> Matrix { let p = self.0.len(); let mut a = vec![vec![0; p]; p]; for i in 0..p { a[i][i] = 1; } let mut res = Matrix(a); let mut x = self.clone(); while n > 0 { if n & 1 == 1 { res *= x.clone(); } x *= x.clone(); n >>= 1; } res } } impl Mul for Matrix { type Output = Matrix; fn mul(self, other: Matrix) -> Matrix { let p = self.0.len(); let q = other.0[0].len(); let r = other.0.len(); let mut a = vec![vec![0; q]; p]; for i in 0..p { for j in 0..q { for k in 0..r { a[i][j] += self.0[i][k] * other.0[k][j] % Matrix::MOD; a[i][j] %= Matrix::MOD; } } } Matrix(a) } } impl MulAssign for Matrix { fn mul_assign(&mut self, rhs: Matrix) { *self = (*self).clone() * rhs; } } fn main() { const MOD: usize = 1_000_000_007; let (k, m, n): (usize, usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) }; let mut a = Matrix(vec![vec![0; k * k]; k * k]); for _ in 0..m { let (p, q, r): (usize, usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, ) }; a.0[p * k + q][q * k + r] = 1; } let res = a.pow(n - 2); let mut ans = 0; for i in 0..k { for j in 0..k { ans = (ans + res.0[i][j * k]) % MOD; } } println!("{}", ans); }