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.
#include <bits/stdc++.h>
using namespace std;
template <class T = int>
T read() {
  T x = 0;
  char ch = 0;
  bool f = false;
  while (!isdigit(ch)) f |= (ch = getchar()) == '-';
  for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ '0');
  return f ? -x : x;
}
struct E {
  int v, w;
};
bool operator<(E a, E b) { return a.w < b.w; }
const int N = 200200;
vector<E> es[N];
const int K = 12;
bool ban[K * K][K * K];
vector<int> ins[N];
int main() {
  int n = read(), m = read();
  int k = read();
  for (int i = (int)(1); i <= (int)(m); i++) {
    int u = read(), v = read(), w = read();
    es[u].push_back({v, w});
  }
  for (int i = (int)(1); i <= (int)(n); i++) sort(es[i].begin(), es[i].end());
  for (int i = (int)(1); i <= (int)(n); i++) {
    int s = es[i].size();
    for (int j = 0; j < (int)(s); j++) {
      int v = es[i][j].v;
      ins[v].push_back(s * K + j + 1);
    }
  }
  for (int i = (int)(1); i <= (int)(n); i++) {
    int s = ins[i].size();
    map<int, int> mp;
    for (int j = 0; j < (int)(s); j++) mp[ins[i][j]]++;
    for (auto p1 : mp)
      for (auto p2 : mp) {
        if (p1.first != p2.first || p1.second >= 2) {
          ban[p1.first][p2.first] = true;
        }
      }
  }
  int a[K];
  int ans = 0;
  for (a[1] = 1; a[1] <= (1 <= k ? 1 : 1); a[1]++)
    for (a[2] = 1; a[2] <= (2 <= k ? 2 : 1); a[2]++)
      for (a[3] = 1; a[3] <= (3 <= k ? 3 : 1); a[3]++)
        for (a[4] = 1; a[4] <= (4 <= k ? 4 : 1); a[4]++)
          for (a[5] = 1; a[5] <= (5 <= k ? 5 : 1); a[5]++)
            for (a[6] = 1; a[6] <= (6 <= k ? 6 : 1); a[6]++)
              for (a[7] = 1; a[7] <= (7 <= k ? 7 : 1); a[7]++)
                for (a[8] = 1; a[8] <= (8 <= k ? 8 : 1); a[8]++)
                  for (a[9] = 1; a[9] <= (9 <= k ? 9 : 1); a[9]++) {
                    for (int i = (int)(1); i <= (int)(9); i++)
                      for (int j = (int)(1); j <= (int)(9); j++)
                        if (ban[i * K + a[i]][j * K + a[j]]) goto qaq;
                    ans++;
                  qaq:;
                  }
  printf("%d\n", ans);
  return 0;
}
