#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& str, const pair<int, int>& p) {
  str << p.first << " " << p.second;
  return str;
}
template <typename T>
void join(T& arr, string sep) {
  bool first = true;
  for (auto t : arr) {
    if (first) {
      first = false;
      cout << t;
    } else {
      cout << sep << t;
    }
  }
  cout << "\n";
}
const int md = 1e9 + 7;
int MOD(int x) {
  int r = x % md;
  if (r < 0) {
    r += md;
  }
  return r;
}
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int res = 0;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < (int)(m); ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back({w, v});
  }
  vector<map<pair<int, int>, int>> inn(n);
  for (int u = 0; u < (int)(n); ++u) {
    if (g[u].empty()) {
      cout << 0 << endl;
      return;
    }
    sort(g[u].begin(), g[u].end());
    for (int i = 0; i < (int)(g[u].size()); ++i) {
      g[u][i].first = i + 1;
      inn[g[u][i].second][{g[u].size(), i + 1}]++;
    }
  }
  vector<vector<vector<vector<int>>>> bad(
      k + 1, vector<vector<vector<int>>>(
                 k + 1, vector<vector<int>>(k + 1, vector<int>(k + 1, 0))));
  for (int u = 0; u < (int)(n); ++u) {
    if (inn[u].empty()) {
      cout << 0 << endl;
      return;
    }
    for (auto& kv1 : inn[u]) {
      if (kv1.second > 1) {
        bad[kv1.first.first][kv1.first.second][kv1.first.first]
           [kv1.first.second] = 1;
      }
      for (auto& kv2 : inn[u]) {
        if (kv1.first == kv2.first && kv1.second == kv2.second) continue;
        bad[kv1.first.first][kv1.first.second][kv2.first.first]
           [kv2.first.second] = 1;
      }
    }
  }
  vector<int> cur(k);
  function<void(int)> permute = [&](int i) {
    if (i >= k) {
      bool success = true;
      for (int ii = 0; ii < (int)(k); ++ii) {
        for (int jj = (int)(ii + 1); jj < (int)(k); ++jj) {
          if (bad[ii + 1][cur[ii]][ii + 1][cur[ii]] ||
              bad[jj + 1][cur[jj]][jj + 1][cur[jj]]) {
            success = false;
            break;
          }
          if (bad[ii + 1][cur[ii]][jj + 1][cur[jj]] ||
              bad[jj + 1][cur[jj]][ii + 1][cur[ii]]) {
            success = false;
            break;
          }
        }
        if (!success) break;
      }
      if (success) res++;
      return;
    }
    for (int j = (int)(1); j < (int)(i + 2); ++j) {
      cur[i] = j;
      permute(i + 1);
    }
  };
  permute(0);
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  for (int t = 1; t <= tt; t++) {
    solve();
  }
  return 0;
}
