/* -*- coding: utf-8 -*- * * 1502.cc: No.1502 Many Simple Additions - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MOD = 1000000007; /* typedef */ typedef long long ll; typedef queue qi; typedef pair pii; typedef vector vpii; /* global variables */ vpii nbrs[MAX_N]; int ps[MAX_N], as[MAX_N]; ll bs[MAX_N]; /* subroutines */ /* main */ int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); x--, y--; nbrs[x].push_back(pii(y, z)); nbrs[y].push_back(pii(x, z)); } int p0 = 1, p1 = 1; for (int st = 0; st < n; st++) if (as[st] == 0) { ps[st] = -1; as[st] = 1, bs[st] = 0; int minx = 1, maxx = k; bool minf = false, maxf = true; qi q; q.push(st); while (! q.empty()) { int u = q.front(); q.pop(); for (auto pe: nbrs[u]) { int v = pe.first; ll z = pe.second; if (as[v] == 0) { ps[v] = u; // (au*x+bu)+(av*x+bv)=z -> av*x+bv=-au*x+(z-bu) as[v] = -as[u], bs[v] = z - bs[u]; // 1<=av*x+bv<=k -> 1-bv<=av*x<=k-bv if (as[v] > 0) { // av == 1 -> 1-bv<=x<=k-bv ll x0 = 1 - bs[v], x1 = k - bs[v]; if (maxx < x0 || x1 < minx) { puts("0"); return 0; } if (minx < x0) minx = x0, minf = false; if (maxx > x1) maxx = x1, maxf = true; } else { // av == -1 -> bv-1>=x>=bv-k ll x0 = bs[v] - k, x1 = bs[v] - 1; if (maxx < x0 || x1 < minx) { puts("0"); return 0; } if (minx < x0) minx = x0, minf = true; if (maxx > x1) maxx = x1, maxf = false; } q.push(v); } else if (v != ps[u]) { int sa = as[u] + as[v]; ll sb = bs[u] + bs[v]; if (sa == 0) { if (sb != z) { puts("0"); return 0; } } else { if ((z - sb) & 1) { puts("0"); return 0; } ll x = (z - sb) / sa; bool f = (as[u] * x + bs[u] == k) || (as[v] * x + bs[v] == k); if (maxx < x || x < minx) { puts("0"); return 0; } if (minx < x) minx = x, minf = f; if (maxx > x) maxx = x, maxf = f; } } } } //printf("minx=%d,minf=%d maxx=%d,maxf=%d\n", minx, minf, maxx, maxf); int r1 = maxx - minx + 1; int r0 = r1 - (minf ? 1 : 0) - (maxf ? 1 : 0); p0 = (ll)p0 * r0 % MOD; p1 = (ll)p1 * r1 % MOD; } printf("%d\n", (p1 + MOD - p0) % MOD); return 0; }