import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class edgy {

	static class Node {
		int data;
		Node link;

		Node(int d,Node l){
			data=d;
			link=l;
		}
	}

	static class queue {
		protected Node front;
		protected Node rear;
		int size;

		queue(){
			front=null;
			rear=null;
			size=0;
		}

		boolean isEmpty() {
			return rear==null;
		}

		void enqueue(int x) {
			Node nptr=new Node(x,null);
			if(rear==null) {
				front=nptr;
			}
			else {
				rear.link=nptr;
			}
			rear=nptr;
			size++;
		}

		int dequeue() {
			int n=front.data;
			front=front.link;
			if(front==null) {
				rear=null;
			}
			size--;
			return n;
		}

		int see() {
			int n=front.data;
			return n;
		}

	}

	static class gedgy{

		int n;
		ArrayList<ArrayList<Integer>> adj;

		gedgy(int V) {
			n=V;
			adj=new ArrayList<ArrayList<Integer>>();

			for(int i=0;i<n;i++) {
				adj.add(new ArrayList<Integer>());
			}
		}

		void add_edge(gedgy g,int a,int b){

			g.adj.get(a).add(b);
			g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

		}

		long BFS(gedgy g,int s,int[] visit) {
			queue q=new queue();

			//int inf=-1;
			q.enqueue(s);
			visit[s]=1;
			//visit[s]=count;

			long count=1;

			while(!q.isEmpty()) {
				int v=q.dequeue();

				for(int w : g.adj.get(v)) {

						if(visit[w]==0) {
							visit[w]=1;
							count+=1;
							q.enqueue(w);
						}

				}
			}
			return count;
		}

	}

	/** Class for buffered reading int and double values */
	static class Redgy {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/** call this method to initialize reader for InputStream */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/** get next word */
		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		//BigInteger ans=Math.pow((double)n, (double)k)%1000000007;

		BigInteger ansi=BigInteger.valueOf(n);
		BigInteger ans=ansi.modPow(BigInteger.valueOf(k),BigInteger.valueOf(1000000007));

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				BigInteger c=BigInteger.valueOf(g.BFS(g, e, visit));

				BigInteger tmp=c.modPow(BigInteger.valueOf(k),BigInteger.valueOf(1000000007));
				ans=ans.subtract(tmp);
			}
		}

		System.out.println(ans.mod(BigInteger.valueOf(1000000007)));

//		BigInteger ai=BigInteger.valueOf(100000);
//		BigInteger an=ai.modPow(BigInteger.valueOf(100),BigInteger.valueOf(1000000007));
//		System.out.println(an);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class edgy {

	static class Node {
		int data;
		Node link;

		Node(int d,Node l){
			data=d;
			link=l;
		}
	}

	static class queue {
		protected Node front;
		protected Node rear;
		int size;

		queue(){
			front=null;
			rear=null;
			size=0;
		}

		boolean isEmpty() {
			return rear==null;
		}

		void enqueue(int x) {
			Node nptr=new Node(x,null);
			if(rear==null) {
				front=nptr;
			}
			else {
				rear.link=nptr;
			}
			rear=nptr;
			size++;
		}

		int dequeue() {
			int n=front.data;
			front=front.link;
			if(front==null) {
				rear=null;
			}
			size--;
			return n;
		}

		int see() {
			int n=front.data;
			return n;
		}

	}

	static class gedgy{

		int n;
		ArrayList<ArrayList<Integer>> adj;

		gedgy(int V) {
			n=V;
			adj=new ArrayList<ArrayList<Integer>>();

			for(int i=0;i<n;i++) {
				adj.add(new ArrayList<Integer>());
			}
		}

		void add_edge(gedgy g,int a,int b){

			g.adj.get(a).add(b);
			g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

		}

		long BFS(gedgy g,int s,int[] visit) {
			queue q=new queue();

			//int inf=-1;
			q.enqueue(s);
			visit[s]=1;
			//visit[s]=count;

			long count=1;

			while(!q.isEmpty()) {
				int v=q.dequeue();

				for(int w : g.adj.get(v)) {

						if(visit[w]==0) {
							visit[w]=1;
							count+=1;
							q.enqueue(w);
						}

				}
			}
			return count;
		}

	}

	/** Class for buffered reading int and double values */
	static class Redgy {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/** call this method to initialize reader for InputStream */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/** get next word */
		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		//BigInteger ans=Math.pow((double)n, (double)k)%1000000007;

		BigInteger ansi=BigInteger.valueOf(n);
		BigInteger ans=ansi.modPow(BigInteger.valueOf(k),BigInteger.valueOf(1000000007));

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				BigInteger c=BigInteger.valueOf(g.BFS(g, e, visit));

				BigInteger tmp=c.modPow(BigInteger.valueOf(k),BigInteger.valueOf(1000000007));
				ans=ans.subtract(tmp);
			}
		}

		System.out.println(ans);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class edgy {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		double ans=Math.pow((double)n, (double)k)%1000000007;

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				double c=g.BFS(g, e, visit);

				ans-=Math.pow(c, k)%1000000007;
			}
		}

		System.out.println((long)ans);
	}

}

/** Class for buffered reading int and double values */
class Redgy {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	/** call this method to initialize reader for InputStream */
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	/** get next word */
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
}

class gedgy{

	int n;
	ArrayList<ArrayList<Integer>> adj;

	gedgy(int V) {
		n=V;
		adj=new ArrayList<ArrayList<Integer>>();

		for(int i=0;i<n;i++) {
			adj.add(new ArrayList<Integer>());
		}
	}

	void add_edge(gedgy g,int a,int b){

		g.adj.get(a).add(b);
		g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

	}

	long BFS(gedgy g,int s,int[] visit) {
		queue q=new queue();

		//int inf=-1;
		q.enqueue(s);
		visit[s]=1;
		//visit[s]=count;

		long count=1;

		while(!q.isEmpty()) {
			int v=q.dequeue();

			for(int w : g.adj.get(v)) {

					if(visit[w]==0) {
						visit[w]=1;
						count+=1;
						q.enqueue(w);
					}

			}
		}
		return count;
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class edgy {

	static class Node {
		int data;
		Node link;

		Node(int d,Node l){
			data=d;
			link=l;
		}
	}

	static class queue {
		protected Node front;
		protected Node rear;
		int size;

		queue(){
			front=null;
			rear=null;
			size=0;
		}

		boolean isEmpty() {
			return rear==null;
		}

		void enqueue(int x) {
			Node nptr=new Node(x,null);
			if(rear==null) {
				front=nptr;
			}
			else {
				rear.link=nptr;
			}
			rear=nptr;
			size++;
		}

		int dequeue() {
			int n=front.data;
			front=front.link;
			if(front==null) {
				rear=null;
			}
			size--;
			return n;
		}

		int see() {
			int n=front.data;
			return n;
		}

	}

	static class gedgy{

		int n;
		ArrayList<ArrayList<Integer>> adj;

		gedgy(int V) {
			n=V;
			adj=new ArrayList<ArrayList<Integer>>();

			for(int i=0;i<n;i++) {
				adj.add(new ArrayList<Integer>());
			}
		}

		void add_edge(gedgy g,int a,int b){

			g.adj.get(a).add(b);
			g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

		}

		long BFS(gedgy g,int s,int[] visit) {
			queue q=new queue();

			//int inf=-1;
			q.enqueue(s);
			visit[s]=1;
			//visit[s]=count;

			long count=1;

			while(!q.isEmpty()) {
				int v=q.dequeue();

				for(int w : g.adj.get(v)) {

						if(visit[w]==0) {
							visit[w]=1;
							count+=1;
							q.enqueue(w);
						}

				}
			}
			return count;
		}

	}

	/** Class for buffered reading int and double values */
	static class Redgy {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/** call this method to initialize reader for InputStream */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/** get next word */
		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		long ans=(long)Math.pow((double)n, (double)k)%1000000007;

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				double c=g.BFS(g, e, visit);

				ans=ans-(long)Math.pow(c, k)%1000000007;
			}
		}

		System.out.println(ans);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class edgy {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		double ans=Math.pow((double)n, (double)k)%1000000007;

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				double c=g.BFS(g, e, visit);

				ans-=Math.pow(c, k)%1000000007;
			}
		}

		System.out.println((long)ans);
	}

}

class gedgy{

	int n;
	ArrayList<ArrayList<Integer>> adj;

	gedgy(int V) {
		n=V;
		adj=new ArrayList<ArrayList<Integer>>();

		for(int i=0;i<n;i++) {
			adj.add(new ArrayList<Integer>());
		}
	}

	void add_edge(gedgy g,int a,int b){

		g.adj.get(a).add(b);
		g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

	}

	long BFS(gedgy g,int s,int[] visit) {
		queue q=new queue();

		//int inf=-1;
		q.enqueue(s);
		visit[s]=1;
		//visit[s]=count;

		long count=1;

		while(!q.isEmpty()) {
			int v=q.dequeue();

			for(int w : g.adj.get(v)) {

					if(visit[w]==0) {
						visit[w]=1;
						count+=1;
						q.enqueue(w);
					}

			}
		}
		return count;
	}

}

/** Class for buffered reading int and double values */
class Redgy {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	/** call this method to initialize reader for InputStream */
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	/** get next word */
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class edgy {

	static class gedgy{

		int n;
		ArrayList<ArrayList<Integer>> adj;

		gedgy(int V) {
			n=V;
			adj=new ArrayList<ArrayList<Integer>>();

			for(int i=0;i<n;i++) {
				adj.add(new ArrayList<Integer>());
			}
		}

		void add_edge(gedgy g,int a,int b){

			g.adj.get(a).add(b);
			g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

		}

		long BFS(gedgy g,int s,int[] visit) {
			queue q=new queue();

			//int inf=-1;
			q.enqueue(s);
			visit[s]=1;
			//visit[s]=count;

			long count=1;

			while(!q.isEmpty()) {
				int v=q.dequeue();

				for(int w : g.adj.get(v)) {

						if(visit[w]==0) {
							visit[w]=1;
							count+=1;
							q.enqueue(w);
						}

				}
			}
			return count;
		}

	}

	/** Class for buffered reading int and double values */
	static class Redgy {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/** call this method to initialize reader for InputStream */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/** get next word */
		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		double ans=Math.pow((double)n, (double)k)%1000000007;

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				double c=g.BFS(g, e, visit);

				ans-=Math.pow(c, k)%1000000007;
			}
		}

		System.out.println((long)ans);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class edgy {

	static class Node {
		int data;
		Node link;

		Node(int d,Node l){
			data=d;
			link=l;
		}
	}

	static class queue {
		protected Node front;
		protected Node rear;
		int size;

		queue(){
			front=null;
			rear=null;
			size=0;
		}

		boolean isEmpty() {
			return rear==null;
		}

		void enqueue(int x) {
			Node nptr=new Node(x,null);
			if(rear==null) {
				front=nptr;
			}
			else {
				rear.link=nptr;
			}
			rear=nptr;
			size++;
		}

		int dequeue() {
			int n=front.data;
			front=front.link;
			if(front==null) {
				rear=null;
			}
			size--;
			return n;
		}

		int see() {
			int n=front.data;
			return n;
		}

	}

	static class gedgy{

		int n;
		ArrayList<ArrayList<Integer>> adj;

		gedgy(int V) {
			n=V;
			adj=new ArrayList<ArrayList<Integer>>();

			for(int i=0;i<n;i++) {
				adj.add(new ArrayList<Integer>());
			}
		}

		void add_edge(gedgy g,int a,int b){

			g.adj.get(a).add(b);
			g.adj.get(b).add(a); // **IMP** ONLY IF THE GRAPH IS UNDIRECTED

		}

		long BFS(gedgy g,int s,int[] visit) {
			queue q=new queue();

			//int inf=-1;
			q.enqueue(s);
			visit[s]=1;
			//visit[s]=count;

			long count=1;

			while(!q.isEmpty()) {
				int v=q.dequeue();

				for(int w : g.adj.get(v)) {

						if(visit[w]==0) {
							visit[w]=1;
							count+=1;
							q.enqueue(w);
						}

				}
			}
			return count;
		}

	}

	/** Class for buffered reading int and double values */
	static class Redgy {
		static BufferedReader reader;
		static StringTokenizer tokenizer;

		/** call this method to initialize reader for InputStream */
		static void init(InputStream input) {
			reader = new BufferedReader(new InputStreamReader(input));
			tokenizer = new StringTokenizer("");
		}

		/** get next word */
		static String next() throws IOException {
			while (!tokenizer.hasMoreTokens()) {
				// TODO add check for eof if necessary
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}

		static int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		static double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		static long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Redgy.init(System.in);

		int n=Redgy.nextInt();
		int k=Redgy.nextInt();

		gedgy g=new gedgy(n);

		for(int i=0;i<n-1;i++) {
			int u=Redgy.nextInt();
			int v=Redgy.nextInt();
			int x=Redgy.nextInt();

			if(x==0) {
				g.add_edge(g, u-1, v-1);
			}
		}

		int[] visit=new int[n];

		double ans=Math.pow((double)n, (double)k)%1000000007;

		for(int e=0;e<visit.length;e++) {
			if(visit[e]==0) {
				double c=g.BFS(g, e, visit);

				ans-=Math.pow(c, k)%1000000007;
			}
		}

		System.out.println((long)ans);
	}

}


import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    static boolean[] vis;
    static long size  = 0;
    static void dfs(int x){
        if(vis[x])
            return;
        vis[x]=true;
        size++;
        for (Integer edge : edges[x]) {
            dfs(edge);
        }
    }
    static ArrayList<Integer>[] edges;
    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
         edges = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            edges[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
                edges[a].add(b);
                edges[b].add(a);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        /*if(one.size() == 0){
            out.println(0);
            return;
        }*/
        vis = new boolean[n+1];
        long all = power(n,k,mod);
        for (int i = 1; i <= n; i++) {
            size = 0;
            dfs(i);
            all = all - power(size,k,mod);
            all+=mod;
            all%=mod;
        }
      //  long etc = power(zero.size(),k,mod);
      /*  int common = 0;
        for (Integer a : zero) {
            if(one.contains(a))
                common++;
        }
        long zz = 0;
        if(common>0)
         zz = power(common,k,mod)-common;
        long vv = power(one.size(),2,mod);
        long vn = power(n,k-2,mod);
        long dd = (one.size()*(one.size()-1))/2;
        long ss = (vv*vn*dd-n)%mod;
*/
        //long ans = all - etc - (n - zero.size()) + zz;
        out.println(all);

    }

}


import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    static boolean[] vis;
    static int size  = 0;
    static void dfs(int x){
        if(vis[x])
            return;
        vis[x]=true;
        size++;
        for (Integer edge : edges[x]) {
            dfs(edge);
        }
    }
    static ArrayList<Integer>[] edges;
    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
         edges = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            edges[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
                edges[a].add(b);
                edges[b].add(b);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        if(one.size() == 0){
            out.println(0);
            return;
        }
        vis = new boolean[n+1];
        long all = power(n,k,mod);
        for (int i = 1; i <= n; i++) {
            size = 0;
            dfs(i);
            all = all - power(size,k,mod);
        }
      //  long etc = power(zero.size(),k,mod);
      /*  int common = 0;
        for (Integer a : zero) {
            if(one.contains(a))
                common++;
        }
        long zz = 0;
        if(common>0)
         zz = power(common,k,mod)-common;
        long vv = power(one.size(),2,mod);
        long vn = power(n,k-2,mod);
        long dd = (one.size()*(one.size()-1))/2;
        long ss = (vv*vn*dd-n)%mod;
*/
        //long ans = all - etc - (n - zero.size()) + zz;
        out.println(all);

    }

}


import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        if(one.size() == 0){
            out.println(0);
            return;
        }
        long all = power(n,k,mod);
        long etc = power(zero.size(),k,mod);
        int common = 0;
        for (Integer a : zero) {
            if(one.contains(a))
                common++;
        }
        long zz = 0;
        if(common>0)
         zz = power(common-1,k,mod);

        long ans = all - etc - (n - zero.size()) + zz;
        out.println(ans);

    }

}


import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        if(one.size() == 0){
            out.println(0);
            return;
        }
        long all = power(n,k,mod);
        long etc = power(zero.size(),k,mod);
        int common = 0;
        for (Integer a : zero) {
            if(one.contains(a))
                common++;
        }
        long zz = 0;
        if(common>0)
         zz = power(common,k,mod)-common;

        long ans = all - etc - (n - zero.size()) + zz;
        out.println(ans);

    }

}


import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        if(one.size() == 0){
            out.println(0);
            return;
        }
        long all = power(n,k,mod);
        long etc = power(zero.size(),k,mod);
        long ans = all - etc - (n - zero.size());
        out.println(ans);

    }

}


import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;

public class C {
    public static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
        PrintWriter out = new PrintWriter(outputStreamWriter);
        FastReader in = new FastReader();

        int t = 1;
        while (t-- > 0)
            run(in, out);

        out.close();

    }
    static long mod = (long)(1e9+7);
    static long power(long x, long y, long p)
    {
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    static boolean[] vis;
    static int size  = 0;
    static void dfs(int x){
        if(vis[x])
            return;
        vis[x]=true;
        size++;
        for (Integer edge : edges[x]) {
            dfs(edge);
        }
    }
    static ArrayList<Integer>[] edges;
    private static void run(FastReader in, PrintWriter out) {
        int n = in.nextInt();
        int k = in.nextInt();
        HashSet<Integer> one = new HashSet<>();
        HashSet<Integer> zero = new HashSet<>();
         edges = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            edges[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n-1; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if(c == 0){
                zero.add(a);
                zero.add(b);
                edges[a].add(b);
                edges[b].add(b);
            }
            else {
                one.add(a);
                one.add(b);
            }

        }
        /*if(one.size() == 0){
            out.println(0);
            return;
        }*/
        vis = new boolean[n+1];
        long all = power(n,k,mod);
        for (int i = 1; i <= n; i++) {
            size = 0;
            dfs(i);
            all = all - power(size,k,mod);
            all+=mod;
            all%=mod;
        }
      //  long etc = power(zero.size(),k,mod);
      /*  int common = 0;
        for (Integer a : zero) {
            if(one.contains(a))
                common++;
        }
        long zz = 0;
        if(common>0)
         zz = power(common,k,mod)-common;
        long vv = power(one.size(),2,mod);
        long vn = power(n,k-2,mod);
        long dd = (one.size()*(one.size()-1))/2;
        long ss = (vv*vn*dd-n)%mod;
*/
        //long ans = all - etc - (n - zero.size()) + zz;
        out.println(all);

    }

}


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static ArrayList<Integer>[] vertices;
    static long mod = (long) 1e9 + 7;
    static boolean[] visited;
    static int counter = 0;

    static void dfs (int i) {
        visited[i]= true;
        counter++;
        for (int j : vertices[i]) {
            if (!visited[j]) {
                dfs(j);
            }
        }
    }

    static long fast_pow (long base, long exponent) {
        if (exponent == 0) {
            return 1L;
        }

        long mid = fast_pow(base, exponent / 2);

        if (exponent % 2 == 0) {
            return mid * mid % mod;
        } else {
            return mid * mid % mod * base % mod;
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int k = in.nextInt();

        vertices = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            vertices[i] = new ArrayList<>();
        }

        for (int i = 0; i < n - 1; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int color = in.nextInt();
            if (color == 0) {
                vertices[u].add(v);
                vertices[v].add(u);
            }
        }

        long sum = fast_pow(n, k);
        long inversCases = 0;
        visited = new boolean[n];
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                counter = 0;
                dfs(i);
                inversCases += fast_pow(counter, k);
            }
        }

        inversCases %= mod;
        sum = (sum - inversCases + mod) % mod;

        out.println(sum);

        out.close();
    }
}


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static int counter = 0;
    static int[] visited;
    static long mod = (long) 1e9 + 7;
    static ArrayList<Integer>[] adjoint;

    static void dfs(int i) {
        visited[i] = 1;
        counter++;

        for (int j : adjoint[i]) {
            if (visited[j] == 0) {
                dfs(j);
            }
        }
    }

    static long fast_pow(long base, long exponent) {
        if (exponent == 0) {
            return 1L;
        }

        long mid = fast_pow(base, exponent / 2);

        if (exponent % 2 == 0) {
            return mid * mid % mod;
        } else {
            return mid * mid % mod * base % mod;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int k = in.nextInt();

        adjoint = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            adjoint[i] = new ArrayList<>();
        }

        for (int i = 0; i < n - 1; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int color = in.nextInt();
            if (color == 0) {
                adjoint[u].add(v);
                adjoint[v].add(u);
            }
        }

        long ans = fast_pow(n, k);
        long redCases = 0;
        visited = new int[n];
        for (int i = 0; i < n; i++) {
            if (visited[i] == 0) {
                counter = 0;
                dfs(i);
                // out.println("counter: " + counter);
                redCases += fast_pow(counter, k);
            }
        }

        redCases %= mod;
        ans = (ans - redCases + mod) % mod;

        out.println(ans);

        out.close();
    }
}


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    static Scanner in = new Scanner(System.in);
    static PrintWriter out = new PrintWriter(System.out);
    static int parent[];
    static int size[];
    static long MOD = (long) 1e9 + 7;
    static Random rand = new Random();

    static void makeSet (int u) {
        parent[u] = u;
        size[u] = 1;
    }

    static int findSet (int u) {
        if (u == parent[u]) return u;
        parent[u] = findSet(parent[u]);
        return parent[u];
    }

    static void unionSet (int u, int v) {
        int parentU = findSet(u);
        int parentV = findSet(v);
        if (parentU != parentV) {
            if (size[parentU] < size[parentV]) {
                parent[parentU] = parentV;
                size[parentV] += size[parentU];
            } else {
                parent[parentV] = parentU;
                size[parentU] += size[parentV];
            }
        }
    }

    static long fast_pow (long base, long exponent) {
        if (exponent == 0) {
            return 1;
        }
        long mid = fast_pow(base, exponent / 2);
        if (exponent % 2 == 0) {
            return mid * mid % MOD;
        } else {
            return mid * mid % MOD * base % MOD;
        }
    }

    public static void main(String[] args) {
        int n = in.nextInt();
        int k = in.nextInt();

        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            makeSet(i);
        }

        for (int i = 0; i < n - 1; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int color = in.nextInt();
            if (color == 0) {
                unionSet(u, v);
            }
        }

        // get unique parent
        TreeSet<Integer> uniqueParent = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            uniqueParent.add(findSet(i));
        }

        Object uniqueParentList[] = uniqueParent.toArray();
        // count inverse graph
        long inverseGraph = 0;
        for (Object uParent : uniqueParentList) {
            inverseGraph = (inverseGraph + fast_pow(size[(Integer)uParent], k)) % MOD;
            // out.println("unique Parent: " + ((Integer)uParent + 1));
        }

        // for (int i = 0; i < n; i++) {
        //     out.println("i: " + (i + 1) + " parent: " + (findSet(i) + 1) + " size: " + size[i]);
        // }

        long sum = fast_pow(n, k);
        sum = (sum - inverseGraph + MOD) % MOD;

        out.println(sum);

        in.close();
        out.close();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		count = (long) (Math.pow(count,k));
        		//out.println(count);
        		tCount = (tCount+count);
        	}
        }
        int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		minus++;
        	}
        }
        long ans = (long)Math.pow(n,k);
        ans = ans-minus;
        ans = (ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		long count2 = count;
        		for(int j = 1; j < k; j++)
        		{

        			count = (long)(count2*count)%prime;
        		}
        		//out.println(count);
        		tCount = (tCount+count)%prime;
        	}
        }
        /*int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		out.println(i);
        		minus++;
        	}
        }
        out.println("minus "+minus);
        out.println("tcount "+tCount);
        */
        long ans = n;
        for(int i = 1; i < k; i++)
        {
        	ans = (long)(n*ans)%prime;
        }

        //out.println("ans "+ans);
        //ans = ans-minus;
        ans = (long)(ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		count = (long) (Math.pow(count,k));
        		//out.println(count);
        		tCount = (tCount+count);
        	}
        }
        /*int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		out.println(i);
        		minus++;
        	}
        }
        out.println("minus "+minus);
        out.println("tcount "+tCount);
        */
        long ans = (long)Math.pow(n,k);
        //out.println("ans "+ans);
        //ans = ans-minus;
        ans = (ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		count = (long) (Math.pow(count,k));
        		//out.println(count);
        		tCount = (tCount+count)%prime;
        	}
        }
        /*int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		out.println(i);
        		minus++;
        	}
        }
        out.println("minus "+minus);
        out.println("tcount "+tCount);
        */
        long ans = (long)Math.pow(n,k);
        //out.println("ans "+ans);
        //ans = ans-minus;
        ans = (ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		count = (long) (Math.pow(count,k));
        		//out.println(count);
        		tCount = (tCount+count)%prime;
        	}
        }
        /*int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		out.println(i);
        		minus++;
        	}
        }
        out.println("minus "+minus);
        out.println("tcount "+tCount);
        */
        long ans = (long)Math.pow(n,k)%prime;
        //out.println("ans "+ans);
        //ans = ans-minus;
        ans = (long)(ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		count = (long) (Math.pow(count,k)%prime);
        		//out.println(count);
        		tCount = (tCount+(count%prime))%prime;
        	}
        }
        int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		minus++;
        	}
        }
        long ans = (long) (Math.pow(n,k)%prime);
        ans = ans-minus;
        ans = (ans-tCount)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class C{

	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
	static LinkedList<Integer> ar[];
	static boolean vis[];
	static long count;
	public static void dfs(int i)
	{
		count++;
		vis[i] = true;

		for(int n:ar[i])
		{
			if(!vis[n])
			{
				dfs(n);
			}
		}
	}
	static long prime = (long) (1e9+7);

	static long fast_pow(long a, long b) {
		if(b == 0)
			return 1L;

		long val = fast_pow(a, b / 2);

		if(b % 2 == 0)
			return val * val % prime;
		else
			return val * val % prime * a % prime;
	}
	public static void main(String[] args)
	{
		OutputStream outputStream = System.out;
        FastReader sc = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);

        int n = sc.nextInt();
        int k = sc.nextInt();
        ar = new LinkedList[n];
        for(int i = 0; i <= n-1; i++)
        {
        	ar[i] = new LinkedList<>();
        }
        for(int i = 0; i < n-1; i++)
        {
        	int s = sc.nextInt();
        	int e = sc.nextInt();
        	int c = sc.nextInt();
        	if(c == 1) continue;

        	ar[s-1].add(e-1);
        	ar[e-1].add(s-1);
        }

        vis = new boolean[n];
        long tCount = 0;
        Arrays.fill(vis, false);
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		count = 0;
        		dfs(i);
        		//out.println(count);
        		long count2 = count;
        		for(int j = 1; j < k; j++)
        		{

        			count = (long)(count2*count)%prime;
        		}
        		//out.println(count);
        		tCount = (tCount+count)%prime;
        	}
        }
        /*int minus = 0;
        for(int i = 0; i < n; i++)
        {
        	if(!vis[i])
        	{
        		out.println(i);
        		minus++;
        	}
        }
        out.println("minus "+minus);
        out.println("tcount "+tCount);
        */
        long ans = n;
        for(int i = 1; i < k; i++)
        {
        	ans = (long)(n*ans)%prime;
        }

        //out.println("ans "+ans);
        //ans = ans-minus;
        ans = (long)(ans-tCount+prime)%prime;
        out.println(ans);
        out.close();
	}

}


import java.io.*;
import java.util.*;

public class C{
	// ------------------------
	static HashMap<Integer,HashSet<Integer>>tree=new HashMap<Integer,HashSet<Integer>>();
	static boolean[] used;
	static final long MOD=1000000007;
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));
		// ------------------------
		int n=sc.nextInt(),k=sc.nextInt();
		for(int i=0;i<n;i++)
			tree.put(i,new HashSet<Integer>());
		used=new boolean[n];
		for(int i=0;i<n-1;i++){
			int u=sc.nextInt()-1,v=sc.nextInt()-1,x=sc.nextInt();
			if(x==0){
				tree.get(u).add(v);
				tree.get(v).add(u);
			}
		}
		long bad=0;
		for(int i=0;i<n;i++)
			if(!used[i])
				bad+=pow(dfs(i),k);
		long total=pow(n,k);
		System.out.println((total+MOD-bad)%MOD);
		// ------------------------
		out.close();
	}
	//------------------------
	private static long pow(long x,long p){
		if(p==0)
			return 1;
		long a=pow(x,p/2);
		return a*a*(p%2==1?x:1);
	}
	private static long dfs(int i){
		used[i]=true;
		int ans=1;
		for(int j:tree.get(i))
			if(!used[j])
				ans+=dfs(j);
		return ans;
	}
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.io.*;
import java.util.*;

public class C{
	// ------------------------
	static HashMap<Integer,HashSet<Integer>>tree=new HashMap<Integer,HashSet<Integer>>();
	static boolean[] used;
	static final long MOD=1000000007;
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));
		// ------------------------
		int n=sc.nextInt(),k=sc.nextInt();
		for(int i=0;i<n;i++)
			tree.put(i,new HashSet<Integer>());
		used=new boolean[n];
		for(int i=0;i<n-1;i++){
			int u=sc.nextInt()-1,v=sc.nextInt()-1,x=sc.nextInt();
			if(x==0){
				tree.get(u).add(v);
				tree.get(v).add(u);
			}
		}
		long bad=0;
		for(int i=0;i<n;i++)
			if(!used[i])
				bad+=pow(dfs(i),k);
		long ans=pow(n,k)-bad;
		while(ans<0)
			ans+=MOD;
		System.out.println(ans%MOD);
		// ------------------------
		out.close();
	}
	//------------------------
	private static long pow(long x,long p){
		if(p==0)
			return 1;
		long a=pow(x,p/2);
		return (((a*a)%MOD)*(p%2==1?x:1))%MOD;
	}
	private static long dfs(int i){
		used[i]=true;
		int ans=1;
		for(int j:tree.get(i))
			if(!used[j])
				ans+=dfs(j);
		return ans;
	}
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.io.*;
import java.util.*;

public class C{
	// ------------------------
	static HashMap<Integer,HashSet<Integer>>tree=new HashMap<Integer,HashSet<Integer>>();
	static boolean[] used;
	static final long MOD=1000000007;
	public static void main(String[] args) {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));
		// ------------------------
		int n=sc.nextInt(),k=sc.nextInt();
		for(int i=0;i<n;i++)
			tree.put(i,new HashSet<Integer>());
		used=new boolean[n];
		for(int i=0;i<n-1;i++){
			int u=sc.nextInt()-1,v=sc.nextInt()-1,x=sc.nextInt();
			if(x==0){
				tree.get(u).add(v);
				tree.get(v).add(u);
			}
		}
		long bad=0;
		for(int i=0;i<n;i++)
			if(!used[i])
				bad+=pow(dfs(i),k);
		long total=pow(n,k);
		System.out.println((total+MOD-bad)%MOD);
		// ------------------------
		out.close();
	}
	//------------------------
	private static long pow(long x,long p){
		if(p==0)
			return 1;
		long a=pow(x,p/2);
		return (((a*a)%MOD)*(p%2==1?x:1))%MOD;
	}
	private static long dfs(int i){
		used[i]=true;
		int ans=1;
		for(int j:tree.get(i))
			if(!used[j])
				ans+=dfs(j);
		return ans;
	}
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

public class ForcesC {

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;
		//it reads the data about the specified point and divide the data about it ,it is quite fast
		//than using direct

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());//converts string to integer
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}

	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static long mod = (long)(1e9+7);
	static FastReader sc = new FastReader();
    static List<Integer>[] edge ;
    static int[] init;
    static int[] goal;
	public static void main(String[] args) {

		int n = sc.nextInt();
		int k = sc.nextInt();
		edge = new ArrayList[n+1];
		for(int i=0;i<=n;++i) edge[i] = new ArrayList<>();
		for(int i=1;i<n;++i) {
			int u = sc.nextInt();
			int v = sc.nextInt();
			int x = sc.nextInt();
			if(x == 0) {
			edge[u].add(v);
			edge[v].add(u);
			}
		}
		List<List<Integer>> cc = getComponents(n);
		long total = pow(n,k);
		long nahi  = 0;
		for(List<Integer> x : cc) {
			nahi = (nahi+pow(x.size(),k))%mod;
		}
		out.println(total-nahi);
        out.close();

	}

	private static long pow(long x, int y) {
	    long res = 1;
        x = x % mod;
        while (y > 0) {

            if((y & 1)==1)
                res = (res * x) % mod;

            y = y >> 1;
            x = (x * x) % mod;
        }
        return res;
}

	private static List<List<Integer>> getComponents(int n) {
		List<List<Integer>> output = new ArrayList<>();
		boolean[] visited = new boolean[n+1];
		for(int i=1;i<=n;++i) {
			if(!visited[i]) {
				List<Integer> component = new ArrayList<>();
				dfs(component,visited,i);
				output.add(component);
			}
		}
		return output;
	}

	private static void dfs(List<Integer> component, boolean[] visited, int v) {

		visited[v] = true;
		component.add(v);
		for(int i=0;i<edge[v].size();++i) {
			int adjV = edge[v].get(i);
			if(!visited[adjV])
				dfs(component,visited,adjV);
		}
	}

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

public class ForcesC {

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;
		//it reads the data about the specified point and divide the data about it ,it is quite fast
		//than using direct

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());//converts string to integer
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}

	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static long mod = (long)(1e9+7);
	static FastReader sc = new FastReader();
    static List<Integer>[] edge ;
    static int[] init;
    static int[] goal;
	public static void main(String[] args) {

		int n = sc.nextInt();
		int k = sc.nextInt();
		edge = new ArrayList[n+1];
		for(int i=0;i<=n;++i) edge[i] = new ArrayList<>();
		for(int i=1;i<n;++i) {
			int u = sc.nextInt();
			int v = sc.nextInt();
			int x = sc.nextInt();
			if(x == 0) {
			edge[u].add(v);
			edge[v].add(u);
			}
		}
		List<List<Integer>> cc = getComponents(n);
		long total = pow(n,k);
		long nahi  = 0;
		for(List<Integer> x : cc) {
			nahi = (nahi%mod+pow(x.size(),k))%mod;
		}

		out.println((total-nahi+mod)%mod);
        out.close();

	}

	private static long pow(long x, int y) {
	    long res = 1;
        x = x % mod;
        while (y > 0) {

            if((y & 1)==1)
                res = (res * x) % mod;

            y = y >> 1;
            x = (x * x) % mod;
        }
        return res;
}

	private static List<List<Integer>> getComponents(int n) {
		List<List<Integer>> output = new ArrayList<>();
		boolean[] visited = new boolean[n+1];
		for(int i=1;i<=n;++i) {
			if(!visited[i]) {
				List<Integer> component = new ArrayList<>();
				dfs(component,visited,i);
				output.add(component);
			}
		}
		return output;
	}

	private static void dfs(List<Integer> component, boolean[] visited, int v) {

		visited[v] = true;
		component.add(v);
		for(int i=0;i<edge[v].size();++i) {
			int adjV = edge[v].get(i);
			if(!visited[adjV])
				dfs(component,visited,adjV);
		}
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author cunbidun
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CEdgyTrees solver = new CEdgyTrees();
        solver.solve(1, in, out);
        out.close();
    }

    static class CEdgyTrees {
        private static final int MOD = (int) 1e9 + 7;
        private ArrayList<Integer>[] tree;
        private int[] size;
        private boolean[] vis;
        SBTree<PairII> set;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            tree = new ArrayList[n + 1];
            size = new int[n + 1];
            vis = new boolean[n + 1];
            for (int i = 1; i <= n; i++) {
                tree[i] = new ArrayList<>();
            }
            ArrayList<PairII> black = new ArrayList<>();
            set = new SBTree<>();
            for (int i = 1; i < n; i++) {
                int u = in.nextInt();
                int v = in.nextInt();
                int col = in.nextInt();
                if (col == 1) {
                    set.add(new PairII(u, v));
                    set.add(new PairII(v, u));
                }
                tree[u].add(v);
                tree[v].add(u);
            }

            int ans = 0;
            for (int i = 1; i <= n; i++) {
                if (!vis[i]) {
                    dfs(i);
                    ans = Op.addMod(ans, Op.powMod(size[i], k, MOD), MOD);
                }
            }
            out.println(Op.subMod(Op.powMod(n, k, MOD), ans, MOD));
        }

        private void dfs(int u) {
            size[u] = 1;
            vis[u] = true;
            for (int v : tree[u]) {
                if (!vis[v] && !set.find(new PairII(u, v))) {
                    dfs(v);
                    size[u] += size[v];
                }
            }
        }

    }

    static class Op {
        public static int addMod(int x, int y, int MOD) {
            return ((x + y) >= MOD ? x + y - MOD : x + y);
        }

        public static int subMod(int x, int y, int MOD) {
            return ((x - y) < 0 ? x - y + MOD : x - y);
        }

        public static int mulMod(int x, int y, int MOD) {
            return (int) (((long) x * y) % MOD);
        }

        public static int powMod(int x, long p, int MOD) {
            if (p == 0) return 1;
            int t = powMod(x, p / 2, MOD);
            if ((p & 1) == 1)
                return mulMod(mulMod(t, t, MOD), x, MOD);
            return mulMod(t, t, MOD);
        }

    }

    static class PairII implements Comparable<PairII> {
        public int first;
        public int second;

        public PairII(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            PairII pair = (PairII) o;

            return first == pair.first && second == pair.second;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        public int compareTo(PairII o) {
            int value = Integer.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }

    static class SBTree<T extends Comparable<T>> {
        private Node<T> root;

        public SBTree() {
            root = new Node<>();
        }

        private Node<T> rotate(Node<T> a, boolean flag) {
            int d = 0;
            if (flag) d = 1;
            Node<T> b = a.ch[1 - d];
            a.ch[1 - d] = b.ch[d];
            b.ch[d] = a;
            b.size = a.size;
            a.size = a.ch[0].size + a.ch[1].size + 1;
            return b;
        }

        private Node<T> maintain(Node<T> n, boolean flag) {
            if (n.size == 0) return n;
            int d = 0;
            if (flag) d = 1;
            if (n.ch[d].size > 0 && n.ch[d].ch[d].size > n.ch[1 - d].size) {
                n = rotate(n, !flag);
            } else if (n.ch[d].size > 0 && n.ch[d].ch[1 - d].size > n.ch[1 - d].size) {
                n.ch[d] = rotate(n.ch[d], flag);
                n = rotate(n, !flag);
            } else
                return n;
            n.ch[1] = maintain(n.ch[1], true);
            n.ch[0] = maintain(n.ch[0], false);
            n = maintain(n, true);
            n = maintain(n, false);
            return n;
        }

        private Node<T> add(Node<T> n, T key) {
            if (n.size == 0) {
                n = new Node<>(key);
                return n;
            } else {
                ++n.size;
                int flag = n.key.compareTo(key);
                int d = 0;
                if (flag < 0) d = 1;
                n.ch[d] = add(n.ch[d], key);
                n = maintain(n, flag < 0);
            }
            return n;
        }

        public void add(T key) {
            root = add(root, key);
        }

        public boolean find(T key) {
            Node<T> o = root;
            while (o.size > 0 && !o.key.equals(key)) {
                int flag = o.key.compareTo(key);
                int d = 0;
                if (flag < 0) d = 1;
                o = o.ch[d];
            }
            return o.size > 0;
        }

        private class Node<T> {
            Node<T>[] ch = new Node[2];
            T key;
            int size;

            Node(T key) {
                this.key = key;
                this.size = 1;
                ch[1] = ch[0] = new Node<>();
            }

            Node() {
                key = null;
                size = 0;
            }

        }

    }

    static class InputReader extends InputStream {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author cunbidun
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CEdgyTrees solver = new CEdgyTrees();
        solver.solve(1, in, out);
        out.close();
    }

    static class CEdgyTrees {
        private static final int MOD = (int) 1e9 + 7;
        private ArrayList<Integer>[] tree;
        private boolean[] vis;
        private int size = 0;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            tree = new ArrayList[n + 1];
            vis = new boolean[n + 1];
            for (int i = 1; i <= n; i++) {
                tree[i] = new ArrayList<>();
            }
            for (int i = 1; i < n; i++) {
                int u = in.nextInt();
                int v = in.nextInt();
                int col = in.nextInt();
                if (col == 0) {
                    tree[u].add(v);
                    tree[v].add(u);
                }

            }

            int ans = 0;
            for (int i = 1; i <= n; i++) {
                size = 0;
                if (!vis[i]) {
                    dfs(i);
                    ans = Op.addMod(ans, Op.powMod(size, k, MOD), MOD);
                }
            }
            out.println(Op.subMod(Op.powMod(n, k, MOD), ans, MOD));
        }

        private void dfs(int u) {
            size++;
            vis[u] = true;
            for (int v : tree[u])
                if (!vis[v]) dfs(v);
        }

    }

    static class InputReader extends InputStream {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class Op {
        public static int addMod(int x, int y, int MOD) {
            return ((x + y) >= MOD ? x + y - MOD : x + y);
        }

        public static int subMod(int x, int y, int MOD) {
            return ((x - y) < 0 ? x - y + MOD : x - y);
        }

        public static int mulMod(int x, int y, int MOD) {
            return (int) (((long) x * y) % MOD);
        }

        public static int powMod(int x, long p, int MOD) {
            if (p == 0) return 1;
            int t = powMod(x, p / 2, MOD);
            if ((p & 1) == 1)
                return mulMod(mulMod(t, t, MOD), x, MOD);
            return mulMod(t, t, MOD);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author cunbidun
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CEdgyTrees solver = new CEdgyTrees();
        solver.solve(1, in, out);
        out.close();
    }

    static class CEdgyTrees {
        private static final int MOD = (int) 1e9 + 7;
        private ArrayList<Integer>[] tree;
        private int[] size;
        private boolean[] vis;
        SBTree<PairII> set;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            tree = new ArrayList[n + 1];
            size = new int[n + 1];
            vis = new boolean[n + 1];
            for (int i = 1; i <= n; i++) {
                tree[i] = new ArrayList<>();
            }
            ArrayList<PairII> black = new ArrayList<>();
            set = new SBTree<>(false);
            for (int i = 1; i < n; i++) {
                int u = in.nextInt();
                int v = in.nextInt();
                int col = in.nextInt();
                if (col == 1) {
                    set.add(new PairII(u, v));
                    set.add(new PairII(v, u));
                }
                tree[u].add(v);
                tree[v].add(u);
            }

            int ans = 0;
            for (int i = 1; i <= n; i++) {
                if (!vis[i]) {
                    dfs(i);
                    ans = Op.addMod(ans, Op.powMod(size[i], k, MOD), MOD);
                }
            }
            out.println(Op.subMod(Op.powMod(n, k, MOD), ans, MOD));
        }

        private void dfs(int u) {
            size[u] = 1;
            vis[u] = true;
            for (int v : tree[u]) {
                if (!vis[v] && !set.find(new PairII(u, v))) {
                    dfs(v);
                    size[u] += size[v];
                }
            }
        }

    }

    static class PairII implements Comparable<PairII> {
        public int first;
        public int second;

        public PairII(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            PairII pair = (PairII) o;

            return first == pair.first && second == pair.second;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        public int compareTo(PairII o) {
            int value = Integer.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }

    static class Op {
        public static int addMod(int x, int y, int MOD) {
            return ((x + y) >= MOD ? x + y - MOD : x + y);
        }

        public static int subMod(int x, int y, int MOD) {
            return ((x - y) < 0 ? x - y + MOD : x - y);
        }

        public static int mulMod(int x, int y, int MOD) {
            return (int) (((long) x * y) % MOD);
        }

        public static int powMod(int x, long p, int MOD) {
            if (p == 0) return 1;
            int t = powMod(x, p / 2, MOD);
            if ((p & 1) == 1)
                return mulMod(mulMod(t, t, MOD), x, MOD);
            return mulMod(t, t, MOD);
        }

    }

    static class InputReader extends InputStream {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class SBTree<T extends Comparable<T>> {
        private Node<T> root;
        private boolean isMultiSet;

        public SBTree(boolean isMultiSet) {
            root = new Node<>();
            this.isMultiSet = isMultiSet;
        }

        private Node<T> rotate(Node<T> a, boolean flag) {
            int d = 0;
            if (flag) d = 1;
            Node<T> b = a.ch[1 - d];
            a.ch[1 - d] = b.ch[d];
            b.ch[d] = a;
            b.size = a.size;
            a.size = a.ch[0].size + a.ch[1].size + 1;
            return b;
        }

        private Node<T> maintain(Node<T> n, boolean flag) {
            if (n.size == 0) return n;
            int d = 0;
            if (flag) d = 1;
            if (n.ch[d].size > 0 && n.ch[d].ch[d].size > n.ch[1 - d].size) {
                n = rotate(n, !flag);
            } else if (n.ch[d].size > 0 && n.ch[d].ch[1 - d].size > n.ch[1 - d].size) {
                n.ch[d] = rotate(n.ch[d], flag);
                n = rotate(n, !flag);
            } else
                return n;
            n.ch[1] = maintain(n.ch[1], true);
            n.ch[0] = maintain(n.ch[0], false);
            n = maintain(n, true);
            n = maintain(n, false);
            return n;
        }

        private Node<T> add(Node<T> n, T key) {
            if (n.size == 0) {
                n = new Node<>(key);
                return n;
            } else {
                ++n.size;
                int flag = n.key.compareTo(key);
                int d = 0;
                if (flag < 0) d = 1;
                n.ch[d] = add(n.ch[d], key);
                n = maintain(n, flag < 0);
            }
            return n;
        }

        public void add(T key) {
            if (!find(key) || isMultiSet) root = add(root, key);
        }

        public boolean find(T key) {
            Node<T> o = root;
            while (o.size > 0 && !o.key.equals(key)) {
                int flag = o.key.compareTo(key);
                int d = 0;
                if (flag < 0) d = 1;
                o = o.ch[d];
            }
            return o.size > 0;
        }

        private class Node<T> {
            Node<T>[] ch = new Node[2];
            T key;
            int size;

            Node(T key) {
                this.key = key;
                this.size = 1;
                ch[1] = ch[0] = new Node<>();
            }

            Node() {
                key = null;
                size = 0;
            }

        }

    }
}