//tempa //#pragma GCC optimize ("-O3") #ifdef _DEBUG #include #include "bits_stdc++.h" #else #include #endif #define t_T template using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned) clock() + (unsigned) time(NULL)); }; } __initon; //衝突対策 #define ws ___ws #define over4(o1, o2, o3, o4, name, ...) name #define int long long #define ll long long #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo= bool; #define au auto using P = pair; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == using vi = vector; using vb = vector; using vs = vector; using vd = vector; using vc = vector; using vp = vector

; //#define V vector #define vvt0(t) vector> #define vvt1(t, a) vector>a #define vvt2(t, a, b) vector>a(b) #define vvt3(t, a, b, c) vector> a(b,vector(c)) #define vvt4(t, a, b, c, d) vector> a(b,vector(c,d)) #define vvi(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(ll,__VA_ARGS__) #define vvb(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(bool,__VA_ARGS__) #define vvs(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(string,__VA_ARGS__) #define vvd(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(double,__VA_ARGS__) #define vvc(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(char,__VA_ARGS__) #define vvp(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(P,__VA_ARGS__) #define v3i(a, b, c, d) vector> a(b, vector(c, vi(d))) #define v3d(a, b, c, d) vector> a(b, vector(c, vd(d))) #define v3m(a, b, c, d) vector> a(b, vector(c, vm(d))) #define PQ priority_queue, greater > #define tos to_string using mapi = map; using mapd = map; using mapc = map; using maps = map; using seti = set; using setd = set; using setc = set; using sets = set; using qui = queue; #define bset bitset #define uset unordered_set #define mset multiset #define umap unordered_map #define umapi unordered_map #define umapp unordered_map #define mmap multimap //マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name # define _rep(i, n) for(int i = 0,_lim=n; i < _lim ; i++) #define repi(i, m, n) for(int i = m,_lim=n; i < _lim ; i++) #define repadd(i, m, n, ad) for(int i = m,_lim=n; i < _lim ; i+= ad) #define rep(...) _overloadrep(__VA_ARGS__,repadd,repi,_rep,)(__VA_ARGS__) #define _rer(i, n) for(int i = n; i >= 0 ; i--) #define reri(i, m, n) for(int i = m,_lim=n; i >= _lim ; i--) #define rerdec(i, m, n, dec) for(int i = m,_lim=n; i >= _lim ; i-=dec) #define rer(...) _overloadrep(__VA_ARGS__,rerdec,reri,_rer,)(__VA_ARGS__) #define fora(a, b) for(auto&& a : b) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 t_T T MAX() { return numeric_limits::max(); } t_T T MIN() { return numeric_limits::min(); } const int inf = (int) 1e9 + 100; const ll linf = (ll) 1e18 + 100; t_T T INF() { return MAX() / 2; } template<> signed INF() { return inf; } template<> ll INF() { return linf; } template<> double INF() { return (double) linf * linf; } const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits::min(); ll mi = numeric_limits::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; //マクロ省略形 関数等 #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define mt make_tuple #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() /*@formatter:off*/ inline void sort(string &a) { sort(all(a)); } t_T inline void sort(vector &a) { sort(all(a)); }; t_T inline void rsort(vector &a) { sort(all(a), greater()); }; template inline void sortp(vector &a, vector &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second;; }};//F = T //例えばreturn p.fi + p.se; template inline void rsortp(vector &a, vector &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; }}; template void fill(V &xx, const T vall) {xx = vall;} template void fill(vector &vecc, const T vall) {for (auto &&vx: vecc) fill(vx, vall);} //@汎用便利関数 入力 template T _in() {T x;cin >> x;return (x);} #define _overloadin(_1, _2, _3, _4, name, ...) name #define in0() _in() #define in1(a) cin>>a #define in2(a, b) cin>>a>>b #define in3(a, b, c) cin>>a>>b>>c #define in4(a, b, c, d) cin>>a>>b>>c>>d #define in(...) _overloadin(__VA_ARGS__,in4,in3,in2 ,in1,in0)(__VA_ARGS__) #define _overloaddin(_1, _2, _3, _4, name, ...) name #define din1(a) int a;cin>>a #define din2(a, b) int a,b;cin>>a>>b #define din3(a, b, c) int a,b,c;cin>>a>>b>>c #define din4(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d #define din(...) _overloadin(__VA_ARGS__,din4,din3,din2 ,din1)(__VA_ARGS__) #define _overloaddind(_1, _2, _3, _4, name, ...) name #define din1d(a) int a;cin>>a;a-- #define din2d(a, b) int a,b;cin>>a>>b;a--,b-- #define din3d(a, b, c) int a,b,c;cin>>a>>b>>c;a--,b--,c-- #define din4d(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d;;a--,b--,c--,d-- #define dind(...) _overloaddind(__VA_ARGS__,din4d,din3d,din2d ,din1d)(__VA_ARGS__) template void resize(W &vec_, const H head) { vec_.resize(head); } template void resize(W &vec_, const H &head, const T ... tail) {vec_.resize(head);for (auto &v: vec_)resize(v, tail...);} string sin() { return _in(); } ll lin() { return _in(); } t_T void na(vector &a, ll n) {a.resize(n);rep(i, n)cin >> a[i];} #define dna(a, n) vi a; na(a, n);/*nを複数使うと n==in()の時バグる事に注意*/ #define dnad(a, n) vi a; nad(a, n); t_T void nad(vector &a, ll n) {a.resize(n);rep(i, n)cin >> a[i], a[i]--;} template void na2(vector &a, vector &b, ll n) {a.resize(n);b.resize(n);rep(i, n)cin >> a[i] >> b[i];} #define dna2(a, b, n) vi a,b; na2(a,b,n); template void na3(vector &a, vector &b, vector &c, ll n) {a.resize(n);b.resize(n);c.resize(n);rep(i, n)cin >> a[i] >> b[i] >> c[i];} #define nt(a, h, w) resize(a,h,w);rep(nthi,h)rep(ntwi,w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) resize(a,h,w);rep(ntdhi,h)rep(ntdwi,w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) resize(a,h+2,w+2);fill(a,'#');rep(ntphi,1,h+1)rep(ntpwi,1,w+1) cin >> a[ntphi][ntpwi]; #define dnt(S, h, w) vvi(S,h,w);nt(S,h,w); #define dntc(S, h, w) vvc(S,h,w);nt(S,h,w); #define dnts(S, h, w) vvs(S,h,w);nt(S,h,w); //デバッグ #define sp << " " << constexpr int DEB_LEN =20; constexpr int DEB_LEN_H =12; string deb_tos(const int &v) { if (abs(v) == inf || abs(v) == linf)return "e"; else return to_string(v); } template string deb_tos(const T &a) {stringstream ss;ss << a;return ss.str();} template string deb_tos(const vector &a, ll W = inf) { stringstream ss; if (W == inf)W = min(sz(a), DEB_LEN); if (sz(a) == 0)return ss.str(); rep(i, W) { ss << deb_tos(a[i]); if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str();} template string deb_tos(const vector > &a, vi H, vi W, int key = -1) { stringstream ss; ss << endl; vi lens(sz(W)); fora(h, H) { rep(wi, sz(W)) { lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1); lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1); } } if (key == -1)ss << " *|"; else ss << " " << key << "|"; int wi = 0; fora(w, W) { ss << std::right << std::setw(lens[wi]) << w; wi++; } ss << "" << endl; rep(i, sz(W))rep(_, lens[i]) ss << "_"; rep(i, 3)ss << "_"; ss << "" << endl; fora(h, H) { ss << std::right << std::setw(2) << h << "|"; int wi = 0; fora(w, W) { ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]); wi++; } ss << "" << endl; } return ss.str();} template string deb_tos(const vector > &a, ll H = inf, ll W = inf, int key = -1) { H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H}); W = min({W, sz(a[0]), DEB_LEN_H}); vi hs, ws; rep(h, H) { hs.push_back(h); } rep(w, W) { ws.push_back(w); } return deb_tos(a, hs, ws, key);} template string deb_tos(const vector > > &a, ll H = inf) { stringstream ss; if (H == inf)H = DEB_LEN_H; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << deb_tos(a[i], inf, inf, i); } return ss.str();} template string deb_tos(T (&a)[A]) { return deb_tos(vector(begin(a), end(a))); } template string deb_tos(T (&a)[A][B]) { return deb_tos(vector >(begin(a), end(a))); } template string deb_tos(T (&a)[A][B][C]) { return deb_tos(vector > >(begin(a), end(a))); } #define debugName(VariableName) # VariableName #define _deb1(x) cerr << debugName(x)<<" = "< auto rev(const T &v, int i) {return v[sz(v) - 1 - i];} t_T vector rev(vector &a) { auto b = a; reverse(all(b)); return b;} str rev(str &a) {str s = a;reverse(all(s));return s;} ll ceil(ll a, ll b) {if (b == 0) {cerr<<"ceil"< tv)break; } if (v > 1)res.pb(v); return res;} inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } inline bool inside(int v, int l, int r) { return l <= v && v < r; } t_T vector ruiv(vector &a) { vector ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + a[i]; return ret;} //imoは0-indexed //ruiは1-indexed t_T vector imo(vector &v) { vector ret = v; rep(i, sz(ret) - 1)ret[i + 1] += ret[i]; return ret;} #define ins inside ll u0(ll a) { return a < 0 ? 0 : a; } t_T vector u(const vector &a) { vector ret = a;fora(v, ret)v = u0(v);return ret;} #define MIN(a) numeric_limits::min() #define MAX(a) numeric_limits::max() template pair operator+(const pair &a, const pair & b) {return pair(a.fi+b.fi,a.se+b.se);} void yn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a)cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a)cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template set &operator+=(set &a, U v) { a.insert(v); return a; } template vector &operator+=(vector &a, U v) { a.push_back(v); return a; } template vector& operator+=(vector & a,const vector & b){ rep(i, sz(b))a += b[i]; return a; } t_T T sum(vector &v, int s = 0, int t = inf) { T ret = 0; rep(i, s, min(sz(v), t))ret += v[i]; return ret;} void mod(int &a, int m) { a = (a % m + m) % m; } template inline int mgr(int ok, int ng, F f) { #define _mgrbody int mid = (ok + ng) / 2; if (f(mid))ok = mid; else ng = mid; if (ok < ng)while (ng - ok > 1) { _mgrbody } else while (ok - ng > 1) { _mgrbody } return ok; } template inline int mgr(int ok, int ng, int second, F f) { #define _mgrbody2 int mid = (ok + ng) / 2; if (f(mid, second))ok = mid; else ng = mid; if (ok < ng) while (ng - ok > 1) { _mgrbody2 } else while (ok - ng > 1) { _mgrbody2 } return ok; } template ostream &operator<<(ostream &os, vector &m) { rep(i, sz(m)){os << m[i];if(i < sz(m)-1)os<<" ";} return os;} ostream &operator<<(ostream &os, vector &m) {rep(i, sz(m)){os << m[i];} return os;} template ostream &operator<<(ostream &os, vector> &m) {rep(i, sz(m)){os << m[i]; if(i < sz(m)-1)os << endl;} return os;} constexpr bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) {m /= (ll) pow(sinsuu, keta);return m % sinsuu;} ll bit(int n) { return (1LL << (n)); } ll bit(int n, int sinsuu) { return (ll) pow(sinsuu, n); } int mask(int n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll template inline bool chma(T &a, const U &b) {if (a < b) {a = b;return true;}return false;} template inline bool chma(const U &b) { return chma(ma, b); } template inline bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false;} template inline bool chmi(const U &b) { return chmi(mi, b); } #define unique(v) v.erase( unique(v.begin(), v.end()), v.end() ); int max(vi &a) { int res = a[0]; fora(v, a) { res = max(res, v); } return res;} int min(vi &a) { int res = a[0]; fora(v, a) { res = min(res, v); } return res;} template class fixed_point : T {public: explicit constexpr fixed_point(T &&t) noexcept: T(std::forward(t)) {} template constexpr decltype(auto) operator()(Args &&... args) const { return T::operator()(*this, std::forward(args)...); }};template static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point{std::forward(t)}; } constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) {while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W))return i; }return i;} #define mei(nh, nw, h, w) for (int i = mei_inc(h, w, H, W, -1), nh = i<4? h + h4[i] : 0, nw = i<4? w + w4[i] : 0; i < 4; i=mei_inc(h,w,H,W,i), nh = h+h4[i], nw = w+w4[i]) int mei_inc8(int h, int w, int H, int W, int i) { while (++i < 8) { if (inside(h + h8[i], w + w8[i], H, W))return i; } return i;} #define mei8(nh, nw, h, w) for (int i = mei_inc8(h, w, H, W, -1), nh = i<8? h + h8[i] : 0, nw = i<8? w + w8[i] : 0; i < 8; i=mei_inc8(h,w,H,W,i), nh = h+h8[i], nw = w+w8[i]) int mei_incv(int h, int w, int H, int W, int i, vp &p) { while (++i < sz(p)) { if (inside(h + p[i].fi, w + p[i].se, H, W))return i; } return i;} #define meiv(nh, nw, h, w, p) for (int i = mei_incv(h, w, H, W, -1, p), nh = i void out2(T head, U ... tail) { cout << head << " "; out2(tail...);} template void out(T head, U ... tail) { cout << head << " "; out2(tail...); cout << "" << endl;} t_T void out(T head) { cout << head << endl; } void out() { cout << "" << endl; } #define lowerIndex(a, v) (lower_bound(all(a),v)-a.begin()) #define upperIndex(a, v) (uppereturnbound(all(a),v)-a.begin()) template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } #define vni(name, ...) auto name = make_v(__VA_ARGS__) #define vnb(name, ...) auto name = make_v(__VA_ARGS__) #define vns(name, ...) auto name = make_v(__VA_ARGS__) #define vnd(name, ...) auto name = make_v(__VA_ARGS__) #define vnc(name, ...) auto name = make_v(__VA_ARGS__) #define vnp(name, ...) auto name = make_v

(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v(__VA_ARGS__) #define VEC vector /*@formatter:on*/ int N, M, H, W; vi A, B, C; #ifndef MINT #define MINT /*@formatter:off*/ template class modint; template T minv(T a, T m); template T minv(T a); using Type = std::uint_fast64_t; constexpr signed MOD = 998244353; // 1e9 + 7;//MOD using mint = modint::type, MOD>>; template class modint { constexpr static auto mod() { return Mod_t::value; } public: Type value; constexpr modint() : value(){} constexpr modint(const Type &x) noexcept {if (-mod() <= x && x < mod()) value = static_cast(x); else value = static_cast(x % mod());if (value < 0) value += mod();} const Type &operator()() const { return value; } template explicit operator U() const { return static_cast(value); } constexpr modint operator+(const modint rhs) const noexcept {return modint(*this) += rhs;} constexpr modint operator-(const modint rhs) const noexcept {return modint(*this) -= rhs;} constexpr modint operator*(const modint rhs) const noexcept {return modint(*this) *= rhs;} constexpr modint operator/(const modint rhs) const noexcept {return modint(*this) /= rhs;} constexpr modint &operator+=(const modint rhs) noexcept { value += rhs.value; if (value >= mod()) { value -= mod(); } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (value < rhs.value) { value += mod(); } value -= rhs.value; return *this;} constexpr modint &operator*=(const modint rhs) noexcept {value = value * rhs.value % mod();return *this;} constexpr modint &operator/=(const modint& rhs) noexcept {return operator*=(minv(rhs.value));} modint &operator++() { return *this += 1; } modint &operator--() { return *this -= 1; } modint operator++(signed) {modint result(*this);*this += 1;return result;} modint operator--(signed) {modint result(*this);*this -= 1;return result;} modint operator-() const { return modint(-value); } template friend bool operator==(const modint &lhs, const modint &rhs); template friend bool operator<(const modint &lhs, const modint &rhs); template friend std::istream &operator>>(std::istream &stream, modint &number); operator int() { return value; } }; /*@formatter:off*/ template bool operator==(const modint &lhs, const modint &rhs) { return lhs.value == rhs.value; } template bool operator==(const modint &lhs, U rhs) { return lhs == modint(rhs); } template bool operator==(U lhs, const modint &rhs) { return modint(lhs) == rhs; } template bool operator!=(const modint &lhs, const modint &rhs) { return !(lhs == rhs); } template bool operator!=(const modint &lhs, U rhs) { return !(lhs == rhs); } template bool operator!=(U lhs, const modint &rhs) { return !(lhs == rhs); } template bool operator<(const modint &lhs, const modint &rhs) { return lhs.value < rhs.value; } template modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += rhs; } template modint operator+(const modint &lhs, U rhs) { return modint(lhs) += rhs; } template modint operator+(U lhs, const modint &rhs) { return modint(lhs) += rhs; } template modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= rhs; } template modint operator-(const modint &lhs, U rhs) { return modint(lhs) -= rhs; } template modint operator-(U lhs, const modint &rhs) { return modint(lhs) -= rhs; } template modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= rhs; } template modint operator*(const modint &lhs, U rhs) { return modint(lhs) *= rhs; } template modint operator*(U lhs, const modint &rhs) { return modint(lhs) *= rhs; } template modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= rhs; } template modint operator/(const modint &lhs, U rhs) { return modint(lhs) /= rhs; } template modint operator/(U lhs, const modint &rhs) { return modint(lhs) /= rhs; } template modint operator%(const modint &lhs, U rhs) { cerr << "can't mint % int" << endl; assert(0); exit(0); return 0;} #ifdef optional template optional operator+(const mint &mv, const optional &opt) { if (opt.has_value())return mv + opt.value(); else return optional(); } template optional operator-(const mint &mv, const optional &opt) { if (opt.has_value())return mv * opt.value(); else return optional(); } template optional operator*(const mint &mv, const optional &opt) { if (opt.has_value())return mv * opt.value(); else return optional(); } template optional operator/(const mint &mv, const optional &opt) { if (opt.has_value())return mv / opt.value(); else return optional(); } template optional operator+(const optional &opt, const mint &mv) { if (opt.has_value())return mv + opt.value(); else return optional(); } template optional operator-(const optional &opt, const mint &mv) { if (opt.has_value())return mv * opt.value(); else return optional(); } template optional operator*(const optional &opt, const mint &mv) { if (opt.has_value())return mv * opt.value(); else return optional(); } template optional operator/(const optional &opt, const mint &mv) { if (opt.has_value())return mv / opt.value(); else return optional(); } template void operator+=(mint &mv, const optional &opt) { if (opt.has_value())mv += opt.value(); } template void operator-=(mint &mv, const optional &opt) { if (opt.has_value())mv -= opt.value(); } template void operator*=(mint &mv, const optional &opt) { if (opt.has_value())mv *= opt.value(); } template void operator/=(mint &mv, const optional &opt) { if (opt.has_value())mv /= opt.value(); } template void operator+=(const optional &opt, mint &mv) {if (opt.has_value())opt.value += mv;} template void operator-=(const optional &opt, mint &mv) {if (opt.has_value())opt.value -= mv;} template void operator*=(const optional &opt, mint &mv) {if (opt.has_value())opt.value *= mv;} template void operator/=(const optional &opt, mint &mv) {if (opt.has_value())opt.value /= mv;} #endif //constexpr int mint_len = std::min(1400001, MOD); vector fac, finv, inv; vector p2; void setmod_resize(int mint_len) { // p2.resize(mint_len);p2[0] = 1; for (int i = 1; i < mint_len; ++i) p2[i] = p2[i - 1] * 2 % MOD; int tn = sz(fac); fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = tn; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = tn; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } //4e6 >= n なら O(r)で計算する mint com_big(int n, int r) { Type fa = finv[r]; rep(i, r)fa *= n - i, fa %= MOD; return mint(fa);} mint com(int n, int r) {if (r < 0 || r > n) return 0; /*nが大きくてrが小さい場合、nを上からr個掛ける*/ if (n >= sz(fac)) { if (n <= 4e6)setmod_resize(n + 1000); else return com_big(n, r); }return mint(finv[r] * fac[n] % MOD * finv[n - r]);} mint pom(int n, int r) {/* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0;return mint(fac[n] * finv[n - r]);} mint npr(int n, int r) {/* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0;return mint(fac[n] * finv[n - r]);} mint com_grid(int n, int r) {assert(r);return com(n + r - 2, n - 1);} Type nprin(int n, int r) {/* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0;return fac[n] * finv[n - r] % MOD;} Type icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD;} #define ncr com #define ncri icom //n種類でr個選ぶ //n個の場所にr個の物を置く //負が渡された場合もcomが処理出来る mint nhr(int n, int r) { if (n == 0 && r == 0)return 1; else return com(n + r - 1, r); } //n種類でそれぞれ少なくとも1つは選び、r個揃える mint nhr1(int n, int r) { return nhr(n, r - n); } mint hom(int n, int r) { if (n == 0 && r == 0)return 1; else return com(n + r - 1, r); } Type nhri(int n, int r) { if (n == 0 && r == 0)return 1; else return icom(n + r - 1, r); } //グリッドで0-indexedの最短経路 pascal mint pas(int h, int w) { return com(h + w, w); } template T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u;} template T minv(T a) { if (a < sz(fac)){ return inv[a]; } T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u;} //int型 mod m template mint mpow(const T& a, const U &b) {if (b < 0)return minv(mpow(a, -b)); mint x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res;} template int mpowi(const T &a, const U &b, const V &m = MOD) {return mpow(a,b, m);} template string to_string(const modint &number) { return to_string(number()); } /*@formatter:off*/ #ifdef _DEBUG void yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint) i / j == a) { st << i << " / " << j; i = 2000; break; } } } string val = st.str(); if (val != "") { deb(val); return; } rep(i, 1000) { rep(j, 1000) { if ((mint) i / j == a) { st << i << " / " << j; i = 2000; break; } } } val = st.str(); deb(val);} #else #define yuri(...) ; #endif template std::ostream &operator<<(std::ostream &stream, const modint &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off /*@formatter:on*/ template std::istream &operator>>(std::istream &stream, modint &number) { typename common_type::Type, Type>::type x; stream >> x; number.value = modint::normalize(x); return stream; } /*@formatter:off*/ using PM = pair; using vm = vector; using mapm = map; using umapm = umap; #define vvm(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(mint,__VA_ARGS__) #define vnm(name, ...) auto name = make_v(__VA_ARGS__) string out_m2(mint a) { stringstream st; st<<(int)a ; rep(i, 300) { rep(j, 2, 300) { if ((i%j)&&(mint) i / j == a) { st <<"("<< i << "/" << j<<")"; i = 2000; break; } } } return st.str();} //nhr n個の場所にr個の物を分ける mint m1 = (mint) 1; mint half = (mint) 1 / 2; struct setmod{ //comで長さが足りない場合はsetmod_resizeでリサイズする int mint_len =1.4*1e6; setmod() { //p2を使う場合setmod_resizeも変える // p2.resize(mint_len);p2[0] = 1; for (int i = 1; i < mint_len; ++i) p2[i] = p2[i - 1] * 2 % MOD; fac.resize(mint_len); finv.resize(mint_len); inv.resize(mint_len); inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i < mint_len; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i < mint_len; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; }}setmodv; template using mint_ = modint::type, S>>; #endif /*@formatter:on*/ mint dp[6001][6001]; void solve() { din(N, Q); na(A, N); //組合せ // vvm(dp, N + 1, N + 1); dp[0][0] = 1; fora(a, A) { dp[0][0] *= a; } rep(i, N) { rep(k, i + 1) { dp[i + 1][k] += dp[i][k] * (A[i] - 1) / A[i]; //ひける dp[i + 1][k + 1] += dp[i][k] / A[i]; } } deb(dp); rep(_, Q){ din(v); out(dp[N][v]); } } signed main() { solve(); return 0; }