#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5000005,P = 1000000007;
int n,t[N],a[N],aa[N],ans[N];
void gen();
ll sum[3];
int main ()
{
	ios::sync_with_stdio(false);
	cin >> n;gen();
	for (int i = 1;i <= n;i++) sum[t[i]] += a[i],aa[i] = a[i];
	if (sum[1] > sum[2])
	{
		swap(sum[1],sum[2]);
		for (int i = 1;i <= n;i++) t[i] = 3 - t[i];
	}
	ll cur = t[1] == 2;// 开局要多拿一个
	for (int i = 1;i <= n;i++)
		if (t[i] == 1) cur += a[i],a[i] = 0;
		else { int mn = min(a[i] * 1ll,cur);cur -= mn;a[i] -= mn; }
	for (int i = 1;i <= n;i++)
	{
		int mn = min(a[i] * 1ll,cur);cur -= mn;a[i] -= mn;
	}
	for (int i = 1;i <= n;i++) ans[i] = aa[i] - a[i];
	ll out = 1;
	for (int i = 1;i <= n;i++) out = out * ((ans[i] ^ (i * 1ll * i)) % P + 1) % P;
	cout << out << endl;
	return 0;
}
void gen()
{
	const int N = 200005;
	int m;cin >> m;
	static ll seed = 0,base = 0;
	auto rnd = [&]() {int ret = seed;seed = (seed * base + 233) % 1000000007;return ret;};
	static int p[N],kk[N],b[N],w[N];
	for (int i = 1;i <= m;i++)
	{
		cin >> p[i] >> kk[i] >> b[i] >> w[i];
		seed = b[i];base = w[i];
		for (int j = p[i - 1] + 1;j <= p[i];j++)
			t[j] = rnd() % 2 + 1,a[j] = rnd() % kk[i] + 1;
	}
}