#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &n) {
  T w = 1;
  n = 0;
  char ch = getchar();
  while (!isdigit(ch) && ch != EOF) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch) && ch != EOF) {
    n = (n << 1) + (n << 3) + (ch ^ 48);
    ch = getchar();
  }
  n *= w;
}
template <typename T>
inline void write(T x) {
  unsigned long long y = 0;
  int l = 0;
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (!x) {
    putchar(48);
    return;
  }
  while (x) {
    y = y * 10 + x % 10;
    x /= 10;
    l++;
  }
  while (l) {
    putchar(y % 10 + 48);
    y /= 10;
    l--;
  }
}
template <typename T>
inline void writeln(T x) {
  write(x);
  puts("");
}
template <typename T>
inline void writes(T x) {
  write(x);
  putchar(' ');
}
template <typename T>
inline void checkmax(T &a, T b) {
  a = a > b ? a : b;
}
template <typename T>
inline void checkmin(T &a, T b) {
  a = a < b ? a : b;
}
const int N = 4e5 + 100, K = 15;
struct wzy {
  int to, val;
} h[N][K];
int s[K], cnt[N], ans;
unsigned long long hh[N], nh[K][K];
inline bool cmp(wzy x, wzy y) { return x.val < y.val; }
unsigned long long no = 0, zz = 0;
inline void dfs(int x, int k) {
  if (x > k) {
    ans += (no == zz);
    return;
  }
  for (int i = (1); i <= (x); i++) {
    no ^= nh[x][i];
    dfs(x + 1, k);
    no ^= nh[x][i];
  }
}
int main() {
  int n, m, k;
  read(n);
  read(m);
  read(k);
  srand(time(0));
  for (int i = (1); i <= (n); i++) {
    hh[i] = 1ULL * rand() * rand() * rand();
    zz ^= hh[i];
  }
  for (int i = (1); i <= (m); i++) {
    int x, y, w;
    read(x);
    read(y);
    read(w);
    cnt[x]++;
    h[x][cnt[x]].to = y;
    h[x][cnt[x]].val = w;
  }
  for (int i = (1); i <= (n); i++) sort(h[i] + 1, h[i] + cnt[i] + 1, cmp);
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (cnt[i]); j++) {
      nh[cnt[i]][j] ^= hh[h[i][j].to];
    }
  dfs(1, k);
  writeln(ans);
  return 0;
}
