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 ) ) ,*) } ;} fn main() { let (n, c) = read!(usize, usize); let mut to = vec![vec![]; n]; let (d, s) = { let mut root = Rerooting::::new(n); for _ in 0..n - 1 { let (u, v) = read!(usize1, usize1); to[u].push(v); to[v].push(u); root.add_edge(u, v); } root.init(); let mut dm = n; let mut s = 0; for i in 0..n { if dm > root.ans[i] { dm = root.ans[i]; s = i; } } (dm - 1, s) }; let cm = std::cmp::min(c, 6 * d + 1); let mut dp = vec![vec![Mint(1); cm + 1]; n]; let mut visit = vec![vec![false; cm + 1]; n]; let mut ans = Mint(0); for i in 1..=cm { ans += dfs(s, s, &to, i, cm, &mut dp, &mut visit); } if cm != c { ans += dp[s][3 * d + 1] * (c - (6 * d + 1)) as i64; } println!("{}", ans); } struct D; impl DP for D { type T = usize; fn add_root(a: &Self::T) -> Self::T { a + 1 } fn id() -> Self::T { 0 } fn concat(l: &Self::T, r: &Self::T) -> Self::T { std::cmp::max(*l, *r) } } trait DP { type T: Clone; fn add_root(a: &Self::T) -> Self::T; fn id() -> Self::T; fn concat(l: &Self::T, r: &Self::T) -> Self::T; } struct Rerooting { to: Vec>, dp: Vec>, ans: Vec, } impl Rerooting { fn new(n: usize) -> Self { Rerooting { to: vec![Vec::::new(); n], dp: vec![Vec::::new(); n], ans: vec![D::id(); n], } } fn add_edge(&mut self, u: usize, v: usize) { self.to[u].push(v); self.to[v].push(u); } fn init(&mut self) { self.dfs(0, None); self.bfs(0, D::id(), None); } fn dfs(&mut self, v: usize, p: Option) -> D::T { let mut dp_sum = D::id(); let deg = self.to[v].len(); self.dp[v] = vec![D::id(); deg]; for i in 0..deg { let u = self.to[v][i]; if Some(u) == p { continue; } self.dp[v][i] = self.dfs(u, Some(v)); dp_sum = D::concat(&dp_sum, &self.dp[v][i]); } D::add_root(&dp_sum) } fn bfs(&mut self, v: usize, dp_p: D::T, p: Option) { if let Some(p) = p { let x = self.to[v].iter().enumerate().find(|&(_, v)| *v == p); if let Some((i, _)) = x { self.dp[v][i] = dp_p; } } let deg = self.to[v].len(); let mut dp_sum_l = vec![D::id(); deg + 1]; for i in 0..deg { dp_sum_l[i + 1] = D::concat(&dp_sum_l[i], &self.dp[v][i]); } let mut dp_sum_r = vec![D::id(); deg + 1]; for i in (0..deg).rev() { dp_sum_r[i] = D::concat(&dp_sum_r[i + 1], &self.dp[v][i]); } self.ans[v] = D::add_root(&dp_sum_l[deg]); for i in 0..deg { let u = self.to[v][i]; if Some(u) == p { continue; } self.bfs( u, D::add_root(&D::concat(&dp_sum_l[i], &dp_sum_r[i + 1])), Some(v), ); } } } fn dfs( u: usize, p: usize, to: &Vec>, s: usize, c: usize, dp: &mut Vec>, visit: &mut Vec>, ) -> Mint { if !visit[u][s] { visit[u][s] = true; let mut x = Mint(1); for &v in to[u].iter() { if v == p { continue; } let mut f = Mint(0); if s > 3 { f += dfs(v, u, to, s - 3, c, dp, visit) } if s + 3 <= c { f += dfs(v, u, to, s + 3, c, dp, visit); } x *= f; } dp[u][s] = x; } dp[u][s] } const MOD: i64 = 1000000007; #[derive(Copy, Clone, Debug)] 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::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::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::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::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 } }