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;
long long INF = 1LL << 60;
long long MOD = 1e9 + 7;
namespace output {
void pr(short x) { cout << x; }
void pr(signed x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned short x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { cout << x; }
template <size_t sz>
void pr(const bitset<sz>& x) {
  cout << x;
}
template <class T>
void pr(const complex<T>& x) {
  cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T1, class T2, class T3>
void pr(const tuple<T1, T2, T3>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
  pr(t);
  pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
  pr("(", x.first, ", ", x.second, ")");
}
template <class T1, class T2, class T3>
void pr(const tuple<T1, T2, T3>& x) {
  pr("(", get<0>(x), ", ", get<1>(x), ", ", get<2>(x), ")");
}
template <class T>
void pr(const T& x) {
  pr("[");
  bool fst = 1;
  for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
  pr("]");
}
void print() { pr("\n"); }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
  pr(t);
  if (sizeof...(ts)) pr(" ");
  print(ts...);
}
}  // namespace output
using namespace output;
template <class T1, class T2>
istream& operator>>(istream& is, pair<T1, T2>& x) {
  is >> x.first >> x.second;
  return is;
}
template <class T1, class T2, class T3>
istream& operator>>(istream& is, tuple<T1, T2, T3>& x) {
  is >> get<0>(x) >> get<1>(x) >> get<2>(x);
  return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& x : v) is >> x;
  return is;
}
long long powa(long long base, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp & 1) res = res * base % MOD;
    base = base * base % MOD;
    exp >>= 1;
  }
  return res;
}
bool mini(long long& a, long long b) { return b < a ? a = b, 1 : 0; }
bool maxi(long long& a, long long b) { return b > a ? a = b, 1 : 0; }
signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  const long long BIG = 1000;
  vector<long long> sieve(BIG);
  vector<long long> primes;
  for (long long p = (2); p < (BIG); ++p) {
    if (!sieve[p]) {
      if (p > 500) {
        primes.push_back(p);
      }
      for (long long u = p * p; u < BIG; u += p) {
        sieve[u] = p;
      }
    }
  }
  long long p = primes[rand() % (long long)(primes).size()];
  long long n, m, k;
  cin >> n >> m >> k;
  vector<long long> tk(n, 1);
  for (long long i = (1); i < (n); ++i) {
    tk[i] = p * tk[i - 1] % MOD;
  }
  vector<vector<pair<long long, long long> > > nodes(n);
  for (long long i = (0); i < (m); ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    nodes[u].emplace_back(w, v);
  }
  vector<vector<long long> > cons(k, vector<long long>(k));
  for (long long u = (0); u < (n); ++u) {
    sort((nodes[u]).begin(), (nodes[u]).end());
    for (long long i = (0); i < ((long long)(nodes[u]).size()); ++i) {
      long long w, v;
      tie(w, v) = nodes[u][i];
      cons[(long long)(nodes[u]).size() - 1][i] =
          (cons[(long long)(nodes[u]).size() - 1][i] + tk[v]) % MOD;
    }
  }
  long long want = 0;
  for (long long i = (0); i < (n); ++i) {
    want = (want + tk[i]) % MOD;
  }
  vector<long long> perm(k, -1);
  long long res = 0;
  function<void(long long)> f = [&](long long upto) {
    if (upto == k) {
      long long cur = 0;
      for (long long i = (0); i < (k); ++i) {
        cur = (cur + cons[i][perm[i]]) % MOD;
      }
      if (cur == want) {
        ++res;
      }
      return;
    }
    for (long long i = (0); i < (upto + 1); ++i) {
      perm[upto] = i;
      f(upto + 1);
    }
    perm[upto] = -1;
  };
  f(0);
  print(res);
}
