#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e6+5,mod=1e9+7;
int n,m;
int p[N],t[N],a[N],b[N];
ll s[2];
int seed,base;
inline int rnd(){
	int ret=seed;
	seed=(1ll*seed*base+233)%mod;
	return ret;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,k;i<=m;i++){
		scanf("%d%d%d%d",&p[i],&k,&seed,&base);
		for(int j=p[i-1]+1;j<=p[i];j++){
			t[j]=rnd()%2,a[j]=rnd()%k+1;b[j]=a[j];
			s[t[j]]+=a[j];
		}
	}
	int nw=1;
	if(s[t[1]]>s[t[1]^1]){
		s[t[1]]--,a[1]--;
		while(t[1]==t[nw]&&nw<=n) nw++;
	}
	if(nw<=n){
		int c=t[nw];ll cnt=0;
		while(s[c]){
			if(t[nw]==c) cnt+=a[nw],a[nw]=0;
			else {
				ll v=min(cnt,(ll)a[nw]);
				s[c]-=v,cnt-=v,a[nw]-=v;
			}
			nw++;if(nw>n) nw=1;
		}
	}
	
	ll ans=1;
	for(int i=1;i<=n;i++){
		ans=1ll*(((b[i]-a[i])^(1ll*i*i))+1)%mod*ans%mod;
	}
	printf("%lld",ans);
	return 0;
}