"use strict";
(function(input){
  input=input.trim();
  input=input.split(/\n+/).map(str=>str.split(/\s+/).map(x=>x-0));
  const N=input[0][0],K=input[0][1],P=1e9+7;
  function Node(index){
    let t=this;
    t.id=index;
    t.neighbor={};
    t.visitedNeighbor={};
  }
  const nodes=["zero",];
  for(let i=1;i<=N;i++){
    nodes[i]=new Node(i);
  }
  for(let i=1;i<=N;i++){
    let u=input[i][0],v=input[i][1];
    nodes[u].neighbor[v]=nodes[v];
    nodes[v].neighbor[u]=nodes[u];
  }
  let ans=K;
  let stack=[nodes[1]];
  nodes[1].counted=true;
  for(let n in nodes[1].neighbor){
    n.visitedNeighbor[1]=nodes[1];
  }
  while(stack[0]){
    let t=stack[stack.length-1];
    if(!t.counted){
      t.counted=true;
      let k=0;
      for(let n in t.visitedNeighbor){
        k+=n.visitedNeighbor.keys.length;
      }
      ans=(ans*(K-k-t.visitedNeighbor.keys.length))%P;
      for(let n in t.neighbor){
        n.visitedNeighbor[t.id]=t;
      }
    }
    if(t.neighbor.keys.length===t.visitedNeighbor.keys.length){
      stack.pop();
    }else{
      for(let n in t.neighbor){
        if(!t.visitedNeighbor[n.id]){
          stack.push(t);
          break;
        }
      }
    }
  }
  console.log(ans);
})(require("fs").readFileSync("/dev/stdin","utf8"));
