#include #include #include #include #include // Yukicoder №1359 // https://yukicoder.me/problems/no/1359 // *********************** // for debug #define DEBUGe #define NOP do{}while(0) #ifdef DEBUG #define TRACE(...) do{printf(__VA_ARGS__);fflush(stdout);}while(0) #define TRACECR do{printf("\n");fflush(stdout);}while(0) #else #define TRACE(...) NOP #define TRACECR NOP #endif #define PRINCR printf("\n") #define NOCR(strig) do{char *p;p=strchr(strig,'\n');if(p)*p='\0';}while(0) // The out-of-date function #define asctime(...) asctime_s(...) #define atof(a) strtod(a,'\0') #define atoi(a) ((int)strtol(a,'\0')) #define atol(a) strtol(a,'\0') #define ctime(...) ctime_s(...) #define strlen(a) mystr_len(a) //#define fopen(...) fopen_s(...) //#define freopen(...) freopen_s(...) //#define rewind(a) fseek(a,0L,SEEK_SET) //#define setbuf(a,b) setvbuf(a,b,_IOFBF,BUFSIZ) // for stdio #define INPUT(str) do{char *p;fgets(str,sizeof(str),stdin);p=strchr(str,'\n');if(p)*p='\0';}while(0) #define mygc(c) (c)=getchar() static char *getinput( char* str ); static void read1(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);} static void read2(int *x, int *y){read1(x);read1(y);} static char *getinput(char* str) {char c;char *cp;cp=&str[0];c=fgetc(stdin);while( c != EOF ){if((c==' ')||( c=='\n')) break;*cp++=c;c=fgetc(stdin);}*cp='\0';return &str[0];} static int max(int a,int b){if(a>b){return a;}return b;} static int min(int a,int b){if(a*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;} static int cmpint_desc(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;} static int cmpchar_asc(const void *a,const void *b){if(*(char *)a>*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;} static int cmpchar_desc(const void *a,const void *b){if(*(char *)a<*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;} static int mystr_len(char *str){int i=0;do{if(str[i]=='\0')break;i++;if(i>9999)break;}while(1);return i;} static int countchar(char str[],char c,int limit ) {int cnt = 0;for(int i=0;i 0) { if (n & 1) ret = ret * x % MOD; // n の最下位bitが 1 ならば x^(2^i) をかける x = x * x % MOD; n >>= 1; // n を1bit 左にずらす } return ret; } #define INPBUF (11*3+3) int main() { char str[INPBUF]; int n; lolong k,m; lolong f; lolong ftotal = 0LL; lolong rmax,rmin; INPUT( str ); sscanf( str,"%d %lld %lld", &n, &k, &m ); MOD = m; REP(i,n) { getinput(str); sscanf(str,"%d", &P[i] ); } REP(i,n) { getinput(str); sscanf(str,"%d", &E[i] ); } REP(i,n) { getinput(str); sscanf(str,"%d", &A[i] ); } REP(i,n) { getinput(str); sscanf(str,"%d", &H[i] ); } qsort( P , n , sizeof(int) , cmpint_asc ); qsort( E , n , sizeof(int) , cmpint_asc ); qsort( A , n , sizeof(int) , cmpint_asc ); qsort( H , n , sizeof(int) , cmpint_asc ); REP(i,n) { rmax = max( max(P[i],E[i]), max(A[i],H[i]) ); rmin = min( min(P[i],E[i]), min(A[i],H[i]) ); if( rmax == rmin ) rmin = 0; f = fastpow( rmax - rmin ,k); ftotal += f; } ftotal %= m; printf("%lld\n", ftotal ); return 0; }