public class BagAndCards {
int N;
final long mod = 1000000007;
long power(long a, long b)
{
long ret = 1;
while(b > 0)
{
if(b % 2 == 1)
ret = (ret * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ret;
}
long inv(long x)
{
return power(x, mod - 2);
}
class poly {
long[] c;
poly()
{
c = new long[N+1];
}
long calcAt(long x)
{
long ret = 0, t = 1;
for(int i = 0; i <= N; i++)
{
ret += t * c[i];
ret %= mod;
t = (t * x) % mod;
}
return ret;
}
void multiByXPlusB(long b)
{
for(int i = N; i >= 0; i--)
{
c[i] = c[i] * b + (i == 0 ? 0 : c[i-1]);
c[i] %= mod; c[i] += mod; c[i] %= mod;
}
}
void divideByXPlusB(long b)
{
for(int i = N; i >= 1; i--)
{
c[i-1] -= c[i] * b;
c[i-1] %= mod; c[i-1] += mod; c[i-1] %= mod;
}
for(int i = 0; i < N; i++)
c[i] = c[i+1];
c[N] = 0;
}
poly add(poly that)
{
poly ret = new poly();
for(int i = 0; i <= N; i++)
ret.c[i] = (c[i] + that.c[i]) % mod;
return ret;
}
poly mul(long t)
{
poly ret = new poly();
for(int i = 0; i <= N; i++)
ret.c[i] = (c[i] * t) % mod;
return ret;
}
};
long[][] f;
long[][] coef;
long[] coefPrime;
public int getHash
(int n,
int m,
int x,
int a,
int b,
int c,
String isGood
) {
N = 2 * m;
f = new long[n][N];
coef = new long[N][N];
coefPrime = new long[N];
for(int i = 0; i < n; i++)
{
poly t = new poly();
for(int j = 0; j < m; j++)
{
t.c[j] = x;
x = (int)(((1L * x * a + b) ^ c) % mod);
}
for(int j = 0; j < N; j++)
f[i][j] = t.calcAt(j);
}
// Lagrange Interpolation
poly prod = new poly();
prod.c[0] = 1;
for(int i = 0; i < N; i++)
prod.multiByXPlusB(-i);
for(int i = 0; i < N; i++)
{
prod.divideByXPlusB(-i);
long mul = 1;
for(int j = 0; j < N; j++)
if(i != j)
{
mul = (mul * (i - j));
mul %= mod; mul += mod; mul %= mod;
}
mul = inv(mul);
for(int j = 0; j < N; j++)
{
coef[j][i] = (coef[j][i] + prod.c[j] * mul) % mod;
if(j < isGood.length() && isGood.charAt(j) == 'Y')
{
coefPrime[i] += prod.c[j] * mul;
coefPrime[i] %= mod;
}
}
prod.multiByXPlusB(-i);
}
long xors = 0;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
{
long s = 0;
for(int k = 0; k < N; k++)
{
s += (((coefPrime[k] * f[i][k]) % mod) * f[j][k]) % mod;
s %= mod;
}
xors ^= s;
}
return (int)xors;
}
}