Boboniu has a directed graph with n vertices and m edges.

The out-degree of each vertex is at most k.

Each edge has an integer weight between 1 and m. No two edges have equal weights.

Boboniu likes to walk on the graph with some specific rules, which is represented by a tuple (c_1,c_2,…,c_k). If he now stands on a vertex u with out-degree i, then he will go to the next vertex by the edge with the c_i-th (1≤ c_i≤ i) smallest weight among all edges outgoing from u.

Now Boboniu asks you to calculate the number of tuples (c_1,c_2,…,c_k) such that

  * 1≤ c_i≤ i for all i (1≤ i≤ k). 
  * Starting from any vertex u, it is possible to go back to u in finite time by walking on the graph under the described rules. 

Input

The first line contains three integers n, m and k (2≤ n≤ 2⋅ 10^5, 2≤ m≤ min(2⋅ 10^5,n(n-1) ), 1≤ k≤ 9).

Each of the next m lines contains three integers u, v and w (1≤ u,v≤ n,u≠ v,1≤ w≤ m), denoting an edge from u to v with weight w. It is guaranteed that there are no self-loops or multiple edges and each vertex has at least one edge starting from itself.

It is guaranteed that the out-degree of each vertex is at most k and no two edges have equal weight.

Output

Print one integer: the number of tuples.

Examples

Input


4 6 3
4 2 1
1 2 2
2 4 3
4 1 4
4 3 5
3 1 6


Output


2


Input


5 5 1
1 4 1
5 1 2
2 5 3
4 3 4
3 2 5


Output


1


Input


6 13 4
3 5 1
2 5 2
6 3 3
1 4 4
2 6 5
5 3 6
4 1 7
4 3 8
5 2 9
4 2 10
2 1 11
6 1 12
4 6 13


Output


1

Note

For the first example, there are two tuples: (1,1,3) and (1,2,3). The blue edges in the picture denote the c_i-th smallest edges for each vertex, which Boboniu chooses to go through.

<image>

For the third example, there's only one tuple: (1,2,2,2).

<image>

The out-degree of vertex u means the number of edges outgoing from u.
import java.util.*;

import java.io.*;

public class B {
	FastScanner in;
	PrintWriter out;
	boolean systemIO = true;

	public static void quickSort(int[] a, int from, int to) {
		if (to - from <= 1) {
			return;
		}
		int i = from;
		int j = to - 1;
		int x = a[from + (new Random()).nextInt(to - from)];
		while (i <= j) {
			while (a[i] < x) {
				i++;
			}
			while (a[j] > x) {
				j--;
			}
			if (i <= j) {
				int t = a[i];
				a[i] = a[j];
				a[j] = t;
				i++;
				j--;
			}
		}
		quickSort(a, from, j + 1);
		quickSort(a, j + 1, to);
	}

	public long gcd(long x, long y) {
		if (y == 0) {
			return x;
		}
		if (x == 0) {
			return y;
		}
		return gcd(y, x % y);
	}

	public boolean prime(long x) {
		for (int i = 2; i * i <= x; i++) {
			if (x % i == 0) {
				return false;
			}
		}
		return true;
	}

	public long pow(long x, long p) {
		if (p == 0) {
			return 1;
		}
		long t = pow(x, p / 2);
		t *= t;
		t %= mod;
		if (p % 2 == 1) {
			t *= x;
			t %= mod;
		}
		return t;
	}

	public class Fenvik {
		long[] sum;

		public Fenvik(int n) {
			sum = new long[n];
		}

		public void add(int x, long d) {
			for (int i = x; i < sum.length; i = (i | (i + 1))) {
				sum[i] += d;
			}
		}

		public long sum(int r) {
			long ans = 0;
			for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
				ans += sum[i];
			}
			return ans;
		}

		public long sum(int l, int r) {
			if (l > r) {
				return 0;
			}
			return sum(r) - sum(l - 1);
		}
	}

	public class Pair implements Comparable<Pair> {
		int x;
		int y;

		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int compareTo(Pair o) {
			if (x > o.x) {
				return 1;
			}
			if (x < o.x) {
				return -1;
			}
			if (y > o.y) {
				return 1;
			}
			if (y < o.y) {
				return -1;
			}
			return 0;
		}
	}

	long mod = 1000000007;

	public class Vector implements Comparable<Vector> {
		int x;
		int y;

		public Vector(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public String toString() {
			return x + " " + y;
		}

		public int quater() {
			if (x > 0 && y >= 0) {
				return 1;
			}
			if (y > 0 && x <= 0) {
				return 2;
			}
			if (y <= 0 && x < 0) {
				return 3;
			}
			return 4;
		}

		@Override
		public int compareTo(Vector o) {
			if (quater() != o.quater()) {
				return quater() - o.quater();
			}
			return -dp(this, o);
		}
	}

	public int dp(Vector a, Vector b) {
		return a.x * b.y - a.y * b.x;
	}

	public double[] leftAdd(double[] auxil, double[] input) {
		double[] ans = new double[auxil.length + input.length];
		for (int i = 0; i < auxil.length; i++) {
			ans[i] = auxil[i];
		}
		for (int i = 0; i < input.length; i++) {
			ans[i + auxil.length] = input[i];
		}
		return ans;
	}

	public double[] tensorProduct(double[] auxil, double[] input) {
		double[] ans = new double[auxil.length * input.length];
		for (int i = 0; i < auxil.length; i++) {
			for (int j = 0; j < input.length; j++) {
				ans[i * input.length + j] = auxil[i] * input[j];
			}
		}
		return ans;
	}

	public double[] tensorProductSquare(double[] auxil, double[] input) {
		double[] ans = new double[auxil.length * input.length * input.length];
		for (int i = 0; i < auxil.length; i++) {
			for (int j = 0; j < input.length; j++) {
				for (int k = 0; k < input.length; k++) {
					ans[i * input.length * input.length + j * input.length + k] = auxil[i] * input[j] * input[k];
				}
			}
		}
		return ans;
	}

	public double[] halfTensor(double[] auxil, double[] input) {
		int halfLa = auxil.length / 2;
		int halfLi = input.length / 2;
		double[] left = new double[halfLa + halfLi];
		for (int i = 0; i < halfLa; i++) {
			left[i] = auxil[i];
		}
		for (int i = 0; i < halfLi; i++) {
			left[halfLa + i] = input[i];
		}
		double[] right = new double[auxil.length + input.length - halfLa - halfLi];
		for (int i = halfLa; i < auxil.length; i++) {
			right[i - halfLa] = auxil[i];
		}
		for (int i = halfLi; i < input.length; i++) {
			right[auxil.length - halfLa + i - halfLi] = input[i];
		}
		auxil = left;
		input = right;
		return tensorProduct(auxil, input);
	}

	public double[] resize(double[] input) {
		int pow = 1;
		while (pow < input.length) {
			pow *= 2;
		}
		double[] ans = new double[pow];
		for (int i = 0; i < input.length; i++) {
			ans[i] = input[i];
		}
		for (int i = input.length; i < ans.length; i++) {
			ans[i] = 0;
		}
		return ans;
	}

	public void normalize(double[] input) {
		double norm = 0;
		for (int i = 0; i < input.length; i++) {
			norm += input[i] * input[i];
		}
		norm = Math.sqrt(norm);
		for (int i = 0; i < input.length; i++) {
			input[i] /= norm;
		}
	}

	public double[] transformation(int mode, double[] auxil, double[] input, boolean norm) {
		if (mode == 1) {
			input = leftAdd(auxil, input);
		} else if (mode == 2) {
			input = tensorProduct(auxil, input);
		} else if (mode == 3) {
			input = tensorProductSquare(auxil, input);
		} else if (mode == 4) {
			input = halfTensor(auxil, input);
		}
		input = resize(input);
		if (norm) {
			normalize(input);
		}
		return input;
	}

	public class Edge implements Comparable<Edge> {
		int to;
		int w;

		public Edge(int to, int w) {
			this.to = to;
			this.w = w;
		}

		@Override
		public int compareTo(Edge o) {
			return w - o.w;
		}
	}

	public class Conditions implements Comparable<Conditions> {
		int[] number;
		int[] index;
		int sz = 0;

		public Conditions(int n) {
			number = new int[n];
			index = new int[n];
		}

		public void add(int n, int i) {
			number[sz] = n;
			index[sz++] = i;
		}

		public void zip() {
			TreeSet<Pair> set = new TreeSet<>();
			for (int i = 0; i < number.length; i++) {
				Pair p = new Pair(number[i], index[i]);
				if (set.contains(p)) {
					can[number[i]][index[i]][number[i]][index[i]] = false;
				}
				set.add(p);
			}
			number = new int[set.size()];
			index = new int[set.size()];
			sz = 0;
			for (Pair p : set) {
				number[sz] = p.x;
				index[sz++] = p.y;
			}
			for (int i = 0; i < sz; i++) {
				for (int j = i + 1; j < sz; j++) {
					can[number[i]][index[i]][number[j]][index[j]] = false;
					can[number[j]][index[j]][number[i]][index[i]] = false;
				}
			}
		}

		@Override
		public int compareTo(Conditions o) {
			return sz - o.sz;
		}
	}

	public void rec(int sz) {
		if (sz == k) {
			ans++;
			return;
		}
		f: for (int i = 0; i <= sz; i++) {
			if (!can[sz][i][sz][i]) {
				continue;
			}
			a[sz] = i;
			for (int j = 0; j < sz; j++) {
				if (!can[j][a[j]][sz][i]) {
					continue f;
				}
			}
			rec(sz + 1);
		}
	}

	public boolean check() {
		f: for (Conditions cond : c) {
			boolean flag = false;
			for (int i = 0; i < cond.sz; i++) {
				if (a[cond.number[i]] == cond.index[i]) {
					if (flag) {
						return false;
					}
					flag = true;
				}
			}
			if (!flag) {
				return false;
			}
		}
		return true;
	}

	int ans = 0;
	int k;
	int[] a;
	boolean[][][][] can;
	Conditions[] c;

	@SuppressWarnings("unchecked")
	public void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		k = in.nextInt();
		can = new boolean[k][k][k][k];
		for (int i = 0; i < can.length; i++) {
			for (int j = 0; j < can.length; j++) {
				for (int x = 0; x < can.length; x++) {
					for (int y = 0; y < can.length; y++) {
						can[i][j][x][y] = true;
					}
				}
			}
		}
		a = new int[k];
		c = new Conditions[n];
		int[] sz = new int[n];
		ArrayList<Edge>[] to = new ArrayList[n];
		for (int i = 0; i < to.length; i++) {
			to[i] = new ArrayList<>();
		}
		for (int i = 0; i < m; i++) {
			int u = in.nextInt() - 1;
			int v = in.nextInt() - 1;
			to[u].add(new Edge(v, in.nextInt()));
			sz[v]++;
		}
		for (int i = 0; i < to.length; i++) {
			c[i] = new Conditions(sz[i]);
			if (sz[i] == 0) {
				out.print(0);
				return;
			}
			Collections.sort(to[i]);
		}
		for (int i = 0; i < to.length; i++) {
			for (int j = 0; j < to[i].size(); j++) {
				c[to[i].get(j).to].add(to[i].size() - 1, j);
			}
		}
		for (int i = 0; i < c.length; i++) {
			c[i].zip();
		}
		rec(0);
		out.print(ans);
	}

	public void run() {
		try {
			if (systemIO) {
				in = new FastScanner(System.in);
				out = new PrintWriter(System.out);
			} else {
				in = new FastScanner(new File("input.txt"));
				out = new PrintWriter(new File("output.txt"));
			}
			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		FastScanner(InputStream f) {
			br = new BufferedReader(new InputStreamReader(f));
		}

		String nextLine() {
			try {
				return br.readLine();
			} catch (IOException e) {
				return null;
			}
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				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());
		}

	}

	// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
	public static void main(String[] arg) {
		new B().run();
	}
}