#include <map>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <algorithm>
#define pii pair<int, int>
#define mp make_pair
#define int long long
using namespace std;
 
inline int read() {
	int x = 0, w = 1;char ch = getchar();
	while (ch > '9' || ch < '0') { if (ch == '-')w = -1;ch = getchar(); }
	while (ch >= '0' && ch <= '9')x = x * 10 + ch - '0', ch = getchar();
	return x * w;
}
inline void write(int x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
 
const int maxn = 5e6 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f;
 
inline int min(int x, int y) { return x < y ? x : y; }
inline int max(int x, int y) { return x > y ? x : y; }

int n, m, a[maxn], org[maxn], t[maxn], sum[2];

int last, seed, base;
inline int rnd() {
	int ret = seed;
	seed = (seed * base + 233) % mod;
	return ret;
}

signed main(void) {
	n = read(), m = read();
	int lastp = 0;
	for(int i = 1, p, k, b, w;i <= m;i++) {
		p = read(), k = read(), b = read(), w = read();
		seed = b, base = w;
		for(int j = lastp;j < p ;j++) {
			t[j] = rnd() % 2;
			sum[t[j]] += (org[j] = a[j] = rnd() % k + 1);
		}
		lastp = p;
	}
	int st = 0;
	if (sum[t[0]] > sum[!t[0]]) {
		sum[t[0]]--, a[0]--;
		while (t[0] == t[st] && st < n) st++;
	}
	int now = st, cnt = 0;
	if (st != n) {
		while (sum[t[st]]) {
			if (t[now] == t[st]) {
				cnt += a[now], a[now] = 0;
			} else {
				int tmp = min(a[now], cnt);
				sum[t[st]] -= tmp, a[now] -= tmp, cnt -= tmp;
			}
			now = (now + 1 == n ? 0 : now + 1);
		}
	}
	int ans = 1;
	for (int i = 0;i < n;i++) {
		int qwq = (((org[i] - a[i]) ^ ((i + 1) * (i + 1))) + 1) % mod;
		ans = (ans * qwq) % mod;
	}
	printf("%lld\n", ans);
    return 0;
}