from collections import deque n,m,k = map(int,input().split()) mod = 10**9+7 inf = 10**10 e = [[] for i in range(n)] for i in range(m): x,y,z = map(int,input().split()) x -= 1 y -= 1 e[x].append((y,z)) e[y].append((x,z)) def calc(k): use = [0]*n dis = [-1]*n val = [[-1]*2 for i in range(n)] def bimatch(x,k): bians = None dis[x] = 0 val[x] = [1,0] use[x] = 1 vis = [] q = deque([x]) while q: now = q.popleft() vis.append(now) a,b = val[now] for nex,c in e[now]: if dis[nex] == -1: dis[nex] = dis[now]^1 val[nex] = [-a,c-b] q.append(nex) use[nex] = 1 else: na,nb = val[nex] if a+na == 0: if b+nb != c: return 0 else: if (c-b-nb)%(a+na): return 0 if bians == None: bians = (c-b-nb)//(a+na) else: if bians != (c-b-nb)//(a+na): return 0 if bians != None: for v in vis: a,b = val[v] num = bians*a+b if num < 1 or num > k: return 0 return 1 odd = [inf,-inf] even = [inf,-inf] for v in vis: a,b = val[v] if dis[v]%2: odd[0] = min(odd[0],a+b) odd[1] = max(odd[1],a+b) else: even[0] = min(even[0],a+b) even[1] = max(even[1],a+b) if odd[0] == inf: return k if odd[0] > even[0]: odd,even = even,odd if odd[0] < 1: dif = 1-odd[0] odd = [odd[0]+dif,odd[1]+dif] even = [even[0]-dif,even[1]-dif] if even[1] > k: dif = even[1]-k odd = [odd[0]+dif,odd[1]+dif] even = [even[0]-dif,even[1]-dif] if odd[0] < 1 or odd[1] > k or even[0] < 1 or even[1] > k: return 0 count = min(k-odd[1],even[0]-1)+min(k-even[1],odd[0]-1)+1 return count ans = 1 for i in range(n): if use[i]: continue ans *= bimatch(i,k) ans %= mod return ans print((calc(k)-calc(k-1))%mod)