#include const int Mod = 998244353, bit[20] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288}; int memo[19][262144][2], a[262144], b[262144]; int recursion(int l, int k, int x) { if (memo[k][l][0] >= 0) return ((long long)memo[k][l][0] * x + memo[k][l][1]) % Mod; else if (k == 0) { memo[k][l][0] = a[l]; memo[k][l][1] = b[l]; return ((long long)a[l] * x + b[l]) % Mod; } x = recursion(l, k - 1, x); x = recursion(l ^ bit[k-1], k - 1, x); memo[k][l][0] = (long long)memo[k-1][l][0] * memo[k-1][l ^ bit[k-1]][0] % Mod; memo[k][l][1] = ((long long)memo[k-1][l ^ bit[k-1]][0] * memo[k-1][l][1] + memo[k-1][l ^ bit[k-1]][1]) % Mod; return x; } int solve(int l, int r, int p, int x) { if (l == r) return x; int i; for (i = 1; (l & bit[i-1]) == 0 && l + bit[i] <= r; i++); return solve(l + bit[i-1], r, p, recursion(l ^ p, i - 1, x)); } int main() { int i, N, Q; scanf("%d %d", &N, &Q); for (i = 0; i < N; i++) scanf("%d %d", &(a[i]), &(b[i])); int l, r, p, x; for (i = 0; bit[i] <= N; i++) for (l = 0; l < N; l++) memo[i][l][0] = -1; for (i = 1; i <= Q; i++) { scanf("%d %d %d %d", &l, &r, &p, &x); printf("%d\n", solve(l, r, p, x)); } return 0; }