#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int first = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    first = first * 10 + ch - 48;
  }
  return first * f;
}
const int mxN = 2e5;
int n, m, p;
vector<pair<int, int> > adj[mxN + 3];
int oud[mxN + 3];
bitset<mxN + 3> f[13][13];
vector<int> vec[13];
bool con[13][13][13][13];
bool ban[13][13];
int seq[mxN + 3], ans;
void dfs1(int u) {
  if (u == p + 1) {
    bool ok = true;
    for (int i = 1; i <= p; i++)
      for (int j = i + 1; j <= p; j++) {
        if (con[i][seq[i]][j][seq[j]]) {
          ok = false;
          break;
        }
      }
    if (ok) {
      ans++;
    }
    return;
  }
  for (int i = 1; i <= u; i++)
    if (!ban[u][i]) {
      seq[u] = i;
      dfs1(u + 1);
    }
}
int main() {
  n = read(), m = read(), p = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), w = read();
    adj[u].push_back(make_pair(w, v));
    oud[u]++;
  }
  for (int u = 1; u <= n; u++) sort(adj[u].begin(), adj[u].end());
  for (int u = 1; u <= n; u++) vec[oud[u]].push_back(u);
  for (int i = 1; i <= p; i++)
    for (int j = 1; j <= i; j++) {
      for (int o = 0; o < vec[i].size(); o++) {
        int u = vec[i][o];
        if (f[i][j].test(adj[u][j - 1].second)) {
          ban[i][j] = true;
          break;
        }
        f[i][j].set(adj[u][j - 1].second);
      }
    }
  for (int i1 = 1; i1 <= p; i1++)
    for (int j1 = 1; j1 <= i1; j1++)
      for (int i2 = i1 + 1; i2 <= p; i2++)
        for (int j2 = 1; j2 <= i2; j2++) {
          if ((f[i1][j1] & f[i2][j2]).any()) {
            con[i1][j1][i2][j2] = true;
          }
        }
  dfs1(1);
  printf("%d\n", ans);
  return 0;
}
