#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
ll hsh[200000];
vector<vector<pair<ll, ll>>> g;
ll n, m, k;
ll c[10];
ll res;
ll allVerticesHash = 1;
ll chash[10][10];
void rec(int i) {
  if (i == k) {
    ll cur = 1;
    for (ll j = 0; j < (ll)k; ++j) {
      cur += chash[j][c[j]];
      cur %= ((ll)(1e9 + 7));
    }
    res += cur == allVerticesHash;
    return;
  }
  for (ll j = 0; j < (ll)i + 1; ++j) {
    c[i] = j;
    rec(i + 1);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  srand(time(0));
  cin >> n >> m >> k;
  for (ll i = 0; i < (ll)n; ++i) {
    hsh[i] = rand() + 2;
    allVerticesHash += hsh[i];
    allVerticesHash %= ((ll)(1e9 + 7));
  }
  g = vector<vector<pair<ll, ll>>>(n);
  for (ll i = 0; i < (ll)m; ++i) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back({w, v});
  }
  for (ll i = 0; i < (ll)n; ++i) sort(g[i].begin(), g[i].end());
  for (ll i = 0; i < (ll)k; ++i) {
    for (int v = 0; v < n; ++v) {
      if (g[v].size() != i + 1) continue;
      for (ll j = 0; j < (ll)i + 1; ++j) {
        chash[i][j] += hsh[g[v][j].second];
        chash[i][j] %= ((ll)(1e9 + 7));
      }
    }
  }
  rec(0);
  cout << res << '\n';
  return 0;
}
