#include<bits/stdc++.h>

#define N 5000007
#define M 200007
#define ll long long

const ll Mod=1000000007;
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

ll n,m,seed,base,tem;
ll num1,num2,ans;
ll a[N],t[N],hh[N];
ll p[M],k[M],b[M],w[M];
ll T1[N],T2[N],c1,c2;

ll rnd()
{
	int ret=seed;
	seed=1ll*(1ll*seed*base+233)%1000000007;
	return ret;
}
int main()
{
	n=read(),m=read();
	num1=num2=0;
	for (int i=1;i<=m;i++)
		p[i]=read(),k[i]=read(),b[i]=read(),w[i]=read();
	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;
			hh[j]=a[j];
			if (t[j]==1) num1+=a[j];
			else num2+=a[j];
		}
	}
//	cout<<num1<<" "<<num2<<endl; 
	//for (int i=1;i<=n;i++)
	//	cout<<a[i]<<" "<<t[i]<<endl;
/*	for (int i=1;i<=n;i++)
		a[i]=hh[i]=read();
	for (int i=1;i<=n;i++)
		t[i]=read();
	for (int i=1;i<=n;i++)
		if (t[i]==1) num1+=a[i];
		else num2+=a[i];*/
	if (num1==0||num2==0)
	{
		ans=1;
		if (num1!=0)
		{
			ans=1ll*ans*(((1)^(1ll*1*1))%Mod+1)%Mod;
			for (int i=2;i<=n;i++)
				ans=1ll*ans*(((0)^(1ll*i*i))%Mod+1)%Mod;
			printf("%lld\n",ans);
		}
		else
		{
			for (int i=1;i<=n;i++)
				ans=1ll*ans*(((0)^(1ll*i*i))%Mod+1)%Mod;
			printf("%lld\n",ans);
		}
	}
	else
	{
		if (num1>num2) tem=2;
		else tem=1;
	
	//	cout<<tem<<endl;
	
		/*if (t[1]!=tem)
		{
			if (tem==1&&hh[1]>=num1)
			{
				hh[1]-=num1;
				for (int i=1;i<=n;i++)
					if (t[i]==tem) hh[i]=0;
			}
			else if (tem==2&&hh[1]>=num2)
			{
				hh[1]-=num2;
				for (int i=1;i<=n;i++)
					if (t[i]==tem) hh[i]=0;
			}
			else
			{
				int z=2;
				while(hh[1])
				{
					if (t[z]!=t[1])
					{
						if (hh[1]>hh[z]) hh[1]-=hh[z],hh[z]=0;
						else hh[z]-=hh[1],hh[1]=0;
					}
					z++; 
				}
			}
		}*/
		//cout<<tem<<" "<<num1<<" "<<num2<<endl;
		int z=1;ll num=0;
		if (t[1]!=tem)
		{
			hh[1]--;
			while(t[z]!=tem)z++; 
		}
	//	for (int i=1;i<=z;i++)
	//		cout<<z<<" "<<t[i]<<" "<<hh[i]<<endl;
		for (int i=z;i<=n<<1;i++)
		{
			int p=(i>n)?i%n:i;
			if (t[p]==tem) num+=hh[p],hh[p]=0;
			else
			{
				if (hh[p]>num) hh[p]-=num,num=0;
				else num-=hh[p],hh[p]=0;
			}
		//	cout<<num<<" "<<hh[p]<<endl;
		}
		
		ans=1;
		for (int i=1;i<=n;i++)
			ans=1ll*ans*(((a[i]-hh[i])^(1ll*i*i))%Mod+1)%Mod;
		printf("%lld\n",ans);
	}
}
