#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include <cmath>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <functional>
//#include <intrin.h>
#include <stack>
//#include <immintrin.h>


using namespace std;
//typedef long long ll;
#define ll long long
#define ld long double


#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)>(y)?(x):(y))
//#define PI 3.14159265358979323846
#define PI M_PI;
#define ABS(a) ((a)<0?-(a):(a))
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long MAX = 1000000000;
const long long LLONG = 9223372036854775807;
const int N = 5000000;
const double EPS = 0.000000001;
const int sqsz = 5000;
template <typename T> inline int sgn(const T& val) { return (T(0) < val) - (val < T(0)); }
inline unsigned long long hsh(int x, int y) { return (((unsigned long long)x) << 32) + y; }
template <typename T> inline T phsh(T a, T b, int p) { return (a << p) + b; }
template <typename T> inline T gcd(T a, T b) {
    while (b) { a %= b; swap(a, b); }
    return a;
}
inline void fastio() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
inline void releaseRedirect() {
#ifndef _DEBUG
    freopen("quarth.in", "r", stdin);
    freopen("quarth.out", "w", stdout);
#endif // !_DEBUG
}
//inline int zerotrail(int a) {
//    return __popcnt(a - 1) - __popcnt(a) + 1;
//}
//reverse pair sort: [](pair<int, int> a, pair<int, int>b) {return a.first > b.first || ((a.first == b.first) && (a.second < b.second)); }
const uint64_t m1 = 0x5555555555555555;
const uint64_t m2 = 0x3333333333333333;
const uint64_t m4 = 0x0f0f0f0f0f0f0f0f;
int popcount64(uint64_t x)
{
    x -= (x >> 1) & m1;
    x = (x & m2) + ((x >> 2) & m2);
    x = (x + (x >> 4)) & m4;
    x += x >> 8;
    x += x >> 16;
    x += x >> 32;
    return x & 0x7f;
}
long long myNpow(long long a, long long n, long long mod)
{
    long long mn = a;
    ll res = 1;
    while (n)
    {
        if (n & 1)
        {
            res *= mn;
            res %= mod;
        }
        mn *= mn;
        mn %= mod;
        n >>= 1;
    }
    return res;
}

int diff(int a, int b)
{
    a ^= b;
    a -= (a >> 1) & 0x55;
    a = (a & m2) + ((a >> 2) & 0x33);
    a = (a + (a >> 4)) & 15;
    return 8 - a;
}


long long seed, base;

inline int my_rnd()
{
    int ret = seed;
    seed = (seed * base + 233) % MOD;
    return ret;
}

//bitset<N> t;
int org[N], a[N], t[N];
long long cnt[2], cn;

int main()
{
    int n, m, p, k;
    priority_queue < pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que;
    scanf("%i%i", &n, &m);
    int j = 0;
    for (int i = 0; i < m; i++)
    {
        scanf("%i %i %lli %lli", &p, &k, &seed, &base);
        for (; j < p; j++)
        {
            t[j] = my_rnd() % 2;
            org[j] = a[j] = my_rnd() % k + 1;
            cnt[t[j]] += a[j];
        }
    }
    k = cnt[1] < cnt[0];
    int i = 0;
    if (t[0] ^ k)
    {
        a[0]--;
        while (t[i] ^ k)
            i++;
    }
    cn = 0;
    while (cnt[k])
    {
        if (t[i] ^ k)
        {
            p = min((long long)a[i], cn);
            cnt[k] -= p;
            cn -= p;
            a[i] -= p;
        }
        else
        {
            cn += a[i];
            a[i] = 0;
        }
        i = (i + 1) % n;
    }
    long long fans = 1;
    for (int i = 0; i < n; i++)
    {
        fans *= (((org[i] - a[i]) ^ (1LL * (i + 1) * (i + 1))) + 1) % MOD;
        fans %= MOD;
    }
    printf("%lli\n", fans);
}