include
include
include
include
define int long long
using namespace std;
struct Matrix{
int rec[40][40];
} uni,a;
struct Ma2{
Matrix rec[40][40];
} org,ans;
int n,k,m;
Matrix zero;
Ma2 z;
Matrix tim(Matrix x,Matrix y){
Matrix tem=zero;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
for(int k=1;k<=n;++k){
tem.rec[i][j]+=x.rec[i][k]y.rec[k][j];
tem.rec[i][j]%=m;
}
}
}
return tem;
}
Matrix pluss(Matrix x,Matrix y){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
x.rec[i][j]+=y.rec[i][j];
x.rec[i][j]%=m;
}
}
return x;
}
void print(Matrix x){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
printf("%lld ",x.rec[i][j]%m);
}
cout<<endl;
}
return ;
}
Ma2 mul(Ma2 x,Ma2 y){
Ma2 tem;
for(int i=1;i<=3;++i){
cout<<i<<endl;
for(int j=1;j<=3;++j){
for(int k=1;k<=3;++k){
tem.rec[i][j]=pluss(tem.rec[i][j],tim(x.rec[i][k],y.rec[k][j]));
}
}
}
return tem;
}
void buildd(){
ans.rec[1][1]=tim(a,a);
ans.rec[2][1]=a;
ans.rec[3][1]=a;
cout<<"k"<<k<<endl;
Ma2 tem=org;
k--;
while(k){
cout<<k<<endl;
if(k&1){
tem=mul(tem,org);
}
k>>=1;
org=mul(org,org);
}
print(mul(tem,ans).rec[1][1]);
return ;
}
signed main(){
scanf("%lld%lld%lld",&n,&k,&m);
for(int i=1;i<=n;++i){
uni.rec[i][i]=1;
for(int j=1;j<=n;++j){
scanf("%lld",&a.rec[i][j]);
a.rec[i][j]%=m;
}
}
org.rec[1][1]=a;org.rec[1][3]=org.rec[2][1]=org.rec[3][1]=uni;
if(k1){
print(a);
return 0;
}
if(k2){
print(pluss(tim(a,a),a));
return 0;
}
k=k-2;
buildd();
return 0;
}
/
2 3 1000
0 1
1 1
*/