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>
#pragma comment(linker, "/STACK: 16777216")
using namespace std;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;
template <typename T>
void do_uniq(vector<T> vec) {
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
template <typename T>
void freadT(T &x) {
  x = 0;
  char ch = getchar_unlocked();
  ll sign = 1;
  while (!isdigit(ch)) {
    if (ch == '-') sign *= -1;
    ch = getchar_unlocked();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar_unlocked();
  }
  x *= sign;
}
int fparse(string s, int start_indx) {
  int x = 0;
  while (isdigit(s[start_indx])) {
    x = x * 10 + s[start_indx] - '0';
    ++start_indx;
  }
  return x;
}
const int MAXN = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll prime = 31;
ll ml(ll a, ll b, ll mod) { return (a * 1ll * b) % mod; }
ll add(ll a, ll b, ll mod) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod) { return (a + mod - b) % mod; }
vector<pair<int, int>> g[MAXN];
ll cnt = 0;
ll target;
bitset<MAXN> prec[10][10];
ll precH[10][10];
bool skip[10][10];
int n, m, k;
void rec(ll bi, int indx) {
  if (indx == k + 1) {
    if (bi == target) {
      ++cnt;
    }
    return;
  }
  for (int i = 1; i <= indx; ++i) {
    if (skip[indx][i]) continue;
    ll nxt = add(bi, precH[indx][i], mod);
    rec(nxt, indx + 1);
  }
}
ll powers[MAXN];
void precc() {
  powers[0] = 1;
  for (int i = 1; i < MAXN; ++i) {
    powers[i] = ml(powers[i - 1], prime, mod);
  }
}
void solve() {
  precc();
  cin >> n >> m >> k;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    g[u].emplace_back(w, v);
  }
  for (int i = 0; i < n; ++i) {
    sort((g[i]).begin(), (g[i]).end());
  }
  for (int i = 0; i < n; ++i) target = add(target, ml(powers[i], 1, mod), mod);
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= g[i].size(); ++j) {
      if (prec[g[i].size()][j][g[i][j - 1].second]) {
        skip[g[i].size()][j] = true;
      } else {
        prec[g[i].size()][j][g[i][j - 1].second] = 1;
      }
    }
  }
  for (int i = 1; i < 10; ++i) {
    for (int j = 1; j < 10; ++j) {
      precH[i][j] = 0;
      for (int kk = 0; kk < n; ++kk) {
        if (prec[i][j][kk]) precH[i][j] = add(precH[i][j], powers[kk], mod);
      }
    }
  }
  rec(0, 1);
  cout << cnt;
}
clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}
void multisolve() {
  int t;
  cin >> t;
  int i = 1;
  while (t--) {
    solve();
    i++;
  }
}
void gen() {
  cout << 50 << "\n";
  for (int i = 0; i < 50; ++i) {
    for (int j = 0; j < 50; ++j) {
      cout << 1;
    }
    cout << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);
  solve();
}
