#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int N=5e6,M=2e5,mod=1e9+7;
int n,m;
int a[N+1],t[N+1];
long long sum[2];
int r[N+1];
int res;
void read(int &x)
{
	char c;
	x=0;
	do {
		c=getchar();
	} while(isspace(c));
	do {
		x=x*10+c-'0';
		c=getchar();
	} while(isdigit(c));
	return;
}
template<typename ...T>
void read(int &x,T &...t)
{
	read(x),read(t...);
	return;
}
void generateAandT()
{
	static int p[M+1],k[M+1],b[M+1],w[M+1];
	static int seed=0,base=0;
	auto rnd=[]()->int {
		int r=seed;
		seed=(1ll*seed*base+233)%mod;
		return r;
	};
	for(int i=1;i<=m;++i) {
		read(p[i],k[i],b[i],w[i]);
	}
	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,a[j]=rnd()%k[i]+1;
		}
	}
	return;
}
int main()
{
	read(n,m);
	generateAandT();
	copy_n(a+1,n,r+1);
	for(int i=1;i<=n;++i) {
		sum[t[i]]+=a[i];
	}
	if(sum[0]>sum[1]) {
		swap(sum[0],sum[1]);
		for(int i=1;i<=n;++i) {
			t[i]^=1;
		}
	}
	long long s=t[1];
	for(int i=1;i<=n;++i) {
		if(t[i]==0) {
			s+=r[i],r[i]=0;
		} else {
			int v=min<long long>(s,r[i]);
			s-=v,r[i]-=v;
		}
	}
	for(int i=1;i<=n;++i) {
		if(t[i]==1) {
			int v=min<long long>(s,r[i]);
			s-=v,r[i]-=v;
		}
	}
	res=1;
	for(int i=1;i<=n;++i) {
		res=1ll*res*((a[i]-r[i]^1ll*i*i)%mod+1)%mod;
	}
	printf("%d\n",res);
	return 0;
}
