#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;

template<typename T = LL>
struct FenwickTree
{
	vector<T> x;
	FenwickTree(size_t n, const T& v = T()) : x(n, v) {}

	void incr( int k, const T& a ) { // z[k] += a;
		for(; k < x.size(); k|=k+1)
			x[k] += a;
	}

	T sum(int i, int j) { // z[i]+...+z[j] : inclusive
		if(i)
			return sum(0, j) - sum(0, i-1);
		else {
			T v = T();
			for(; j>=0; j=(j&(j+1))-1)
				v += x[j];
			return v;
		}
	}
};

static const int MODVAL = 1000000007;

class ProductOfPrices
{
public:
	int product(int N, int L, int X0, int A, int B) 
	{
		LL result = 1;

		FenwickTree<> fw(L), nm(L);

		LL X = X0 % L;
		fw.incr(X, X);
		nm.incr(X, 1);
		for(int i=1; i<N; ++i)
		{
			X = (X*A+B) % L;

			LL sumL = fw.sum(0, X-1);
			LL sumR = fw.sum(X, L-1);
			LL numL = nm.sum(0, X-1);
			LL numR = nm.sum(X, L-1);

			LL price = ((X*numL - sumL) + (sumR - X*numR)) % MODVAL;
			result = (result * price) % MODVAL;

			fw.incr(X, X);
			nm.incr(X, 1);
		}
		return result;
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arg0 = 5; int Arg1 = 10; int Arg2 = 3; int Arg3 = 1; int Arg4 = 1; int Arg5 = 180; verify_case(0, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); }
	void test_case_1() { int Arg0 = 3; int Arg1 = 20; int Arg2 = 5; int Arg3 = 2; int Arg4 = 3; int Arg5 = 64; verify_case(1, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); }
	void test_case_2() { int Arg0 = 4; int Arg1 = 21; int Arg2 = 1; int Arg3 = 7; int Arg4 = 1; int Arg5 = 3087; verify_case(2, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); }
	void test_case_3() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 4; int Arg3 = 37; int Arg4 = 11; int Arg5 = 591860767; verify_case(3, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); }
	void test_case_4() { int Arg0 = 5; int Arg1 = 200000; int Arg2 = 999999999; int Arg3 = 123456789; int Arg4 = 987654321; int Arg5 = 499739175; verify_case(4, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() { ProductOfPrices().run_test(-1); }
// END CUT HERE
