#include <bits/stdc++.h>
using namespace std;
vector<int> u[500008], v[500008];
int n, k, m, l, r, x, z = 998244353, a[500008], d[500008];
int main() {
  for (scanf("%d%d%d", &n, &k, &m); m--;
       scanf("%d%d%d", &l, &r, &x), u[l].push_back(r), v[l].push_back(x))
    ;
  for (d[0] = m = 1; k-- && m; m = m * (0LL | d[n + 1] - d[n]) % z) {
    for (l = n + 2; l--; a[l] = -1)
      ;
    for (l = r = 0; l++ <= n;
         d[l] =
             (d[l - 1] + (r < l ? d[l - 1] - (~a[l] ? d[a[l]] : 0) : 0) % z) %
             z)
      for (a[l] < a[l - 1] ? a[l] = a[l - 1] : 0, x = v[l].size(); x--;
           1 << k & v[l][x] ? r = max(r, u[l][x])
                            : a[u[l][x] + 1] = max(a[u[l][x] + 1], l - 1))
        ;
  }
  printf("%d\n", m < 0 ? m + z : m), exit(0);
}
