import numpy as np k, m, n = map(int, input().split()) P = [tuple(map(int, input().split())) for i in range(m)] X = np.array([[0] * m for i in range(m)]) A = np.array([[0] for i in range(m)]) end = [] for i,(p,q,r) in enumerate(P): for j in range(m): if P[j][0] == q and P[j][1] == r: X[j][i] += 1 if p == 1: A[i][0] += 1 if r == 1: end.append(i) mod = 10**9 + 7 n -= 3 res = np.array([[0]*i + [1] + [0]*(m-i-1) for i in range(m)]) while n > 0: if n & 1: res = np.dot(X, res) % mod X = np.dot(X, X) % mod n >>= 1 B = np.dot(res, A) % mod ans = 0 for i in end: ans += B[i][0] ans %= mod print(ans)