#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define MAXN 5000100
#define ll long long
const ll mod = 1000000007;
ll n, m, cnt, a[MAXN], p, b, k, w, last, t[MAXN], ans[MAXN], sum[MAXN];
ll seed = 0, ret;
ll base = 0;

ll rnd()
{
	ret = seed;
	seed = (seed * base + 233) % 1000000007;
	return ret;
}
int main()
{
	scanf("%lld%lld",&n,&m);
	ll u;
	for(ll i=1;i<=m;i++)
	{
		scanf("%lld%lld%lld%lld",&p,&k,&b,&w);
		seed = b;
		base = w;
		for(ll j=last+1;j<=p;j++)
		{
			t[j] = (rnd() % 2);
			a[j] = rnd()%k + 1;
		}
		last = p;
	}
	for(ll i=1;i<=n;i++) sum[t[i]] += a[i];
	if(sum[1] < sum[0])
	{
		for(ll i=1;i<=n;i++) t[i] = 1-t[i];
	}
	cnt = 0;
	if(t[1] == 1)
	{
		ans[1]++;
		a[1]--;
	}
	for(ll i=1;i<=n;i++)
	{
		if(t[i] == 0)
		{
			cnt += a[i];
			ans[i] += a[i];
			a[i] = 0;
		}
		else
		{
			u = min(cnt, a[i]);
			cnt -= u; a[i] -= u; ans[i] += u;
		}
	}
	for(ll i=1;i<=n;i++)
	{
		if(t[i] != 0)
		{
			u = min(cnt, a[i]);
			cnt -= u; a[i] -= u; ans[i] += u;
		}
	}
	ll ans2 = 1;
	for(ll i=1;i<=n;i++) ans2 = ans2 * (((ans[i] ^ (i*i)) + 1) % mod) % mod;//, cout<<ans[i]<<" ";
//	cout<<endl;
	cout<<ans2<<endl;
	
	return 0;
}