【代码】
#include<bits/stdc++.h>
using namespace std;
const int N = 51000, mod = 987654321;
int n, m, c, f[N][11], uncol[N][11], v[N];
int x, y, z, ans = 0;
int main() {
memset(uncol, 0, sizeof uncol);
memset(v, 0, sizeof v);
scanf("%d%d%d", &n, &m, &c);
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
if(x == 1) {
for(int j = 1; j <= c; j++)
if(j != z)
uncol[y][j] = true;
}
if(x == 2) uncol[y][z] = true;
if(x == 3) {
if(y < z) swap(y, z);
v[y] = true;
}
}
for(int p = 1; p <= c; p++) {
if(uncol[1][p]) continue;
memset(f, 0, sizeof f);
f[1][p] = 1;
for(int i = 2; i < n; i++)
for(int j = 1; j <= c; j++) {
if(uncol[i][j])continue;
if(v[i]) {
f[i][j] += f[i-1][j];
f[i][j] %= mod;
} else {
for(int k = 1; k <= c; k++)
if(j != k) {
f[i][j] += f[i-1][k];
f[i][j] %= mod;
}
}
}
for(int i = 1; i <= c; i++) {
if(uncol[n][i] || i == p) continue;
if(v[n]) {
f[n][i] += f[n - 1][i];
f[n][i] %= mod;
} else {
for(int k = 1; k <= c; k++)
if(i != k) {
f[n][i] += f[n - 1][k];
f[n][i] %= mod;
}
}
ans += f[n][i];
ans %= mod;
}
}
printf("%d\\n", ans);
return 0;
}