Matrix Power Series
include
include
include
include
//#define int long long
using namespace std;
struct Matrix{
int rec[40][40];
} uni,a;
struct Ma2{
Matrix rec[5][5];
} org,ans;
int n,k,m;
Matrix zero;
Ma2 z;
Matrix tim(Matrix x,Matrix y){
// cout<<"fsdsdf";
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]%m)*(y.rec[k][j]%m);
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);
if(j!=n){
printf(" ");
}
}
cout<<endl;
}
return ;
}
Ma2 mul(Ma2 x,Ma2 y){
Ma2 tem;
for(int i=1;i<=3;++i){
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;
}
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][3]=uni;
if(k1){
print(a);
return 0;
}
if(k2){
print(pluss(tim(a,a),a));
return 0;
}
k=k-2;
ans.rec[1][1]=tim(a,a);
ans.rec[2][1]=a;
ans.rec[3][1]=a;
Ma2 tem=org;
k--;
while(k){
if(k&1){
tem=mul(tem,org);
}
k>>=1;
org=mul(org,org);
}
print(mul(tem,ans).rec[1][1]);
return 0;
}