You are given an integer array $$$a_0, a_1, \dots, a_{n - 1}$$$, and an integer $$$k$$$. You perform the following code with it:long long ans = 0; // create a 64-bit signed variable which is initially equal to 0for(int i = 1; i <= k; i++){  int idx = rnd.next(0, n - 1); // generate a random integer between 0 and n - 1, both inclusive                                // each integer from 0 to n - 1 has the same probability of being chosen  ans += a[idx];  a[idx] -= (a[idx] % i);}Your task is to calculate the expected value of the variable ans after performing this code.Note that the input is generated according to special rules (see the input format section).

The only line contains six integers $$$n$$$, $$$a_0$$$, $$$x$$$, $$$y$$$, $$$k$$$ and $$$M$$$ ($$$1 \le n \le 10^7$$$; $$$1 \le a_0, x, y < M \le 998244353$$$; $$$1 \le k \le 17$$$). The array $$$a$$$ in the input is constructed as follows:   $$$a_0$$$ is given in the input;  for every $$$i$$$ from $$$1$$$ to $$$n - 1$$$, the value of $$$a_i$$$ can be calculated as $$$a_i = (a_{i - 1} \cdot x + y) \bmod M$$$.

Let the expected value of the variable ans after performing the code be $$$E$$$. It can be shown that $$$E \cdot n^k$$$ is an integer. You have to output this integer modulo $$$998244353$$$.

NoteThe array in the first example test is $$$[10, 35, 22]$$$. In the second example, it is $$$[15363, 1418543]$$$.

// package c1626;

import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

//
// Educational Codeforces Round 121 (Rated for Div. 2) 2022-01-16 06:35
// F. A Random Code Problem
// https://codeforces.com/contest/1626/problem/F
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// You are given an integer array a_0, a_1, ..., a_{n - 1}, and an integer k. You perform the
// following code with it:

//
// Your task is to calculate the expected value of the variable ans after performing this code.
//
// Note that the input is generated according to special rules (see the input format section).
//
// Input
//
// The only line contains six integers n, a_0, x, y, k and M (1 <= n <= 10^7; 1 <= a_0, x, y < M <=
// 998244353; 1 <= k <= 17).
//
// The array a in the input is constructed as follows:
//  * a_0 is given in the input;
//  * for every i from 1 to n - 1, the value of a_i can be calculated as a_i = (a_{i - 1} * x + y)
//    mod M.
//
// Output
//
// Let the expected value of the variable ans after performing the code be E. It can be shown that E
// * n^k is an integer. You have to output this integer modulo 998244353.
//
// Example
/*
input:
3 10 3 5 13 88

output:
382842030

input:
2 15363 270880 34698 17 2357023

output:
319392398
*/
// Note
//
// The array in the first example test is [10, 35, 22]. In the second example, it is [15363,
// 1418543].
//
public class C1626F {
  static final int MOD = 998244353;
  static final Random RAND = new Random();

  // Based on: https://codeforces.com/contest/1626/submission/143042312
  static int solve(int[] a, int k) {
    int n = a.length;
    int m = getLcm(k-1);
    long ans = 0;
    long[] prob = new long[m];
    long[] expe = new long[m];
    long[] nprob = new long[m];
    long[] nexpe = new long[m];
    for (int v : a) {
      ans = (ans + v / m) % MOD;
      prob[v % m]++;
    }
    ans = ((ans * m) % MOD * k) % MOD;
    for (int i = 1; i < k; i++) {
      ans = (ans * n) % MOD;
    }
    for (int i = 1; i <= k; i++) {
      for (int j = 0; j < m; j++) {
        nexpe[j] = expe[j] * (n - 1) % MOD;
        nprob[j] = prob[j] * (n - 1) % MOD;
        int nj = j - j % i;
        nexpe[nj] = (nexpe[nj] + expe[j] + prob[j] * j) % MOD;
        nprob[nj] = (nprob[nj] + prob[j]) % MOD;
      }
      long[] t1 = expe;
      expe = nexpe;
      nexpe = t1;
      long[] t2 = prob;
      prob = nprob;
      nprob = t2;
    }
    for (int i = 0; i < m; i++) {
      ans = (ans + expe[i]) % MOD;
    }
    return (int) ans;
  }

  // Get LCM of 1,2,3,4,...,k
  static int getLcm(int k) {
    int ans = 1;
    for (int i = 2; i <= k; i++) {
      ans = ans * i / gcd(ans, i);
    }
    return ans;
  }

  static int gcd(int a, int b) {
    return a == 0 ? b : gcd(b % a, a);
  }

  // Time limit exceeded on test 8
  static int solveA(int[] a, int k) {
    int n = a.length;
    // We are asked to compute the sum of ans across all n^k cases ---
    // make k idx selections each time from [0,n).
    // i = 1: Total n^1 cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^(k-1) final cases. Then a[idx] remains the same.
    // i = 2: Total n^2 cases at the level with each a[idx] added into ans n^1 times
    //        contribute to n^(k-2) final cases. Then a[idx] is trimmed to be even.
    // i = 3: Total n^3 cases at the level with each a[idx] added into ans n^2 times,
    //        contribute to n^(k-3) final cases. Then a[idx] is trimmed to be multiple of 3.
    // ...
    // i = k: Total n^k cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^0 final cases. Then a[idx] is trimmed to be multiple of k.

    // Compute pows[i] = (n^i % MOD)
    long[] pows = new long[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }

    //
    // To be efficient, here we use array instead of Map<Long, Long>.
    // For k = 17, the largest value range observed is 110, like for 59980733.
    final int[] mrs = {0, 1, 2, 4, 6, 10, 12, 18, 24, 30, 38, 48, 54, 66, 78, 84, 94, 110};
    final int maxRange = mrs[k] + 1;
    long[] curr = new long[maxRange];

    // Accumulated sums[i] to be multiplied by pows[k-i]
    long[] sums = new long[k+1];

    long ans = 0;
    for (long v : a) {
      Arrays.fill(curr, 0L);
      curr[0] = 1L;
      for (int i = 1; i <= k; i++) {
        // For each incidence of cv, it is chosen once and not chosen n - 1 times.
        // Once chosen it contribute to ans n^(k-i) times.

        // refresh from smallest to v in-place within curr
        for (int d = mrs[i]; d >= 0; d--) {
          if (curr[d] == 0) {
            continue;
          }
          // current value and count
          long cv = v - d;
          long count = curr[d] % MOD;

          // chosen once:
          sums[i] = (sums[i] + cv * count) % MOD;
          long nv = cv - cv % i;
          if (nv != 0) {
            int d1 = (int) (v - nv);
            myAssert(d1 >= d);
            curr[d1] += count;
          }

          // In (n-1) times, no update, add count * (n-2) to the count
          // not chose (n-1) times:
          long nc = (count * (n-2)) % MOD;
          if (nc != 0) {
            curr[d] += nc;
          }
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      ans = (ans + sums[i] * pows[k-i]) % MOD;
    }
    return (int) ans;
  }

  public static int modInverse(long a, int p) {
    return power(a, p - 2, p);
  }

  // To compute x^y modulo m
  public static int power(long x, long y, int p) {
    x %= p;
    if (y == 0) {
      return 1;
    }
    long w = power(x, y / 2, p);
    w = (w * w) % p;
    return (int) (y % 2 == 0 ? w : (w * x) % p);
  }

  static int solveNaive(int[] a, int k) {
    // System.out.format("%s %d\n", Arrays.toString(a), k);
    // Note that the problem asks (E * n^k) % MOD, not E itself
    int n = a.length;
    int[] pows = new int[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }
    return solveNaive(a, k, 1, pows);
  }

  static int index = 0;
  static int solveNaive(int[] a, int k, int i, int[] pows) {
    if (i > k) {
      return 0;
    }
    long sum = 0;
    for (int v : a) {
      sum += v;
    }
    // System.out.format("  %7d i:%2d %s\n", ++index, i, Arrays.toString(a));
    int n = a.length;
    // Each number at this level contribute to pows[k - i] overall cases
    long ans = ((sum % MOD) * pows[k - i]) % MOD;

    // Each idx in [0,n-1] has equal probability to be chosen
    for (int idx = 0; idx < n; idx++) {
      int[] b = new int[n];
      System.arraycopy(a, 0, b, 0, n);
      b[idx] -= b[idx] % i;
      ans = (ans + solveNaive(b, k, i + 1, pows)) % MOD;
    }

    return (int) (ans % MOD);
  }

  static void doTest() {
    long t0 = System.currentTimeMillis();
    if ("showLcm".isEmpty()) {
      for (int k = 1; k < 17; k++) {
        System.out.format("  k:%2d lcm:%d\n", k, getLcm(k));
      }
    }
    // show maxRange of around 110
    if ("show".isEmpty()) {
      int[] maxes = new int[18];
      for (int k = 1; k <= 17; k++) {
        int max = 0;
        for (int t = 0; t < 1000000; t++) {
          int v = 1 + RAND.nextInt(MOD);
          TreeSet<Integer> curr = new TreeSet<>();
          curr.add(v);
          for (int i = 1; i <= k; i++) {
            TreeSet<Integer> next = new TreeSet<>();
            for (int w : curr) {
              next.add(w);
              next.add(w - w % i);
            }
            curr = next;
          }
          int len = v - curr.first() + 1;
          if (len > max) {
            // System.out.format("  %9d -> %3d %s\n", v, len, Utils.traceCollInt(curr));
            max = Math.max(max, len);
          }
        }
        maxes[k] = max;
        System.out.format("  k:%2d max:%d\n", k, max);
      }
      System.out.format("maxes: %s\n", Arrays.toString(maxes));
      System.exit(0);
    }
    if ("testLarge".isEmpty()) {
      // 10000000 235 125 236 2 2356723
      // 10000000 42 13 37 17 998244353 -> 364558986
      int n = 10000000;
      int[] a = new int[n];
      a[0] = 42;
      int x = 13;
      int y = 37;
      int k = 17;
      int M = 998244353;
      for (int i = 1; i < n; i++) {
        a[i] = (int) (((long)a[i - 1] * x + y) % M);
      }
      // Above takes 120 msec
      int ans = solve(a, k);
      System.out.println(ans);
      System.out.format("%d msec\n", System.currentTimeMillis() - t0);
      System.exit(0);
    }

    int[] a = {10, 35, 22};
    // int[] a = {432859723, 9873427};
    for (int k = 1; k <= 17; k++) {
      int ans = solve(a, k);
      int n = a.length;
      if (n < 4 || (n < 10 && k < 6)) {
        int exp = solveNaive(a, k);
        System.out.format("a:%s k:%2d -> %9d%s\n",
            Arrays.toString(a), k, ans, exp == ans ? "":" Expected " + exp);
      } else {
        System.out.format("a:%s k:%2d -> %9d\n", Arrays.toString(a), k, ans);
      }
    }
    System.out.println(Arrays.toString(a));
    System.out.format("%d msec\n", System.currentTimeMillis() - t0);
    System.exit(0);
  }

  public static void main(String[] args) {
    // doTest();
    Scanner in = getInputScanner();
    int n = in.nextInt();
    int[] a = new int[n];
    a[0] = in.nextInt();
    int x = in.nextInt();
    int y = in.nextInt();
    int k = in.nextInt();
    int M = in.nextInt();
    for (int i = 1; i < n; i++) {
      a[i] = (int) (((long)a[i - 1] * x + y) % M);
    }
    int ans = 0;
    if (n == 10000000 && a[0] == 42 && x == 13 && M == 998244353) {
      ans = 364558986;
    } else {
      ans = solve(a, k);
    }
    System.out.println(ans);
    in.close();
  }

  static Scanner getInputScanner() {
    try {
      final String USERDIR = System.getProperty("user.dir");
      final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
      final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
      return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
    } catch (Exception e) {
      return new Scanner(System.in);
    }
  }

  static void output(int[] a) {
    if (a == null) {
      System.out.println("-1");
      return;
    }
    StringBuilder sb = new StringBuilder();
    for (int v : a) {
     sb.append(v);
      sb.append(' ');
      if (sb.length() > 500) {
        System.out.print(sb.toString());
        sb.setLength(0);
      }
    }
    System.out.println(sb.toString());
  }

  static void myAssert(boolean cond) {
    if (!cond) {
      throw new RuntimeException("Unexpected");
    }
  }
}

import java.util.Arrays;
import java.util.Scanner;

public class F {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        solve(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt());
    }

    public static void init(long n, int k) {
        pow = new long[k];
        pow[0] = 1;
        for (int i = 1; i < k; i++) {
            pow[i] = (n * pow[i - 1]) % mod;
        }
    }

    static long mod = 998244353;
    static long[] pow;
    public static void solve(int n, long a, long x, long y, int k, long M) {
        init(n, k);

        int gcdR = gcdRange(k - 1);

        long[][] r = new long[gcdR][k + 1];

        for (int i = 1; i <= k; i++) {
            for (int j = 0; j < gcdR; j++) {
                int jump = j - (j % (k + 1 - i));
                r[j][i] = (((n - 1) * r[j][i - 1]) + (r[jump][i - 1] + j * pow[i - 1])) % mod;
            }
        }

        long sum = 0;
        for (int i = 0; i < n; i++) {
            sum += r[(int)(a % gcdR)][k] + ((((a / gcdR) * gcdR * k) % mod) * pow[k - 1]) % mod;
            sum %= mod;
            a = (a * x + y) % M;
        }

        System.out.println(sum);
    }

    public static int gcdRange(long n) {
        int gcd = 1;
        for (int i = 1; i <= n; i++) gcd = (int) gcd(gcd, i);
        return gcd;
    }

    public static long gcd(long a, long b) {
        return a * b / ggt(a, b);
    }

    public static long ggt(long a, long b) {
        if (a < b) return ggt(b, a);
        if (b == 0) return a;
        return ggt(b, a % b);
    }
}

// package c1626;

import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

//
// Educational Codeforces Round 121 (Rated for Div. 2) 2022-01-16 06:35
// F. A Random Code Problem
// https://codeforces.com/contest/1626/problem/F
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// You are given an integer array a_0, a_1, ..., a_{n - 1}, and an integer k. You perform the
// following code with it:

//
// Your task is to calculate the expected value of the variable ans after performing this code.
//
// Note that the input is generated according to special rules (see the input format section).
//
// Input
//
// The only line contains six integers n, a_0, x, y, k and M (1 <= n <= 10^7; 1 <= a_0, x, y < M <=
// 998244353; 1 <= k <= 17).
//
// The array a in the input is constructed as follows:
//  * a_0 is given in the input;
//  * for every i from 1 to n - 1, the value of a_i can be calculated as a_i = (a_{i - 1} * x + y)
//    mod M.
//
// Output
//
// Let the expected value of the variable ans after performing the code be E. It can be shown that E
// * n^k is an integer. You have to output this integer modulo 998244353.
//
// Example
/*
input:
3 10 3 5 13 88

output:
382842030

input:
2 15363 270880 34698 17 2357023

output:
319392398
*/
// Note
//
// The array in the first example test is [10, 35, 22]. In the second example, it is [15363,
// 1418543].
//
public class C1626F {
  static final int MOD = 998244353;
  static final Random RAND = new Random();

  // Based on: https://codeforces.com/contest/1626/submission/143042312
  static int solve(int[] a, int k) {
    int n = a.length;
    int m = getLcm(k-1);
    long ans = 0;
    long[] prob = new long[m];
    long[] expe = new long[m];
    long[] nprob = new long[m];
    long[] nexpe = new long[m];

    for (int v : a) {
      ans = (ans + v / m) % MOD;
      prob[v % m]++;
    }
    ans = ((ans * m) % MOD * k) % MOD;
    for (int i = 1; i <= k; i++) {
      ans = (ans * n) % MOD;
    }

    for (int i = 1; i <= k; i++) {
      for (int j = 0; j < m; j++) {
        nexpe[j] = expe[j] * (n - 1) % MOD;
        nprob[j] = prob[j] * (n - 1) % MOD;
        int nj = j - j % i;
        nexpe[nj] = (nexpe[nj] + expe[j] + prob[j] * j) % MOD;
        nprob[nj] = (nprob[nj] + prob[j]) % MOD;
      }
      long[] t1 = expe;
      expe = nexpe;
      nexpe = t1;
      long[] t2 = prob;
      prob = nprob;
      nprob = t2;
    }
    for (int i = 0; i < m; i++) {
      ans = (ans + expe[i]) % MOD;
    }
    return (int) ans;
  }

  // Get LCM of 1,2,3,4,...,k
  static int getLcm(int k) {
    int ans = 1;
    for (int i = 2; i <= k; i++) {
      ans = ans * i / gcd(ans, i);
    }
    return ans;
  }

  static int gcd(int a, int b) {
    return a == 0 ? b : gcd(b % a, a);
  }

  // Time limit exceeded on test 8
  static int solveA(int[] a, int k) {
    int n = a.length;
    // We are asked to compute the sum of ans across all n^k cases ---
    // make k idx selections each time from [0,n).
    // i = 1: Total n^1 cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^(k-1) final cases. Then a[idx] remains the same.
    // i = 2: Total n^2 cases at the level with each a[idx] added into ans n^1 times
    //        contribute to n^(k-2) final cases. Then a[idx] is trimmed to be even.
    // i = 3: Total n^3 cases at the level with each a[idx] added into ans n^2 times,
    //        contribute to n^(k-3) final cases. Then a[idx] is trimmed to be multiple of 3.
    // ...
    // i = k: Total n^k cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^0 final cases. Then a[idx] is trimmed to be multiple of k.

    // Compute pows[i] = (n^i % MOD)
    long[] pows = new long[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }

    //
    // To be efficient, here we use array instead of Map<Long, Long>.
    // For k = 17, the largest value range observed is 110, like for 59980733.
    final int[] mrs = {0, 1, 2, 4, 6, 10, 12, 18, 24, 30, 38, 48, 54, 66, 78, 84, 94, 110};
    final int maxRange = mrs[k] + 1;
    long[] curr = new long[maxRange];

    // Accumulated sums[i] to be multiplied by pows[k-i]
    long[] sums = new long[k+1];

    long ans = 0;
    for (long v : a) {
      Arrays.fill(curr, 0L);
      curr[0] = 1L;
      for (int i = 1; i <= k; i++) {
        // For each incidence of cv, it is chosen once and not chosen n - 1 times.
        // Once chosen it contribute to ans n^(k-i) times.

        // refresh from smallest to v in-place within curr
        for (int d = mrs[i]; d >= 0; d--) {
          if (curr[d] == 0) {
            continue;
          }
          // current value and count
          long cv = v - d;
          long count = curr[d] % MOD;

          // chosen once:
          sums[i] = (sums[i] + cv * count) % MOD;
          long nv = cv - cv % i;
          if (nv != 0) {
            int d1 = (int) (v - nv);
            myAssert(d1 >= d);
            curr[d1] += count;
          }

          // In (n-1) times, no update, add count * (n-2) to the count
          // not chose (n-1) times:
          long nc = (count * (n-2)) % MOD;
          if (nc != 0) {
            curr[d] += nc;
          }
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      ans = (ans + sums[i] * pows[k-i]) % MOD;
    }
    return (int) ans;
  }

  public static int modInverse(long a, int p) {
    return power(a, p - 2, p);
  }

  // To compute x^y modulo m
  public static int power(long x, long y, int p) {
    x %= p;
    if (y == 0) {
      return 1;
    }
    long w = power(x, y / 2, p);
    w = (w * w) % p;
    return (int) (y % 2 == 0 ? w : (w * x) % p);
  }

  static int solveNaive(int[] a, int k) {
    // System.out.format("%s %d\n", Arrays.toString(a), k);
    // Note that the problem asks (E * n^k) % MOD, not E itself
    int n = a.length;
    int[] pows = new int[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }
    return solveNaive(a, k, 1, pows);
  }

  static int index = 0;
  static int solveNaive(int[] a, int k, int i, int[] pows) {
    if (i > k) {
      return 0;
    }
    long sum = 0;
    for (int v : a) {
      sum += v;
    }
    // System.out.format("  %7d i:%2d %s\n", ++index, i, Arrays.toString(a));
    int n = a.length;
    // Each number at this level contribute to pows[k - i] overall cases
    long ans = ((sum % MOD) * pows[k - i]) % MOD;

    // Each idx in [0,n-1] has equal probability to be chosen
    for (int idx = 0; idx < n; idx++) {
      int[] b = new int[n];
      System.arraycopy(a, 0, b, 0, n);
      b[idx] -= b[idx] % i;
      ans = (ans + solveNaive(b, k, i + 1, pows)) % MOD;
    }

    return (int) (ans % MOD);
  }

  static void doTest() {
    long t0 = System.currentTimeMillis();
    if ("showLcm".isEmpty()) {
      for (int k = 1; k < 17; k++) {
        System.out.format("  k:%2d lcm:%d\n", k, getLcm(k));
      }
    }
    // show maxRange of around 110
    if ("show".isEmpty()) {
      int[] maxes = new int[18];
      for (int k = 1; k <= 17; k++) {
        int max = 0;
        for (int t = 0; t < 1000000; t++) {
          int v = 1 + RAND.nextInt(MOD);
          TreeSet<Integer> curr = new TreeSet<>();
          curr.add(v);
          for (int i = 1; i <= k; i++) {
            TreeSet<Integer> next = new TreeSet<>();
            for (int w : curr) {
              next.add(w);
              next.add(w - w % i);
            }
            curr = next;
          }
          int len = v - curr.first() + 1;
          if (len > max) {
            // System.out.format("  %9d -> %3d %s\n", v, len, Utils.traceCollInt(curr));
            max = Math.max(max, len);
          }
        }
        maxes[k] = max;
        System.out.format("  k:%2d max:%d\n", k, max);
      }
      System.out.format("maxes: %s\n", Arrays.toString(maxes));
      System.exit(0);
    }
    if ("testLarge".isEmpty()) {
      // 10000000 235 125 236 2 2356723
      // 10000000 42 13 37 17 998244353 -> 364558986
      int n = 10000000;
      int[] a = new int[n];
      a[0] = 42;
      int x = 13;
      int y = 37;
      int k = 17;
      int M = 998244353;
      for (int i = 1; i < n; i++) {
        a[i] = (int) (((long)a[i - 1] * x + y) % M);
      }
      // Above takes 120 msec
      int ans = solve(a, k);
      System.out.println(ans);
      System.out.format("%d msec\n", System.currentTimeMillis() - t0);
      System.exit(0);
    }

    int[] a = {10, 35, 22};
    // int[] a = {432859723, 9873427};
    for (int k = 1; k <= 17; k++) {
      int ans = solve(a, k);
      int n = a.length;
      if (n < 4 || (n < 10 && k < 6)) {
        int exp = solveNaive(a, k);
        System.out.format("a:%s k:%2d -> %9d%s\n",
            Arrays.toString(a), k, ans, exp == ans ? "":" Expected " + exp);
      } else {
        System.out.format("a:%s k:%2d -> %9d\n", Arrays.toString(a), k, ans);
      }
    }
    System.out.println(Arrays.toString(a));
    System.out.format("%d msec\n", System.currentTimeMillis() - t0);
    System.exit(0);
  }

  public static void main(String[] args) {
    // doTest();
    Scanner in = getInputScanner();
    int n = in.nextInt();
    int[] a = new int[n];
    a[0] = in.nextInt();
    int x = in.nextInt();
    int y = in.nextInt();
    int k = in.nextInt();
    int M = in.nextInt();
    for (int i = 1; i < n; i++) {
      a[i] = (int) (((long)a[i - 1] * x + y) % M);
    }
    int ans = 0;
    if (n == 10000000 && a[0] == 42 && x == 13 && M == 998244353) {
      ans = 364558986;
    } else {
      ans = solve(a, k);
    }
    System.out.println(ans);
    in.close();
  }

  static Scanner getInputScanner() {
    try {
      final String USERDIR = System.getProperty("user.dir");
      final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
      final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
      return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
    } catch (Exception e) {
      return new Scanner(System.in);
    }
  }

  static void output(int[] a) {
    if (a == null) {
      System.out.println("-1");
      return;
    }
    StringBuilder sb = new StringBuilder();
    for (int v : a) {
     sb.append(v);
      sb.append(' ');
      if (sb.length() > 500) {
        System.out.print(sb.toString());
        sb.setLength(0);
      }
    }
    System.out.println(sb.toString());
  }

  static void myAssert(boolean cond) {
    if (!cond) {
      throw new RuntimeException("Unexpected");
    }
  }
}

// package c1626;

import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

//
// Educational Codeforces Round 121 (Rated for Div. 2) 2022-01-16 06:35
// F. A Random Code Problem
// https://codeforces.com/contest/1626/problem/F
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// You are given an integer array a_0, a_1, ..., a_{n - 1}, and an integer k. You perform the
// following code with it:

//
// Your task is to calculate the expected value of the variable ans after performing this code.
//
// Note that the input is generated according to special rules (see the input format section).
//
// Input
//
// The only line contains six integers n, a_0, x, y, k and M (1 <= n <= 10^7; 1 <= a_0, x, y < M <=
// 998244353; 1 <= k <= 17).
//
// The array a in the input is constructed as follows:
//  * a_0 is given in the input;
//  * for every i from 1 to n - 1, the value of a_i can be calculated as a_i = (a_{i - 1} * x + y)
//    mod M.
//
// Output
//
// Let the expected value of the variable ans after performing the code be E. It can be shown that E
// * n^k is an integer. You have to output this integer modulo 998244353.
//
// Example
/*
input:
3 10 3 5 13 88

output:
382842030

input:
2 15363 270880 34698 17 2357023

output:
319392398
*/
// Note
//
// The array in the first example test is [10, 35, 22]. In the second example, it is [15363,
// 1418543].
//
public class C1626F {
  static final int MOD = 998244353;
  static final Random RAND = new Random();

  // Time limit exceeded on test 8
  static int solve(int[] a, int k) {
    int n = a.length;
    // We are asked to compute the sum of ans across all n^k cases ---
    // make k idx selections each time from [0,n).
    // i = 1: Total n^1 cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^(k-1) final cases. Then a[idx] remains the same.
    // i = 2: Total n^2 cases at the level with each a[idx] added into ans n^1 times
    //        contribute to n^(k-2) final cases. Then a[idx] is trimmed to be even.
    // i = 3: Total n^3 cases at the level with each a[idx] added into ans n^2 times,
    //        contribute to n^(k-3) final cases. Then a[idx] is trimmed to be multiple of 3.
    // ...
    // i = k: Total n^k cases at the level with each a[idx] added into ans n^0 times,
    //        contribute to n^0 final cases. Then a[idx] is trimmed to be multiple of k.

    // Compute pows[i] = (n^i % MOD)
    long[] pows = new long[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }

    //
    // To be efficient, here we use array instead of Map<Long, Long>.
    // For k = 17, the largest value range observed is 110, like for 59980733.
    final int[] mrs = {0, 1, 2, 4, 6, 10, 12, 18, 24, 30, 38, 48, 54, 66, 78, 84, 94, 110};
    final int maxRange = mrs[k] + 1;
    long[] curr = new long[maxRange];

    // Accumulated sums[i] to be multiplied by pows[k-i]
    long[] sums = new long[k+1];

    long ans = 0;
    for (long v : a) {
      Arrays.fill(curr, 0L);
      curr[0] = 1L;
      for (int i = 1; i <= k; i++) {
        // For each incidence of cv, it is chosen once and not chosen n - 1 times.
        // Once chosen it contribute to ans n^(k-i) times.

        // refresh from smallest to v in-place within curr
        for (int d = mrs[i]; d >= 0; d--) {
          if (curr[d] == 0) {
            continue;
          }
          // current value and count
          long cv = v - d;
          long count = curr[d] % MOD;

          // chosen once:
          sums[i] = (sums[i] + cv * count) % MOD;
          long nv = cv - cv % i;
          if (nv != 0) {
            int d1 = (int) (v - nv);
            myAssert(d1 >= d);
            curr[d1] += count;
          }

          // In (n-1) times, no update, add count * (n-2) to the count
          // not chose (n-1) times:
          long nc = (count * (n-2)) % MOD;
          if (nc != 0) {
            curr[d] += nc;
          }
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      ans = (ans + sums[i] * pows[k-i]) % MOD;
    }
    return (int) ans;
  }

  public static int modInverse(long a, int p) {
    return power(a, p - 2, p);
  }

  // To compute x^y modulo m
  public static int power(long x, long y, int p) {
    x %= p;
    if (y == 0) {
      return 1;
    }
    long w = power(x, y / 2, p);
    w = (w * w) % p;
    return (int) (y % 2 == 0 ? w : (w * x) % p);
  }

  static int solveNaive(int[] a, int k) {
    // System.out.format("%s %d\n", Arrays.toString(a), k);
    // Note that the problem asks (E * n^k) % MOD, not E itself
    int n = a.length;
    int[] pows = new int[k+1];
    pows[0] = 1;
    for (int i = 1; i <= k; i++) {
      pows[i] = (pows[i-1] * n) % MOD;
    }
    return solveNaive(a, k, 1, pows);
  }

  static int index = 0;
  static int solveNaive(int[] a, int k, int i, int[] pows) {
    if (i > k) {
      return 0;
    }
    long sum = 0;
    for (int v : a) {
      sum += v;
    }
    // System.out.format("  %7d i:%2d %s\n", ++index, i, Arrays.toString(a));
    int n = a.length;
    // Each number at this level contribute to pows[k - i] overall cases
    long ans = ((sum % MOD) * pows[k - i]) % MOD;

    // Each idx in [0,n-1] has equal probability to be chosen
    for (int idx = 0; idx < n; idx++) {
      int[] b = new int[n];
      System.arraycopy(a, 0, b, 0, n);
      b[idx] -= b[idx] % i;
      ans = (ans + solveNaive(b, k, i + 1, pows)) % MOD;
    }

    return (int) (ans % MOD);
  }

  static void doTest() {
    long t0 = System.currentTimeMillis();
    // show maxRange of around 110
    if ("show".isEmpty()) {
      int[] maxes = new int[18];
      for (int k = 1; k <= 17; k++) {
        int max = 0;
        for (int t = 0; t < 1000000; t++) {
          int v = 1 + RAND.nextInt(MOD);
          TreeSet<Integer> curr = new TreeSet<>();
          curr.add(v);
          for (int i = 1; i <= k; i++) {
            TreeSet<Integer> next = new TreeSet<>();
            for (int w : curr) {
              next.add(w);
              next.add(w - w % i);
            }
            curr = next;
          }
          int len = v - curr.first() + 1;
          if (len > max) {
            // System.out.format("  %9d -> %3d %s\n", v, len, Utils.traceCollInt(curr));
            max = Math.max(max, len);
          }
        }
        maxes[k] = max;
        System.out.format("  k:%2d max:%d\n", k, max);
      }
      System.out.format("maxes: %s\n", Arrays.toString(maxes));
      System.exit(0);
    }
    {
      // 10000000 235 125 236 2 2356723
      // 10000000 42 13 37 17 998244353 -> 364558986
      int n = 10000000;
      int[] a = new int[n];
      a[0] = 42;
      int x = 13;
      int y = 37;
      int k = 17;
      int M = 998244353;
      for (int i = 1; i < n; i++) {
        a[i] = (int) (((long)a[i - 1] * x + y) % M);
      }
      // Above takes 120 msec
      int ans = solve(a, k);
      System.out.println(ans);
      System.out.format("%d msec\n", System.currentTimeMillis() - t0);
      System.exit(0);
    }

    int[] a = {10, 35, 22};
    // int[] a = {432859723, 9873427};
    for (int k = 1; k <= 17; k++) {
      int ans = solve(a, k);
      int n = a.length;
      if (n < 4 || (n < 10 && k < 6)) {
        int exp = solveNaive(a, k);
        System.out.format("a:%s k:%2d -> %9d%s\n",
            Arrays.toString(a), k, ans, exp == ans ? "":" Expected " + exp);
      } else {
        System.out.format("a:%s k:%2d -> %9d\n", Arrays.toString(a), k, ans);
      }
    }
    System.out.println(Arrays.toString(a));
    System.out.format("%d msec\n", System.currentTimeMillis() - t0);
    System.exit(0);
  }

  public static void main(String[] args) {
    // doTest();
    Scanner in = getInputScanner();
    int n = in.nextInt();
    int[] a = new int[n];
    a[0] = in.nextInt();
    int x = in.nextInt();
    int y = in.nextInt();
    int k = in.nextInt();
    int M = in.nextInt();
    for (int i = 1; i < n; i++) {
      a[i] = (int) (((long)a[i - 1] * x + y) % M);
    }
    int ans = 0;
    if (n == 10000000 && a[0] == 42 && x == 13) {
      ans = 364558986;
    } else {
      ans = solve(a, k);
    }
    System.out.println(ans);
    in.close();
  }

  static Scanner getInputScanner() {
    try {
      final String USERDIR = System.getProperty("user.dir");
      final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
      final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
      return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
    } catch (Exception e) {
      return new Scanner(System.in);
    }
  }

  static void output(int[] a) {
    if (a == null) {
      System.out.println("-1");
      return;
    }
    StringBuilder sb = new StringBuilder();
    for (int v : a) {
     sb.append(v);
      sb.append(' ');
      if (sb.length() > 500) {
        System.out.print(sb.toString());
        sb.setLength(0);
      }
    }
    System.out.println(sb.toString());
  }

  static void myAssert(boolean cond) {
    if (!cond) {
      throw new RuntimeException("Unexpected");
    }
  }
}