#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pii = pair<int, int>;
using vi = vector<int>;
#define fi first
#define se second
#define pb push_back
const int N = 18;
mt19937 rng(time(0));
int mul[1<<N][N+1];
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, m, k;
cin>>n>>m>>k;
for (int i=0; i<(1<<n); i++)
for (int j=0; j<=n; j++)
mul[i][j] = 1;
for (int i=1; i<=k; i++)
{
// c = rng()%(1<<n);
// d = rng()%(n+1);
// x = rng()%m;
cin>>c>>d>>x;
mul[c][d] = (mul[c][d] * x) % m;
}
for (int i=n-1; i>=0; i--)
{
// cout<<i<<":\n";
for (int cnt=0; cnt<=i+1; cnt++)
for (int mask=0; mask<(1<<n); mask++)
{
// cout<<mask<<" "<<cnt<<" "<<mul[mask][cnt]<<"\n";
if (cnt >= 1)
{
mul[mask^(1<<i)][cnt-1] = (mul[mask^(1<<i)][cnt-1] * mul[mask][cnt])%m;
}
}
}
for (int i=0; i<(1<<n); i++)
cout<<mul[i][0]<<" ";
cout<<"\n";
}