import numpy as np import sys def input(): return sys.stdin.readline()[:-1] MOD = 10**9+7 def mat_pow(mat, exp): size = len(mat) res = np.eye(size, dtype=np.object) cnt = 0 while (1<>cnt)&1: res = res @ mat res %= MOD mat = mat @ mat mat %= MOD cnt += 1 return res k, m, n = map(int, input().split()) pre = np.zeros((k*k, 1), dtype=np.object) for i in range(k): pre[i][0] = 1 mat = np.zeros((k*k, k*k), dtype=np.object) for _ in range(m): p, q, r = map(int, input().split()) mat[(q-1)*k + r-1][(p-1)*k + q-1] += 1 res = mat_pow(mat, n-2) @ pre ans = 0 for i in range(k): ans += res[i*k][0] ans %= MOD print(ans)