#include <bits/stdc++.h>
using namespace std;
const int N = 200010, K = 10;
int n, m, k, res;
vector<pair<int, int>> g[N], gr[N];
bool banned[K][K][K][K];
int perm[K];
void dfs(int pos) {
  if (pos == k) {
    for (int i = 0; i < k; ++i)
      for (int j = i; j < k; ++j)
        if (banned[i][perm[i]][j][perm[j]]) return;
    ++res;
    return;
  }
  for (int i = 0; i <= pos; ++i) {
    perm[pos] = i;
    dfs(pos + 1);
  }
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    g[u].push_back({w, v});
  }
  for (int i = 0; i < n; ++i) sort(g[i].begin(), g[i].end());
  bool pos = 1;
  for (int i = 0; i < n; ++i) {
    if (!g[i].size()) pos = 0;
    for (int j = 0; j < g[i].size(); ++j)
      gr[g[i][j].second].push_back({g[i].size() - 1, j});
  }
  for (int i = 0; i < n; ++i) {
    bool have[K][K] = {};
    for (int j = 0; j < gr[i].size(); ++j) {
      int a = gr[i][j].first, b = gr[i][j].second;
      if (have[a][b]) banned[a][b][a][b] = 1;
      have[a][b] = 1;
    }
    for (int a = 0; a < k; ++a)
      for (int b = 0; b < k; ++b)
        if (have[a][b])
          for (int c = 0; c < k; ++c)
            for (int d = 0; d < k; ++d)
              if (have[c][d] && (a != c || b != d)) banned[a][b][c][d] = 1;
  }
  dfs(0);
  cout << pos * res << endl;
}
