intread() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar(); return x * f; } intinc(int x, int y) { if (y < 0) y += mod; if (x + y >= mod) x -= mod; return x + y; } intqpow(int a, int x, int mo) { int res = 1; while (x) { if (x & 1) res = 1ll * res * a % mo; x >>= 1; a = 1ll * a * a % mo; } return res; }
int fac[N]; int pre[N], suf[N]; intget(int *y, int x, int len) { if (x < 0) return0; if (x <= len) return y[x]; int ans = 0; pre[0] = suf[len] = 1; for (int i = 1; i <= len; ++i) pre[i] = 1ll * pre[i - 1] * (x - i + 1) % mod; for (int i = len; i >= 1; --i) suf[i - 1] = 1ll * suf[i] * (x - i) % mod; for (int i = 0; i <= len; i++) { int s1 = 1ll * pre[i] * suf[i] % mod; int s2 = 1ll * fac[i] * fac[len - i] % mod; if ((len - i) & 1) s2 = (mod - s2) % mod; //cout << s1 << " " << s2 << endl; ans = inc(ans, 1ll * y[i] * s1 % mod * s2 % mod); } return ans; } int y[N]; int f[N]; int n, m; intmain() { int T = read(); fac[0] = 1; for (int i = 1; i <= 3000; i++) fac[i] = 1ll * fac[i - 1] * i % mod; for (int i = 1; i <= 3000; i++) fac[i] = qpow(fac[i], mod - 2, mod); while (T--) { n = read(), m = read();
for (int i = 0; i <= n; i++) f[i] = read(); f[n + 1] = get(f, n + 1, n); //cout << f[n + 1] << endl; y[0] = f[0]; for (int i = 1; i <= n + 1; i++) y[i] = inc(y[i - 1], f[i]);
while (m--) { int L = read(), R = read(); printf("%d\n", inc(get(y, R, n + 1), -get(y, L - 1, n + 1))); } } }