import std.stdio, std.algorithm, std.array, std.conv, std.typecons; void main() { auto nums = readln.split.to!(long[]); auto A = nums[0], B = nums[1], C = nums[2], D = nums[3], S = nums[4], T= nums[5], N = nums[6], K = nums[7]; auto mat = Matrix(A%K, B%K, C%K, D%K); // calculate M^N auto matN = Matrix(1, 0, 0, 1); auto mat_pow = mat; auto pow = 1UL; while (pow <= N) { if ((pow & N) != 0) matN = multiply(mat_pow, matN, K); pow *= 2; mat_pow = multiply(mat_pow, mat_pow, K); } auto R = matN[0]*S + matN[1]*T, U = matN[2]*S + matN[3]*T; auto R2 = R % K, U2 = U % K; if (R2 < 0) R2 += K; if (U2 < 0) U2 += K; writefln("%d %d", R2, U2); } // 0 1 // 2 3 alias Matrix = Tuple!(long, long, long, long); Matrix multiply(Matrix a, Matrix b, long K) { return Matrix( (a[0]*b[0] + a[1]*b[2]) % K, (a[0]*b[1] + a[1]*b[3]) % K, (a[2]*b[0] + a[3]*b[2]) % K, (a[2]*b[1] + a[3]*b[3]) % K); }