#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; const ll MOD = 1000000007; class ModInt{ public: ll v; ModInt(ll _v = 0){ if(_v >= MOD) _v %= MOD; v = _v; } ModInt operator+(ll n){ return ModInt((v+n)%MOD); } ModInt operator-(ll n){ return ModInt((v-n+MOD)%MOD); } ModInt operator*(ll n){ if(n >= MOD) n %= MOD; return ModInt((v*n)%MOD); } ModInt operator/(ll n){ return ModInt((ModInt(n).inv()*v).v%MOD); } void operator+=(ll n){ v = (v+n)%MOD; } void operator-=(ll n){ v = (v-n+MOD)%MOD; } void operator*=(ll n){ v = (v*n+MOD)%MOD; } ModInt operator+(ModInt n){ return ModInt((v+n.v)%MOD); } ModInt operator-(ModInt n){ return ModInt((v-n.v+MOD)%MOD); } ModInt operator*(ModInt n){ return ModInt((v*n.v)%MOD); } ModInt operator/(ModInt n){ return ModInt((n.inv()*v).v%MOD); } void operator+=(ModInt n){ v = (v+n.v)%MOD; } void operator-=(ModInt n){ v = (v-n.v+MOD)%MOD; } void operator*=(ModInt n){ v = (v*n.v)%MOD; } void operator=(ModInt n){ v = n.v; } bool operator==(ModInt n){ return v == n.v; } bool operator!=(ModInt n){ return v != n.v; } void operator=(ll n){ v = n%MOD; } ModInt inv(){ if(v == 1) return ModInt(1); else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD); } }; ostream& operator<<(ostream& os, const ModInt& m){ os << m.v; return os; } istream & operator >> (istream &in, ModInt &m){ in >> m.v; return in; } ModInt pow(ModInt a, ll n) { assert(n >= 0); ModInt ans = 1; ModInt tmp = a; for (int i = 0; i <= 60; i++) { ll m = (ll)1 << i; if (m & n) { ans *= tmp; } tmp *= tmp; } return ans; } struct edge{ int to; ll cost; }; using mint = ModInt; vector g[100000]; ll a[100000], b[100000]; bool used[100000]; bool ng; vector visited; set cand; void dfs(int v){ visited.push_back(v); used[v] = true; for(edge e : g[v]){ if(used[e.to]) { if(a[v] == a[e.to]){ // 2*a[v]*x+b[v]+b[e.to] = e.cost if((e.cost-(b[v]+b[e.to]))%2 != 0) ng = true; else{ cand.insert((e.cost-b[v]-b[e.to])/(2*a[v])); } }else{ if(b[v]+b[e.to] != e.cost) ng = true; } }else{ a[e.to] = -a[v]; b[e.to] = e.cost-b[v]; dfs(e.to); } } } const ll INF = 1e15; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, m, k; cin >> n >> m >> k; for(int i = 0; i < m; i++){ int x, y; ll z; cin >> x >> y >> z; x--; y--; g[x].push_back(edge{y, z}); g[y].push_back(edge{x, z}); } auto clear = [&](){ visited.clear(); ng = false; }; auto f = [&](ll x, int v){ return a[v]*x+b[v]; }; mint n_all = 1, n_ng = 1; for(int i = 0; i < n; i++){ if(used[i]) continue; clear(); a[i] = 1; b[i] = 0; dfs(i); if(ng || cand.size() >= 2){ cout << 0 << endl; return 0; }else{ if(cand.empty()){ ll x_min = -INF, x_max = INF; for(int v : visited){ ll s = a[v]*(1-b[v]); ll t = a[v]*(k-b[v]); chmax(x_min, min(s, t)); chmin(x_max, max(s, t)); } set just; // cout << x_min << ' ' << x_max << endl; if(x_max < x_min){ cout << 0 << endl; return -1; } for(int v : visited){ ll x = (k-b[v])/a[v]; if(x_min <= x && x <= x_max) just.insert(x); } n_all *= x_max-x_min+1; n_ng *= x_max-x_min+1-just.size(); }else{ ll x = *cand.begin(); for(int v : visited){ if(f(x, v) < 1 || f(x, v) > k){ cout << 0 << endl; return 0; } if(f(x, v) == k) n_ng = 0; } } } } cout << n_all-n_ng << endl; }