You are given a tree (a connected undirected graph without cycles) of n vertices. Each of the n - 1 edges of the tree is colored in either black or red.

You are also given an integer k. Consider sequences of k vertices. Let's call a sequence [a_1, a_2, …, a_k] good if it satisfies the following criterion:

  * We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a_1 and ending at a_k. 
  * Start at a_1, then go to a_2 using the shortest path between a_1 and a_2, then go to a_3 in a similar way, and so on, until you travel the shortest path between a_{k-1} and a_k.
  * If you walked over at least one black edge during this process, then the sequence is good. 

<image>

Consider the tree on the picture. If k=3 then the following sequences are good: [1, 4, 7], [5, 5, 3] and [2, 3, 7]. The following sequences are not good: [1, 4, 6], [5, 5, 5], [3, 7, 3].

There are n^k sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 10^9+7.

Input

The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100), the size of the tree and the length of the vertex sequence.

Each of the next n - 1 lines contains three integers u_i, v_i and x_i (1 ≤ u_i, v_i ≤ n, x_i ∈ \{0, 1\}), where u_i and v_i denote the endpoints of the corresponding edge and x_i is the color of this edge (0 denotes red edge and 1 denotes black edge).

Output

Print the number of good sequences modulo 10^9 + 7.

Examples

Input


4 4
1 2 1
2 3 1
3 4 1


Output


252

Input


4 6
1 2 0
1 3 0
1 4 0


Output


0

Input


3 5
1 2 1
2 3 0


Output


210

Note

In the first example, all sequences (4^4) of length 4 except the following are good: 

  * [1, 1, 1, 1]
  * [2, 2, 2, 2]
  * [3, 3, 3, 3]
  * [4, 4, 4, 4] 



In the second example, all edges are red, hence there aren't any good sequences.
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class GFG {
    static class edge{
        int s,d,w;
        public edge(int s,int d,int w)
        {
            this.s=s;
            this.d=d;
            this.w=w;
        }
    }
    
    static class graph{
        int v;
        LinkedList<edge> li[];
        boolean visit[];
        boolean visit1[];
        int arr[];
        public graph(int v)
        {
            this.v=v;
            visit=new boolean[v];
            visit1=new boolean[v];
            arr=new int[v];
            li=new LinkedList[v];
            for(int i=0;i<v;i++)
            li[i]=new  LinkedList<>();
        }
        public void addedge(int x,int y,int w)
        {
            edge e=new edge(x,y,w);
            li[x].add(e);
            edge e1=new edge(y,x,w);
            li[y].add(e1);
        }
        public void removeedge(int x,int y,int w)
        {
            Iterator<edge> it =li[x].iterator();
        while (it.hasNext()) {
            if (it.next().d == y) {
                it.remove();
                break;
            }
        }
           Iterator<edge> it1 =li[y].iterator();
        while (it1.hasNext()) {
            if (it1.next().d == x) {
                it1.remove();
                return;
            }
        }
        }
        public boolean hasEdge(int i, int j) {
        return li[i].contains(j);
    }
    public int con(int s)
    {
        return li[s].size();
    }
      public HashSet<Integer> get(int s)
    {
        HashSet<Integer> h=new HashSet<Integer>();
             Iterator<edge> i=li[s].listIterator();
        while(i.hasNext()){
        h.add(i.next().d);}
        return h;
    }
  
        public int bfs(int s,boolean d[])
        {
            LinkedList<Integer> q=new LinkedList<Integer>();
            d[s]=true;
            q.add(s);
            int c=0;
            while(q.size()!=0){
             s=q.poll();c++;
             Iterator<edge> i=li[s].listIterator();
             while(i.hasNext()){int n=i.next().d;
             if(!d[n]){
             q.add(n);d[n]=true;}
             }
            }
             return c;
        }
    }
     static long power(long x,long y,long p) 
    { 
        // Initialize result 
        long res = 1;      
         
        // Update x if it is more   
        // than or equal to p 
        x = x % p;  
      
        while (y > 0) 
        { 
            // If y is odd, multiply x 
            // with result 
            if((y & 1)==1) 
                res = (res * x) % p; 
      
            // y must be even now 
            // y = y / 2 
            y = y >> 1;  
            x = (x * x) % p;  
        } 
        return res; 
    } 
  
    
        public static void main (String[] args)throws IOException {
		Reader sc=new Reader();
		int n=sc.nextInt();
		//int m=sc.nextInt();
		int k=sc.nextInt();
		graph g=new graph(n);
		for(int i=0;i<n-1;i++)
		{
		    int x=sc.nextInt();
		    int y=sc.nextInt();
		    int w=sc.nextInt();
		    if(w==0)
		    g.addedge(x-1,y-1,w);
		}
		ArrayList<Integer> arr=new ArrayList<Integer>();
		boolean d[]=new boolean[n];
		for(int i=0;i<n;i++)
		{
		    if(!d[i])
	        arr.add(g.bfs(i,d));
		}
	    long m=(long)Math.pow(10,9)+7;
	    long ans=power(n,k,m);
	    for(int i=0;i<arr.size();i++)
	    ans=ans-power(arr.get(i),k,m);
	    System.out.println(ans);
        }
		 static class Reader 
    { 
        final private int BUFFER_SIZE = 1 << 16; 
        private DataInputStream din; 
        private byte[] buffer; 
        private int bufferPointer, bytesRead; 
  
        public Reader() 
        { 
            din = new DataInputStream(System.in); 
            buffer = new byte[BUFFER_SIZE]; 
            bufferPointer = bytesRead = 0; 
        } 
  
        public Reader(String file_name) throws IOException 
        { 
            din = new DataInputStream(new FileInputStream(file_name)); 
            buffer = new byte[BUFFER_SIZE]; 
            bufferPointer = bytesRead = 0; 
        } 
  
        public String readLine() throws IOException 
        { 
            byte[] buf = new byte[64]; // line length 
            int cnt = 0, c; 
            while ((c = read()) != -1) 
            { 
                if (c == '\n') 
                    break; 
                buf[cnt++] = (byte) c; 
            } 
            return new String(buf, 0, cnt); 
        } 
  
        public int nextInt() throws IOException 
        { 
            int ret = 0; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
            do
            { 
                ret = ret * 10 + c - '0'; 
            }  while ((c = read()) >= '0' && c <= '9'); 
  
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        public long nextLong() throws IOException 
        { 
            long ret = 0; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
            do { 
                ret = ret * 10 + c - '0'; 
            } 
            while ((c = read()) >= '0' && c <= '9'); 
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        public double nextDouble() throws IOException 
        { 
            double ret = 0, div = 1; 
            byte c = read(); 
            while (c <= ' ') 
                c = read(); 
            boolean neg = (c == '-'); 
            if (neg) 
                c = read(); 
  
            do { 
                ret = ret * 10 + c - '0'; 
            } 
            while ((c = read()) >= '0' && c <= '9'); 
  
            if (c == '.') 
            { 
                while ((c = read()) >= '0' && c <= '9') 
                { 
                    ret += (c - '0') / (div *= 10); 
                } 
            } 
  
            if (neg) 
                return -ret; 
            return ret; 
        } 
  
        private void fillBuffer() throws IOException 
        { 
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
            if (bytesRead == -1) 
                buffer[0] = -1; 
        } 
  
        private byte read() throws IOException 
        { 
            if (bufferPointer == bytesRead) 
                fillBuffer(); 
            return buffer[bufferPointer++]; 
        } 
  
        public void close() throws IOException 
        { 
            if (din == null) 
                return; 
            din.close(); 
        } 
    } 
  
}