#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007//998244353//1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef  long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef ll ValType;
template<typename  T> void maxtt(T& t1, T t2) {
    t1=max(t1,t2);
}
template<typename  T> void mintt(T& t1, T t2) {
    t1=min(t1,t2);
}
#define MAX (135)

bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
const ll MOD2 = (ll)MOD * (ll)MOD;
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD){
    x+=y; if (x>=mod) x-=mod;
    if(x<0) x+=mod;
    assert(x>=0 && x<mod);
}
void et(int x=-1) {
    printf("%d\n", x); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) {
    return y?gcd1(y,x%y):x;
}





void fmain(int tid) {
    int seed = 0,base = 0;
    auto rnd=[&](){
        int ret = seed;
        seed = ((ll)seed * base + 233) % 1000000007;
        return ret;
    };
    scanf("%d%d", &n, &m);
    vector<int> a(n+2,0), t(n+2,0), A(n+2, 0);
    vector<int> p(m+2,0),k(m+2,0),b(m+2,0),w(m+2,0);
    REQ(i,m) scanf("%d%d%d%d",&p[i],&k[i],&b[i],&w[i]);
  
    REQ(i,m){
        seed = b[i];
        base = w[i];
        for (int j = p[i - 1] + 1; j<= p[i];j++){
            t[j] = (rnd() % 2) + 1;
            a[j] = (rnd() % k[i]) + 1;
        }
    }
    auto ppt=[&](){
        ll ans=1;
        REQ(i,n) {
            ll x=((ll)i*i) ^ A[i];
            x++; x%=MOD;
            ans =ans*x%MOD;
        }
        printf("%lld\n", ans); exit(0);
    };
    vector<ll> s(2,0);
    REQ(i,n) t[i]--;
    REQ(i,n) s[t[i]] += a[i];
    ll pre=0;
    int st=1;
    if (s[t[1]] > s[t[1]^1]) {
        A[1]++; a[1]--;
        if (s[t[1]^1]==0)ppt();
        for(;t[st]!=t[1]^1;st++);
    }

    int T=t[st];
    for(int i=st;i<=n*3;i++) {
        int j=i%n; if(j==0) j=n;
        if(t[j]==T) { pre+=a[j]; A[j]+=a[j]; a[j]=0;}
        else {
            ll mi=min(pre,(ll)a[j]);
            a[j]-=mi; pre-=mi; A[j]+=mi;
        }
    }
    ppt();
}
int main() {
    int t=1;
//    init();
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}
