#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
    sort(v.Begin(),v.end());
    v.erase(unique(v.Begin(),v.end()),v.end());
}
template<class T>
void print(T x,int suc=1)
{
    cout<<x;
    if(suc==1) cout<<'\n';
    else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
    for(int i=0;i<v.size();i++)
        print(v[i],i==(int)(v.size())-1?suc:2);
}
const int N = 5e6 + 7;
const int mod = 1e9 + 7;
int a[N], team[N], Begin[N];
int n ,m;
ll sum[2];
 
int seed, base;
int rnd(){
	int ret = seed;
	seed = (1LL * seed * base + 233) % mod;
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n >> m;
	int pre = 0;
	for(int i = 1, p, k, b, w; i <= m; ++i){
		cin >> p >> k >> b >> w;
		seed = b, base = w;
		for(int j = pre; j < p; ++j){
			team[j] = rnd() % 2;
			sum[team[j]] += (Begin[j] = a[j] = rnd() % k + 1);
		}
		pre = p;
	}
	int hh = 0;
	ll ans = 1;
	if(sum[team[0]] > sum[team[0] ^ 1]) {
		while(team[0] == team[hh] && hh < n) ++hh;
		a[0]--, sum[team[0]]--;
		int now = hh;
		ll s = 0;
		if(hh != n) {
			while(sum[team[hh]]) {
				if(team[now] == team[hh]) {
					s += a[now];
					a[now] = 0;
				}
				else {
					ll mi = min((ll)a[now], s);
					sum[team[hh]] -= mi;
					s -= mi;
					a[now] -= mi;
				}
				if(now + 1 == n) now = 0;
				else now += 1;
			}
		}
		for(int i = 0 ; i < n ; ++i){
			ll res = (((Begin[i] - a[i]) ^ (1ll * (i + 1) * (i + 1))) + 1) % mod;
			ans = ans * res % mod;
		}
	}	
	else {
		int now = 0;
		ll s = 0;
		while(sum[team[hh]]) {
			if(team[now] == team[hh]) {
				s += a[now];
				a[now] = 0;
			}
			else {
				ll mi = min((ll)a[now], s);
				sum[team[hh]] -= mi;
				s -= mi;
				a[now] -= mi; 
			}
			if(now + 1 == n) now = 0;
			else now += 1;
		}
		for(int i = 0 ; i < n ; ++i){
			ll res = (((Begin[i] - a[i]) ^ (1ll * (i + 1) * (i + 1))) + 1) % mod;
			ans = ans * res % mod;
		}
	}
	print(ans);
}