#include <cstdio>
#include <algorithm>

#define _P 1000000007

#define _M 200010
#define _N 5000010

#define int long long

struct Mod {
	int val;
	Mod() {
		val = 0;
	}
	Mod(long long x) {
		val = x >= 0 ? x % _P : x % _P + _P;
	}
};

Mod inline operator +(const Mod& left, const Mod& right) {
	return left.val + right.val;
}
Mod inline operator *(const Mod& left, const Mod& right) {
	return 1ll * left.val * right.val;
}

int n, m;

int p[_M], k[_M], b[_M], w[_M];
int t[_N], a[_N];

long long ans[_N];

Mod seed, base;

int sum[2];

int inline rnd() {
	int res = seed.val;
	seed = seed * base + 233;
	return res;
}

signed main() {
	scanf("%lld", &n);
	scanf("%lld", &m);
	for (int i = 1; i <= m; i++) {
		scanf("%lld%lld%lld%lld", &p[i], &k[i], &b[i], &w[i]);
		seed = b[i], base = w[i];
		for (int j = p[i - 1] + 1; j <= p[i]; j++) {
			t[j] = rnd() & 1;
			a[j] = (rnd() % k[i]) + 1;
		}
	}
	for (int i = 1; i <= n; i++) {
		sum[t[i]] += a[i];
	}
	if (sum[0] != 0 && sum[1] != 0) {
		int less = sum[0] <= sum[1] ? 0 : 1;
		for (int i = 1; i <= n; i++) {
			if (t[i] == less) {
				ans[i] = a[i];
			}
		}
		if (t[1] != less) {
			for (int i = 1; i <= n; i++) {
				if (t[i] == less) {
					a[1]--;
					ans[1]++;
					break;
				}
			}
		}
		int cnt = 0;
		for (int j = 0; j < 3; j++) {
			for (int i = 1; i <= n; i++) {
				if (t[i] == less) {
					cnt = cnt + a[i];
					a[i] = 0;
				} else {
					int mn = std::min(cnt, a[i]);
					cnt -= mn, a[i] -= mn;
					ans[i] = ans[i] + mn;
				}
			}
		}
	} else {
		ans[1] = 1;
	}
	Mod fians = 1;
	for (int i = 1; i <= n; i++) {
		fians = fians * ((ans[i] ^ (i * i)) + 1);
	}
	printf("%lld\n", fians.val);
	return 0;
}
