use std::vec::Vec;
use std::cmp::*;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

const MOD: usize = 1_000_000_000 + 7;

fn main() {
    let (n, k) = {
      let v: Vec<usize> = read_vec();
      (v[0], v[1])
    };

    let mut g = vec![vec![]; n];
    for _ in 0..n-1 {
      let v: Vec<usize> = read_vec();
      g[v[0] - 1].push(v[1] - 1);
      g[v[1] - 1].push(v[0] - 1);
    }
    println!("{}", dfs(n, &g, k).get_val());

}



//単一視点から目的のノードまでの最短距離、ただし全ての辺の重みが等しいグラフに限る
fn dfs(n: usize, g: &Vec<Vec<usize>>, k: usize) -> Mod {
  let mut visit = vec![false; n];
  let mut stack = Vec::new();
  let mut res = Mod::new_one();

  // first
  res = res * Mod::new(k);
  visit[0] = true;

  // second
  let &l = &g[0].len();
  if  l > 0 {
    res = res * Mod::new(k-1).perm(Mod::new(l));

    for &e in &g[0] {
      if !visit[e] {
        visit[e] = true;
        stack.push(e);
      }
    }
  }

  // third
  while !stack.is_empty() {
    let now
     = match stack.pop() {
        None => 0,
        Some(e) => e,
     };
     let &l = &g[now].len();
     res = res * Mod::new(k-2).perm(Mod::new(l - 1));

    for &e in &g[now] {
      if !visit[e] {
          visit[e] = true;
          stack.push(e);
      }
    }
  }
   res
}


use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Div;


#[derive(Debug, Clone, Copy, Eq, PartialEq)]
struct Mod {
  val: usize,
}


// 和を定義
impl Add for Mod {
  type Output = Mod;
  // O(1)
  fn add(self, other: Mod) -> Mod {
    Mod { val: (self.val + other.val) % (1_000_000_000 + 7) }
  }
}

// 差を定義
impl Sub for Mod {
  type Output = Mod;

  // O(1)
  fn sub(self, other: Mod) -> Mod {
    let mx = max(self.val, other.val);
    let mn = min(self.val, other.val);

    Mod { val: (mx - mn) % (1_000_000_000 + 7) }
  }
}

// 積を定義
impl Mul for Mod {
  type Output = Mod;

  // O(1)
  fn mul(self, other: Mod) -> Mod {
    Mod { val: (self.val * other.val) % (1_000_000_000 + 7) }
  }
}

// 加法単位元
impl Neg for Mod {
  type Output = Mod;

  // O(1)
  fn neg(self) -> Mod {
    Mod { val: (1_000_000_000 + 7) - self.val }
  }
}

// 逆元を定義
// フェルマーの小定理により実装
impl Div for Mod {
  type Output = Mod;

  //O(logN)
  fn div(self, other: Mod) -> Mod {
    self * other.inv()
  }
}

impl Mod {
  // コンストラクター
  fn new(n: usize) -> Mod {
    Mod { val: n % (1_000_000_000 + 7) }
  }

  // 加法単位元
  fn new_zero() -> Mod {
    Mod { val: 0 }
  }

  // 乗法単位元
  fn new_one() -> Mod {
    Mod { val: 1 }
  }

  // 加法逆元
  // 上記にて定義



  // 乗法逆元
  fn inv(self) -> Mod {
    if self.val == 0 { panic!("zero divide"); }
    self.pow(1_000_000_000 + 5)
  }

  // 累乗を定義
  // O(logN)
  fn pow(self, n: usize) -> Mod {
    let mut res = Mod::new(1);
    let mut a = self;
    let mut n = n;

    while n > 0 {
      if n % 2 == 0 {
        a = a * a;
        n /= 2;
      } else {
        res = res * a;
        a = a * a;
        n /= 2;
      }
    }
    res
  }

  // 最大公約数
  // O(logN)
  fn gcd(self, other: Mod) -> usize {
      let mut x = self.val;
      let mut y = other.val;
      while y != 0 {
          let t = y;
          y = x % y;
          x = t;
      }
      x
  }

  // 階乗
  // O(N)
  fn fact(self) -> Mod {
    let mut res = Mod::new_one();
    for i in 1..self.val+1 {
      res = res * Mod::new(i)
    }

    res
  }

  // 順列
  fn perm(self, other: Mod) -> Mod {
    let mut res = Mod::new_one();

    if self.val < other.val {
      return Mod::new_zero();
    }
    for x in (self.val - other.val + 1)..self.val+1 {
      res = res * Mod::new(x);
    }

    res
  }

  // 組み合わせ
  fn conb(self, other: Mod) -> Mod {
    self.fact() / (other.fact() * (self - other).fact())
  }

  fn get_val(self) -> usize {
  self.val
  }
}


Measured memory_bytes: 12540