#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<queue>
#include<vector>
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define mod 1000000007
#define LL long long
using namespace std;
inline int read(){
	int f=1,ans=0; char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return f*ans;
}
const int MAXN=5e6+11;
const int MAXM=2e5+11;
int col[MAXN],ret,seed,base,A[MAXN],B[MAXN],N,M,p[MAXM],k[MAXM],b[MAXM],w[MAXM];
int rnd(){
	ret=seed,seed=(LL)((LL)seed*base+233)%mod; return ret;
}
signed main(){
	//freopen("A.in","r",stdin);
	N=read(),M=read(); for(int i=1;i<=M;i++) p[i]=read(),k[i]=read(),b[i]=read(),w[i]=read();
	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++) col[j]=(rnd()%2)+1,B[j]=A[j]=(rnd()%k[i])+1;}
	int res1=0,res2=0;
	for(int i=1;i<=N;i++) if(col[i]==1) res1+=A[i];else res2+=A[i];
	int ps=0; if(res1!=res2){
		if(res1<res2) ps=1; else ps=2;
	}else ps=col[1];
	int be=0,res=(ps==1)?res1:res2;
	//for(int i=1;i<=N;i++) cerr<<col[i]<<" ";cerr<<endl;
	//for(int i=1;i<=N;i++) cerr<<A[i]<<" ";cerr<<endl;
	int cnt=0; 
	if(col[1]==ps) be=1;
	else {A[1]--; for(int i=1;i<=N;i++) if(col[i]==ps){be=i;break;}}
	//for(int i=1;i<=N;i++) cerr<<A[i]<<" ";cerr<<endl;
	while(res||cnt){
		if(A[be]){
			if(col[be]==ps){cnt+=A[be];res-=A[be];A[be]=0;}
			else{
				int w=min(A[be],cnt); A[be]-=w;
				cnt-=w;
			}
		} be++; if(be==N+1) be=1;
	}
	LL Ans=1;
	//for(int i=1;i<=N;i++) cerr<<B[i]-A[i]<<" ";cerr<<"\n";
	for(int i=1;i<=N;i++) Ans*=(LL)((LL)((B[i]-A[i])^(LL)(i*i))+1)%mod,Ans%=mod;
	printf("%lld\n",Ans); return 0;
}