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.io.*;
import java.math.BigInteger;
import java.util.*;


public class GFG770 {
    static int mod1 = (int) (1e9 + 7);
    static LinkedList<Integer>[] linkedLists;
    static int count1=0;
    static int[] visited;
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        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 nextString() throws IOException {
            String str00 = scan.next();
            return str00;
        }

        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);
        }

        String next() throws IOException {
            int c;
            for (c = read(); c <= 32; c = read());
            StringBuilder sb = new StringBuilder();
            for (; c > 32; c = read()) {
                sb.append((char) c);
            }
            return sb.toString();
        }

        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();
        }

        public int[] nextArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }
    }


    static int GCD(int a, int b) {
        BigInteger big1 = new BigInteger(String.valueOf(a));
        BigInteger big2 = new BigInteger(String.valueOf(b));
        big1 = big1.gcd(big2);
        return Integer.parseInt(big1.toString());
    }


    static long power(long x, long y, long p)
    {
        long res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
        // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y%2!=0)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }


    static boolean primeCheck(long num0) {
        boolean b1 = true;
        if (num0 == 1) {
            b1 = false;
        } else {
            int num01 = (int) (Math.sqrt(num0)) + 1;
            me1:
            for (int i = 2; i < num01; i++) {
                if (num0 % i == 0) {
                    b1 = false;
                    break me1;
                }
            }
        }
        return b1;
    }

    public static int dev(long num1)
    {
        int count00=0;
        while (num1%2==0)
        {
            count00++;
            num1/=2;
        }
        HashMap<Long,Long> hashMap=new HashMap<>();
        if(count00!=0)
        {
            hashMap.put(2L,(long)count00);
        }
        for (int i = 3; i <= (int)Math.sqrt(num1); i = i + 2)
        {
            // While i divides n, print i and divide n
            if(num1%i==0) {
                int count01 = 0;

                while (num1 % i == 0) {
                    num1 /= i;
                    count01++;
                }
                hashMap.put((long)i,(long)count01);
            }
        }
        if(num1>2)
        {
            hashMap.put((long)num1,1L);
        }
        long numOfDiv=1;
        for(long num00:hashMap.keySet())
        {
            long cDiv0=hashMap.get(num00);
            numOfDiv*=(cDiv0+1);
        }

        return (int)(numOfDiv);
    }

    static void dfs(int index)
    {
        visited[index]=1;
        count1++;

        for(int num00:linkedLists[index])
        {
            if(visited[num00]==0)
            {
                dfs(num00);
            }
        }
    }


    public static void main(String[] args) throws IOException {
        //Reader r = new Reader();
        //PrintWriter writer=new PrintWriter(System.out);
        Scanner r = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        //OutputWriter770 out77 = new OutputWriter770(System.out);

        int len1=r.nextInt();
        int k=r.nextInt();
        visited=new int[len1+1];
        linkedLists=new LinkedList[len1+1];
        for(int i=0;i<len1+1;i++)
        {
            linkedLists[i]=new LinkedList<>();
        }
        for(int i=0;i<len1-1;i++)
        {
            int U=r.nextInt();
            int V=r.nextInt();
            int st=r.nextInt();

            if(st==0)
            {
                linkedLists[U].addLast(V);
                linkedLists[V].addLast(U);
            }
        }

        long res1=0;

        for(int i=1;i<len1+1;i++)
        {
            if(visited[i]==0)
            {
                count1=0;
                dfs(i);
                long res2=power(count1,k,mod1);
                res1+=res2;
                res1%=mod1;
            }
        }
        res1=power(len1,k,mod1)-res1;
        res1+=mod1;
        res1%=mod1;
        System.out.print(res1+"");
        r.close();
    }
}

class OutputWriter770
{
    BufferedWriter writer;

    public OutputWriter770(OutputStream stream)
    {
        writer = new BufferedWriter(new OutputStreamWriter(stream));
    }
    public void print(int i) throws IOException
    {
        writer.write(i + "");
    }

    public void println(int i) throws IOException
    {
        writer.write(i + "\n");
    }

    public void print(String s) throws IOException
    {
        writer.write(s + "");
    }

    public void println(String s) throws IOException
    {
        writer.write(s + "\n");
    }

    public void print(char[] c) throws IOException
    {
        writer.write(c);
    }

    public void close() throws IOException
    {
        writer.flush();
        writer.close();
    }
}

class NodeInfo
{
    int i,j;
    NodeInfo(int i,int j)
    {
        this.i=i;
        this.j=j;
    }
}
