#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int seed = 0;
int base = 0;

int rnd()
{
	int ret = seed;
	seed = (seed * base + 233) %mod;
	return ret;
}

int n,m,p[200001],k[200001],b[200001],w[200001],a[5000001],t[5000001],ans[5000001],anss=1,cntw,cntb,a0[5000001];
signed main()
{
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;i++)scanf("%lld%lld%lld%lld",&p[i],&k[i],&b[i],&w[i]);
	p[0] = 0;
	for(int i=1;i<=m;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() % k[i]) + 1;
		}
	}
	if(t[1]==2)for(int i=1;i<=n;i++)t[i]=3-t[i];
	for(int i=1;i<=n;i++)if(t[i]==1)cntw+=a[i];
	else cntb+=a[i];
	memcpy(a0,a,sizeof(a));
	if(cntw>cntb)
	{
		int tmp(0);
		--a[1];
		for(int i=1;i<=n;i++)if(t[i]==2)tmp+=a[i],a[i]=0;
		else if(tmp>=a[i])tmp-=a[i],a[i]=0;
		else a[i]-=tmp,tmp=0;
		for(int i=1;i<=n;i++)if(t[i]==2)tmp+=a[i],a[i]=0;
		else if(tmp>=a[i])tmp-=a[i],a[i]=0;
		else a[i]-=tmp,tmp=0;
		memcpy(ans,a,sizeof(a));
	}
	else
	{
		int tmp(0);
		for(int i=1;i<=n;i++)if(t[i]==1)tmp+=a[i],a[i]=0;
		else if(tmp>=a[i])tmp-=a[i],a[i]=0;
		else a[i]-=tmp,tmp=0;
		for(int i=1;i<=n;i++)if(t[i]==1)tmp+=a[i],a[i]=0;
		else if(tmp>=a[i])tmp-=a[i],a[i]=0;
		else a[i]-=tmp,tmp=0;
		memcpy(ans,a,sizeof(a));
	}
//	for(int i=1;i<=n;i++)printf("%d ",a0[i]-ans[i]);
	for(int i=1;i<=n;i++)(anss*=(((a0[i]-ans[i])^(i*i))+1)%mod)%=mod;
	printf("%lld",anss);
	return 0;
}