#include using namespace std; using ll = long long; #include using mint = atcoder::modint998244353; #line 2 "misc/fastio.hpp" #include #include #include #include #include using namespace std; #line 2 "internal/internal-type-traits.hpp" #line 4 "internal/internal-type-traits.hpp" using namespace std; namespace internal { template using is_broadly_integral = typename conditional_t || is_same_v || is_same_v, true_type, false_type>::type; template using is_broadly_signed = typename conditional_t || is_same_v, true_type, false_type>::type; template using is_broadly_unsigned = typename conditional_t || is_same_v, true_type, false_type>::type; #define ENABLE_VALUE(x) \ template \ constexpr bool x##_v = x::value; ENABLE_VALUE(is_broadly_integral); ENABLE_VALUE(is_broadly_signed); ENABLE_VALUE(is_broadly_unsigned); #undef ENABLE_VALUE #define ENABLE_HAS_TYPE(var) \ template \ struct has_##var : false_type {}; \ template \ struct has_##var> : true_type {}; \ template \ constexpr auto has_##var##_v = has_##var::value; #define ENABLE_HAS_VAR(var) \ template \ struct has_##var : false_type {}; \ template \ struct has_##var> : true_type {}; \ template \ constexpr auto has_##var##_v = has_##var::value; } // namespace internal #line 12 "misc/fastio.hpp" namespace fastio { static constexpr int SZ = 1 << 17; static constexpr int offset = 64; char inbuf[SZ], outbuf[SZ]; int in_left = 0, in_right = 0, out_right = 0; struct Pre { char num[40000]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i * 4 + j] = n % 10 + '0'; n /= 10; } } } } constexpr pre; void load() { int len = in_right - in_left; memmove(inbuf, inbuf + in_left, len); in_right = len + fread(inbuf + len, 1, SZ - len, stdin); in_left = 0; } void flush() { fwrite(outbuf, 1, out_right, stdout); out_right = 0; } void skip_space() { if (in_left + offset > in_right) load(); while (inbuf[in_left] <= ' ') in_left++; } void single_read(char& c) { if (in_left + offset > in_right) load(); skip_space(); c = inbuf[in_left++]; } void single_read(string& S) { skip_space(); while (true) { if (in_left == in_right) load(); int i = in_left; for (; i != in_right; i++) { if (inbuf[i] <= ' ') break; } copy(inbuf + in_left, inbuf + i, back_inserter(S)); in_left = i; if (i != in_right) break; } } template >* = nullptr> void single_read(T& x) { if (in_left + offset > in_right) load(); skip_space(); char c = inbuf[in_left++]; [[maybe_unused]] bool minus = false; if constexpr (internal::is_broadly_signed_v) { if (c == '-') minus = true, c = inbuf[in_left++]; } x = 0; while (c >= '0') { x = x * 10 + (c & 15); c = inbuf[in_left++]; } if constexpr (internal::is_broadly_signed_v) { if (minus) x = -x; } } void rd() {} template void rd(Head& head, Tail&... tail) { single_read(head); rd(tail...); } void single_write(const char& c) { if (out_right > SZ - offset) flush(); outbuf[out_right++] = c; } void single_write(const bool& b) { if (out_right > SZ - offset) flush(); outbuf[out_right++] = b ? '1' : '0'; } void single_write(const string& S) { flush(), fwrite(S.data(), 1, S.size(), stdout); } void single_write(const char* p) { flush(), fwrite(p, 1, strlen(p), stdout); } template >* = nullptr> void single_write(const T& _x) { if (out_right > SZ - offset) flush(); if (_x == 0) { outbuf[out_right++] = '0'; return; } T x = _x; if constexpr (internal::is_broadly_signed_v) { if (x < 0) outbuf[out_right++] = '-', x = -x; } constexpr int buffer_size = sizeof(T) * 10 / 4; char buf[buffer_size]; int i = buffer_size; while (x >= 10000) { i -= 4; memcpy(buf + i, pre.num + (x % 10000) * 4, 4); x /= 10000; } if (x < 100) { if (x < 10) { outbuf[out_right] = '0' + x; ++out_right; } else { uint32_t q = (uint32_t(x) * 205) >> 11; uint32_t r = uint32_t(x) - q * 10; outbuf[out_right] = '0' + q; outbuf[out_right + 1] = '0' + r; out_right += 2; } } else { if (x < 1000) { memcpy(outbuf + out_right, pre.num + (x << 2) + 1, 3); out_right += 3; } else { memcpy(outbuf + out_right, pre.num + (x << 2), 4); out_right += 4; } } memcpy(outbuf + out_right, buf + i, buffer_size - i); out_right += buffer_size - i; } void wt() {} template void wt(const Head& head, const Tail&... tail) { single_write(head); wt(forward(tail)...); } template void wtn(const Args&... x) { wt(forward(x)...); wt('\n'); } struct Dummy { Dummy() { atexit(flush); } } dummy; } // namespace fastio using fastio::rd; using fastio::skip_space; using fastio::wt; using fastio::wtn; mint ans[5010]; mint p[5010]; int le[5010],ri[5010],k[5010]; void solve(int l,int r,vector&idx){ if(r-l==1){ for(int i = 0;i>1; vector> left(mid-l+1,vector(mid-l+1,0)); vector> right(r-mid+1,vector(r-mid+1,0)); left[mid-l][0] = 1; for(int i = mid-1;i>=l;i--){ int ni = i - l; for(int j = mid-l;j>=0;j--){ left[ni][j] = left[ni+1][j]; if(j-1>=0) left[ni][j] += left[ni+1][j-1] * p[i]; } } right[0][0] = 1; for(int i = mid;i=0;j--){ right[ni][j] = right[ni-1][j]; if(j-1>=0) right[ni][j] += right[ni-1][j-1] * p[i]; } } vector ls,rs; for(int i = 0;i=mid-l+1||k[ni]-j>=r-mid+1) continue; ans[ni] += left[nni][j] * right[nnj][k[ni]-j]; } } } solve(l,mid,ls); solve(mid,r,rs); } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; rd(n); for(int i = 0;i idx(q); for(int i = 0;i