#include <bits/stdc++.h>
using namespace std;
void cppio() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
const int maxn = int(2e5) + 10;
int n, m, k;
int a[maxn];
vector<pair<int, int> > e[maxn];
bitset<10> rnk[maxn][10];
int key[maxn];
int gain[10][10];
int ans, ax, cx;
void dfs(int lev) {
  if (lev == k) {
    if (ax == cx) ++ans;
    return;
  }
  for (int i = lev + 1; i; --i) {
    cx ^= gain[lev + 1][i];
    dfs(lev + 1);
    cx ^= gain[lev + 1][i];
  }
}
int main() {
  cppio();
  cin >> n >> m >> k;
  for (int i = 1; i <= (m); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    e[a].emplace_back(c, b);
  }
  for (int i = 1; i <= (n); ++i) sort((e[i]).begin(), (e[i]).end());
  for (int i = 1; i <= (n); ++i) {
    int r = 0, s = (int)(e[i]).size();
    for (auto &tmp : e[i]) {
      int j = tmp.second;
      rnk[j][s][++r] = 1;
    }
  }
  for (int i = 1; i <= (n); ++i) {
    key[i] = (rand() << 15) | (rand());
    ax ^= key[i];
  }
  for (int i = 1; i <= (n); ++i) {
    for (int j = 1; j <= (k); ++j)
      for (int l = 1; l <= (j); ++l)
        if (rnk[i][j][l]) gain[j][l] ^= key[i];
  }
  dfs(0);
  cout << ans << endl;
  return 0;
}
