//use itertools::Itertools;
use std::cmp;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        k: u64,
        edges: [(usize1,usize1);n-1]
    }

    let mut adj_list = vec![vec![]; n];
    for &(from, to) in &edges {
        adj_list[from].push(to);
        adj_list[to].push(from);
    }

    let mut visited = vec![false; n];
    let mut heap = BinaryHeap::new();
    // rootを0とする 親からの距離,親ノード, 子ノード
    heap.push((0, MAX, 0));

    let mut distMap = HashMap::new();

    // 根はk色で塗ることができる
    let mut ans = k;

    while !heap.is_empty() {
        let (dist, parent, node) = heap.pop().unwrap();
        let mut kids_count = adj_list[node].len();
        if parent != MAX {
            kids_count -= 1;
        }

        ans *= match dist {
            0 => permutation_mod(k - 1, kids_count as u64, LARGE_PRIME),
            _ => permutation_mod(k - 2, kids_count as u64, LARGE_PRIME),
        };
        ans %= LARGE_PRIME;

        for &i in &adj_list[node] {
            if i != parent {
                let dist_count = distMap.entry(dist + 1).or_insert(0);
                *dist_count += 1;
                heap.push((dist + 1, node, i));
            }
        }

        //println!("{} {} {}", node, dist, kids_count);
    }

    println!("{}", ans);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet
fn power_mod(base: u64, exp: u64, modulo: u64) -> u64 {
    if exp == 0 {
        return 1;
    } else if exp & 1 == 0 {
        return (power_mod(base, exp / 2, modulo) % modulo).pow(2) % modulo;
    } else {
        return power_mod(base, exp - 1, modulo) % modulo * base % modulo;
    }
}

fn inverse_mod(element: u64, modulo: u64) -> u64 {
    // フェルマーの小定理からpが素数なら a^(p-1) = 1
    // よってa*a^(p-2) = 1 より a^(p-2)がaの逆元
    power_mod(element, modulo - 2, modulo) % modulo
}

fn combination_mod(m: u64, n: u64, modulo: u64) -> u64 {
    // m C n = m! / ( n! * (m - n)! )
    //       = m*(m-1)*(m-2)*...*(m-n+1)/n!
    //       = m*(m-1)*(m-2)*...*(m-n+1) * (n!)^-1
    let mut numerator = 1;
    for i in 0..n {
        numerator *= m - i;
        numerator %= modulo;
    }

    // n!
    let mut element = 1;
    for j in 1..(n + 1) {
        element *= j;
        element %= modulo;
    }
    numerator * inverse_mod(element, modulo) % modulo
}

fn permutation_mod(m: u64, n: u64, modulo: u64) -> u64 {
    // m P n = m! / (m - n)!
    //       = m*(m-1)*(m-2)*...*(m-n+1)
    let mut numerator = 1;
    for i in 0..n {
        numerator *= m - i;
        numerator %= modulo;
    }

    numerator
}


Measured memory_bytes: 22908