Powers of a matrix

Given a 2×22 \times 2 matrix MM of natural numbers, a natural number nn and a natural number mm, compute MnM^n. To avoid overflows, compute every element of MnmodmM^n \bmod m.

Input

Input consists of several cases, each with M11M_{11}, M12M_{12}, M21M_{21} and M22M_{22} in this order, followed by nn and mm. Assume that the elements of MM are not larger than 500500, 0n1090 \le n \le 10^9, and 2m10002 \le m \le 1000.

Output

For every case, print the elements of MnmodmM^n \bmod m in two lines following the format of the sample. Print a line with 10 dashes after every matrix.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:10:48.171Z

© Jutge.org, 2006–2026.
https://jutge.org