local mod = 1000000007 local mfl = math.floor local function bmul(x, y) if x * y <= 1000000000000000 then return (x * y) % mod end local x1, y1 = mfl(x / 31623), mfl(y / 31623) local x0, y0 = x - x1 * 31623, y - y1 * 31623 return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod end local function badd(x, y) return (x + y) % mod end local function bsub(x, y) return x < y and x - y + mod or x - y end local function modpow(src, pow) local res = 1 while 0 < pow do if pow % 2 == 1 then res = bmul(res, src) pow = pow - 1 end src = bmul(src, src) pow = mfl(pow / 2) end return res end local n, m, x = io.read("*n", "*n", "*n") local ea, eb, ec = {}, {}, {} for i = 1, m do ea[i], eb[i], ec[i] = io.read("*n", "*n", "*n") end local edge = {} for i = 1, n do edge[i] = {} end local parent = {} for i = 1, n do parent[i] = i end local function uf_findroot(idx) local idx_update = idx while parent[idx] ~= idx do idx = parent[idx] end while parent[idx_update] ~= idx do parent[idx_update], idx_update = idx, parent[idx_update] end return idx end local used = 0 for i = 1, m do local i1, i2, c = ea[i], eb[i], ec[i] local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent) parent[i1], parent[i2] = r1, r2 if r1 ~= r2 then parent[r2] = r1 parent[i2] = r1 used = used + 1 table.insert(edge[i1], i) table.insert(edge[i2], i) if used == n - 1 then break end end end local size = {} for i = 1, n do parent[i] = 0 size[i] = 1 end local tasks = {1} for i = 1, n do local src = tasks[i] for j = 1, #edge[src] do local ei = edge[src][j] local dst = ea[ei] == src and eb[ei] or ea[ei] if parent[src] ~= dst then parent[dst] = src table.insert(tasks, dst) end end end local ans = 0 for i = n, 1, -1 do local src = tasks[i] for j = 1, #edge[src] do local ei = edge[src][j] local dst = ea[ei] == src and eb[ei] or ea[ei] if parent[src] ~= dst then local csize = size[dst] ans = badd(ans, bmul(bmul(csize, n - csize), modpow(x, ec[ei]))) size[src] = size[src] + csize end end end print(ans)