pub fn readln() -> String { let mut line = String::new(); ::std::io::stdin() .read_line(&mut line) .unwrap_or_else(|e| panic!("{}", e)); line } macro_rules !read {($($t :tt ) ,*;$n :expr ) =>{{let stdin =::std ::io ::stdin () ;let ret =::std ::io ::BufRead ::lines (stdin .lock () ) .take ($n ) .map (|line |{let line =line .unwrap () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } ) .collect ::>() ;ret } } ;($($t :tt ) ,*) =>{{let line =readln () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } } ;} macro_rules !_read {($it :ident ;[char ] ) =>{_read !($it ;String ) .chars () .collect ::>() } ;($it :ident ;[u8 ] ) =>{Vec ::from (_read !($it ;String ) .into_bytes () ) } ;($it :ident ;usize1 ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 } ;($it :ident ;[usize1 ] ) =>{$it .map (|s |s .parse ::() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 ) .collect ::>() } ;($it :ident ;[$t :ty ] ) =>{$it .map (|s |s .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) ) .collect ::>() } ;($it :ident ;$t :ty ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) } ;($it :ident ;$($t :tt ) ,+) =>{($(_read !($it ;$t ) ) ,*) } ;} macro_rules! mint { ($a :expr ) => { Mint::new({ $a }) }; } const MOD: i64 = 1000000007; #[derive(Copy, Clone)] pub struct Mint(i64); impl Mint { fn new(x: i64) -> Self { Mint(x.rem_euclid(MOD)) } fn pow(self, n: usize) -> Self { match n { 0 => Mint::new(1), _ => { let mut a = self.pow(n >> 1); a *= a; if n & 1 == 1 { a *= self; } a } } } fn inv(self) -> Self { self.pow((MOD - 2) as usize) } } impl std::ops::Neg for Mint { type Output = Mint; fn neg(self) -> Self::Output { Self::new(-self.0) } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; self.0 %= MOD; } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, rhs: i64) { *self += Mint::new(rhs); } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, rhs: usize) { *self += Mint::new(rhs as i64); } } impl std::ops::Add for Mint where Mint: std::ops::AddAssign, { type Output = Self; fn add(self, other: T) -> Self { let mut res = self; res += other; res } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; if self.0 < 0 { self.0 += MOD; } } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, rhs: i64) { *self -= Mint::new(rhs); } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, rhs: usize) { *self -= Mint::new(rhs as i64); } } impl std::ops::Sub for Mint where Mint: std::ops::SubAssign, { type Output = Self; fn sub(self, other: T) -> Self { let mut res = self; res -= other; res } } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0; self.0 %= MOD; } } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, rhs: i64) { *self *= Mint::new(rhs); } } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, rhs: usize) { *self *= Mint::new(rhs as i64); } } impl std::ops::Mul for Mint where Mint: std::ops::MulAssign, { type Output = Self; fn mul(self, other: T) -> Self { let mut res = self; res *= other; res } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, rhs: Self) { *self *= rhs.inv(); } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, rhs: i64) { *self /= Mint::new(rhs); } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, rhs: usize) { *self /= Mint::new(rhs as i64); } } impl std::ops::Div for Mint where Mint: std::ops::DivAssign, { type Output = Self; fn div(self, other: T) -> Self { let mut res = self; res /= other; res } } impl std::fmt::Display for Mint { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } impl std::ops::Deref for Mint { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 } } impl std::ops::DerefMut for Mint { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } struct UnionFind { n: usize, par: Vec, rank: Vec, } impl UnionFind { fn new(n: usize) -> Self { UnionFind { n, par: (0..n).collect(), rank: vec![1; n], } } fn unite(&mut self, x: usize, y: usize) { let rx = self.root(x); let ry = self.root(y); if rx == ry { return; } if self.rank[x] > self.rank[y] { self.par[ry] = rx; self.rank[rx] += self.rank[ry]; } else { self.par[rx] = ry; self.rank[ry] += self.rank[rx]; } } fn same(&mut self, x: usize, y: usize) -> bool { self.root(x) == self.root(y) } fn root(&mut self, x: usize) -> usize { if self.par[x] == x { x } else { self.par[x] = self.root(self.par[x]); self.par[x] } } fn size(&mut self, x: usize) -> usize { let r = self.root(x); self.rank[r] } fn sizes(&mut self) -> Vec { let mut s = vec![]; for x in 0..self.n { if x == self.root(x) { s.push(self.rank[x]); } } s } } fn main() { let (n, m, r) = read!(usize, usize, i64); let r = mint!(r); let mut uf = UnionFind::new(n); let mut to = vec![vec![]; n]; let mut i = 0; for (x, y, z) in (0..m).map(|_| read!(usize1, usize1, usize)) { if uf.same(x, y) { continue; } uf.unite(x, y); to[x].push((y, i, z)); to[y].push((x, i, z)); i += 1; } let mut cost = vec![r; i]; dfs(n, 0, None, &to, &mut cost); let mut ans = mint!(0); for c in cost { ans += c; } println!("{}", ans); } fn dfs( n: usize, u: usize, p: Option, to: &Vec>, cost: &mut Vec, ) -> usize { let mut d = 1; for &(v, i, z) in to[u].iter() { if p == Some(v) { continue; } let m = dfs(n, v, Some(u), to, cost); cost[i] = cost[i].pow(z) * ((n - m) * m) as i64; d += m; } d }