import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines k,m,n = map(int,readline().split()) pqr = list(map(int,read().split())) mod = 10**9+7 mat = [[0] * 36 for _ in range(36)] it = iter(pqr) for p,q,r in zip(it,it,it): p,q,r = p-1,q-1,r-1 mat[(p*6+q)][(q*6+r)] = 1 def dot_mod(a,b): res = [[0] * 36 for _ in range(36)] for i in range(36): for j in range(36): for k in range(36): res[i][j] += a[i][k] * b[k][j] res[i][j] %= mod return res mat_ex = [mat] for i in range(65): mat_ex.append( dot_mod(mat_ex[-1],mat_ex[-1]) ) mat_n = [[0] * 36 for _ in range(36)] for i in range(36): mat_n[i][i] = 1 x = n-2 for i in range(65): if((x>>i)&1): mat_n = dot_mod(mat_n,mat_ex[i]) ans = 0 for i in range(6): for j in range(6): ans += mat_n[i][6*j] ans %= mod print(ans)