import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int k = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); long n = Long.parseLong(sa[2]); int max = 62; int k2 = k * k; long[][][] g = new long[max][k2][k2]; for (int i = 0; i < m; i++) { sa = br.readLine().split(" "); int p = Integer.parseInt(sa[0]) - 1; int q = Integer.parseInt(sa[1]) - 1; int r = Integer.parseInt(sa[2]) - 1; int pq = p * k + q; int qr = q * k + r; g[0][pq][qr] = 1; } br.close(); int mod = 1000000007; for (int i = 0; i < max - 1; i++) { // for (int j = 0; j < k2; j++) { // for (int j2 = 0; j2 < k2; j2++) { // System.out.print(g[i][j][j2] + "\t"); // } // System.out.println(); // } // System.out.println(); for (int j = 0; j < k2; j++) { for (int j2 = 0; j2 < k2; j2++) { for (int x = 0; x < k2; x++) { g[i + 1][j][j2] += g[i][j][x] * g[i][x][j2]; g[i + 1][j][j2] %= mod; } } } } long n2 = n - 2; long[] dp = new long[k2]; for (int i = 0; i < k; i++) { dp[i] = 1; } for (int i = 0; i < max - 1; i++) { if ((n2 >> i & 1) == 1) { long[] work = new long[k2]; for (int j = 0; j < k2; j++) { for (int x = 0; x < k2; x++) { work[j] += dp[x] * g[i][x][j]; work[j] %= mod; } } dp = work; } } long ans = 0; for (int i = 0; i < k2; i+=k) { ans += dp[i]; } ans %= mod; System.out.println(ans); } }