import java.io.*;
import java.util.Scanner;
/**
* Created by EugeniaKim on 15/12/2015.
*/
public class Main {
private int a;
private int b;
private int x;
private int y;
private int z;
private int M;
private State head;
public Main(int fst, int snd, int a, int b, int x, int y, int z, int M) {
this.head = new State(fst, snd);
this.a = a;
this.b = b;
this.x = x;
this.y = y;
this.z = z;
this.M = M;
}
public State getStateFromList(int index) {
State quickPtr = head;
State slowPtr = head;
do {
if(index == 0) {
return slowPtr;
}
quickPtr = quickPtr.getNext(a, b, x, y, z, M).getNext(a, b, x, y, z, M);
slowPtr = slowPtr.getNext(a, b, x, y, z, M);
index--;
} while (!(quickPtr.equals(slowPtr)));
slowPtr = head;
while(slowPtr.equals(quickPtr)) {
quickPtr = quickPtr.getNext(a, b, x, y, z, M);
slowPtr = slowPtr.getNext(a, b, x, y, z, M);
index--;
if(index == 0) {
return slowPtr;
}
}
// slowPtr is now on the start of the loop
if(index > 0) {
int length = lengthOfCycle(head);
index %= length;
while(index > 0) {
slowPtr = slowPtr.getNext(a, b, x, y, z, M);
index--;
}
}
return slowPtr;
}
private int lengthOfCycle(State start) {
int length = 0;
State current = start.getNext(a, b, x, y, z, M);
while (current.equals(start)) {
current = current.getNext(a, b, x, y, z, M);
length++;
}
return length;
}
private class State {
private int fst;
private int snd;
private State next = null;
public State(int fst, int snd) {
this.fst = fst;
this.snd = snd;
}
public State getNext(int a, int b, int x, int y, int z, int M) {
if(next == null) {
next = new State(this.snd, (int)(a*Math.pow(fst, 2) + b*Math.pow(snd, 2) + x*fst + y*snd + z)%M);
}
return next;
}
@Override
public boolean equals(Object s) {
return fst == ((State)s).fst && snd == ((State)s).snd;
}
@Override
public int hashCode() {
return this.hashCode();
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("rsir.in"));
int T0 = Integer.parseInt(sc.next());
int T1 = Integer.parseInt(sc.next());
int a = Integer.parseInt(sc.next());
int b = Integer.parseInt(sc.next());
int x = Integer.parseInt(sc.next());
int y = Integer.parseInt(sc.next());
int z = Integer.parseInt(sc.next());
int M = Integer.parseInt(sc.next());
int index = Integer.parseInt(sc.next());
Main rsir = new Main(T0, T1, a, b, x, y, z, M);
State result = rsir.getStateFromList(index);
sc.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("rsir.out"));
bw.write(String.valueOf(result.fst));
bw.close();;
}
}