#include <bits/stdc++.h>
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
const int maxn = 2e5 + 100;
using bs = std::bitset<maxn>;
std::vector<pii> out[maxn];
std::vector<pii> in[maxn];
int bad[10][10];
int bad2[10][10][10][10];
std::vector<pii> active;
int generate(int i, int k) {
  if (i == k + 1) return 1;
  int result = 0;
  for (int v = 1; v <= i; v++) {
    if (bad[i][v]) continue;
    bool ok = true;
    for (auto& a : active) {
      if (bad2[a.first][a.second][i][v]) ok = false;
    }
    if (ok) {
      active.push_back({i, v});
      result += generate(i + 1, k);
      active.pop_back();
    }
  }
  return result;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, m, k;
  std::cin >> n >> m >> k;
  for (int i = 0; i < m; i++) {
    int x, y, w;
    std::cin >> x >> y >> w;
    out[x].push_back({w, y});
  }
  for (int i = 1; i <= n; i++) std::sort(out[i].begin(), out[i].end());
  std::map<int, std::vector<int>> by_deg;
  for (int i = 1; i <= n; i++) {
    by_deg[out[i].size()].push_back(i);
  }
  for (int i = 1; i <= k; i++) {
    for (int v = 1; v <= i; v++) {
      std::vector<int> outs;
      for (auto& x : by_deg[i]) {
        int y = out[x][v - 1].second;
        outs.push_back(y);
      }
      std::sort(outs.begin(), outs.end());
      outs.erase(std::unique(outs.begin(), outs.end()), outs.end());
      if (outs.size() != by_deg[i].size()) {
        bad[i][v] = 1;
        continue;
      }
      for (int y : outs) {
        for (auto& prev : in[y]) {
          bad2[prev.first][prev.second][i][v] = 1;
          bad2[i][v][prev.first][prev.second] = 1;
        }
        in[y].push_back({i, v});
      }
    }
  }
  std::cout << generate(1, k) << std::endl;
  return 0;
}
