import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") # グラフの読み込み・重みあり n,m,k = map(int, input().split()) ns = [[] for _ in range(n)] for _ in range(m): u,v,c = map(int, input().split()) u -= 1 v -= 1 ns[u].append((c,v)) ns[v].append((c,u)) def sub(k): ans = 1 seen = [0]*n a = [0]*n b = [0]*n M = 10**9+7 for u in range(n): if seen[u]: continue res = _sub(u,seen,a,b,k) if res==0: return 0 ans *= res ans %= M return ans def _sub(u,seen,a,b,k): seen[u] = 1 a[u] = 1 b[u] = 0 val = None q = [u] lb = 1 ub = k while q: u = q.pop() aa,bb = a[u],b[u] if aa==1: lb = max(lb, 1-bb) ub = min(ub, k-bb) else: lb = max(lb, bb-k) ub = min(ub, bb-1) for c,v in ns[u]: na = -aa nb = -bb+c if not seen[v]: a[v] = na b[v] = nb seen[v] = 1 q.append(v) else: va,vb = a[v],b[v] if va==na: if vb!=nb: return 0 else: if (nb-vb) % (va-na) != 0: return 0 nval = (nb-vb) // (va-na) if val is not None and nval!=val: return 0 val = nval if not 1<=val<=k: return 0 if val is not None: if all(aa*val+bb<=k for aa,bb in zip(a,b)): return 1 return 0 else: return max(0, ub-lb+1) ans = sub(k) - sub(k-1) print(ans%M)