#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[262144][19][2], a[262144], b[262144]; int recursion(int l, int k, int x) { if (memo[l][k][0] >= 0) return ((long long)memo[l][k][0] * x + memo[l][k][1]) % Mod; else if (k == 0) { memo[l][k][0] = a[l]; memo[l][k][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[l][k][0] = (long long)memo[l][k-1][0] * memo[l ^ bit[k-1]][k-1][0] % Mod; memo[l][k][1] = ((long long)memo[l ^ bit[k-1]][k-1][0] * memo[l][k-1][1] + memo[l ^ bit[k-1]][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 (l = 0; l < N; l++) for (i = 0; bit[i] <= N; i++) memo[l][i][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; }