{$APPTYPE CONSOLE}
{$Q+,R+,O-,I+}

uses SysUtils;

Const
  MaxM=20;
  MaxN=10;
  Fname='d';

Type matrix=array [1..MaxN,1..MaxN] of integer;

var n,num,v,a,b,i,j,k,l:integer;
    m:array[1..MaxM] of matrix;
    mm:matrix;

begin
	assign(input, 'd.in');
  reset(input);
  
  assign(output, 'd.out');
  rewrite(output);
repeat  
  read(num,n,a,b,v);
  for i:=1 to num do
    for j:=1 to n do
      for k:=1 to n do read(m[i,j,k]);
  mm:=m[1];
  fillchar(m[1],sizeof(m[1]),0);
  for i:=2 to num do begin
    for j:=1 to n do 
      for k:=1 to n do begin
        m[1,j,k]:=0;
        for l:=1 to n do            
          m[1,j,k]:=(m[1,j,k]+mm[j,l]*m[i,l,k]) mod v;
      end;
    mm:=m[1];
  end;
  writeln(mm[a,b] mod v);
until seekEof;
end.
