import sys input = sys.stdin.buffer.readline def main(): k, m, n = map(int, input().split()) #mod =10**9+7 PQR = [] for i in range(m): p, q, r = map(int, input().split()) p, q, r = p-1, q-1, r-1 PQR.append((p, q, r)) if n == 3: ans = 0 for i in range(m): p1, q1, r1 = PQR[i] if p1 == 0 and r1 == 0: ans += 1 print(ans) exit() A = [[0]*m for _ in range(m)] for i in range(m): p1, q1, r1 = PQR[i] for j in range(m): p2, q2, r2 = PQR[j] if q1 == p2 and r1 == q2: A[i][j] = 1 C = [[0]*m for _ in range(m)] for i in range(m): p1, q1, r1 = PQR[i] if p1 != 0: continue for j in range(m): p2, q2, r2 = PQR[j] if q1 == p2 and r1 == q2: C[i][j] = 1 # A*B def mul(a, b): c = [[0]*len(b[0]) for i in range(len(a))] for i in range(len(a)): for k in range(len(b)): for j in range(len(b[0])): c[i][j] = (c[i][j] + a[i][k]*b[k][j])%mod return c #A**n def pow(a, n): b = [[0]*len(a) for i in range(len(a))] for i in range(len(a)): b[i][i] = 1 while n > 0: if n & 1 == 1: b = mul(a, b) a = mul(a, a) n = n>>1 return b #print(C) B = pow(A, n-4) B = mul(C, B) #print(B) ans = 0 for i in range(m): p1, q1, r1 = PQR[i] if r1 == 0: for j in range(m): ans += B[j][i] #ans %= mod print(ans%mod) if __name__ == '__main__': mod =10**9+7 main()