#include #include #include using namespace std; using ll = long long; ll power_mod(ll a,ll n,ll m) { ll ans = 1; ll x = a % m; if (x < 0) x += m; while (n > 0) { if (n % 2 == 1) { ans = (ans * x) % m; } x = (x * x) % m; n /= 2; } return ans; } ll unfairness(ll p, ll e, ll a, ll h, int k, int m) { ll max = p; if (max < e) max = e; if (max < a) max = a; if (max < h) max = h; ll min = p; if (min > e) min = e; if (min > a) min = a; if (min > h) min = h; return power_mod(max - min, k, m); } void vector_input(vector& X) { for (int i = 0; i < X.size(); i++) { cin >> X.at(i); } sort(X.begin(), X.end()); } int main() { int N, K, M; cin >> N >> K >> M; vector P(N), E(N), A(N), H(N); vector_input(P); vector_input(E); vector_input(A); vector_input(H); ll X = 0; for (int i = 0; i < N; i++) { X += unfairness(P[i], E[i], A[i], H[i], K, M); X %= M; } cout << X << endl; return 0; }