結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー バイトバイト
提出日時 2020-05-30 16:48:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 3,500 ms
コード長 49,460 bytes
コンパイル時間 4,549 ms
コンパイル使用メモリ 234,052 KB
実行使用メモリ 65,928 KB
最終ジャッジ日時 2024-11-07 19:56:44
合計ジャッジ時間 20,272 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
47,232 KB
testcase_01 AC 96 ms
47,360 KB
testcase_02 AC 99 ms
47,360 KB
testcase_03 AC 108 ms
47,872 KB
testcase_04 AC 103 ms
47,744 KB
testcase_05 AC 104 ms
47,616 KB
testcase_06 AC 101 ms
47,744 KB
testcase_07 AC 102 ms
47,616 KB
testcase_08 AC 107 ms
47,744 KB
testcase_09 AC 107 ms
47,744 KB
testcase_10 AC 101 ms
47,616 KB
testcase_11 AC 102 ms
47,616 KB
testcase_12 AC 99 ms
47,488 KB
testcase_13 AC 684 ms
65,800 KB
testcase_14 AC 666 ms
65,800 KB
testcase_15 AC 674 ms
65,804 KB
testcase_16 AC 677 ms
65,796 KB
testcase_17 AC 676 ms
65,804 KB
testcase_18 AC 678 ms
65,796 KB
testcase_19 AC 684 ms
65,800 KB
testcase_20 AC 677 ms
65,804 KB
testcase_21 AC 679 ms
65,804 KB
testcase_22 AC 681 ms
65,924 KB
testcase_23 AC 673 ms
65,732 KB
testcase_24 AC 673 ms
65,800 KB
testcase_25 AC 673 ms
65,800 KB
testcase_26 AC 663 ms
65,928 KB
testcase_27 AC 658 ms
65,928 KB
testcase_28 AC 617 ms
65,800 KB
testcase_29 AC 613 ms
65,800 KB
testcase_30 AC 614 ms
65,760 KB
testcase_31 AC 100 ms
47,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//tempa
//#pragma GCC optimize ("-O3")
#ifdef _DEBUG
#include<cassert>
#include "bits_stdc++.h"
#else
#include <bits/stdc++.h>
#endif

#define t_T template<class T>

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

using sig_dou = double;
#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<ll, ll>;

#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<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;

//#define V vector
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>>a
#define vvt2(t, a, b) vector<vector<t>>a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b,vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b,vector<t>(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<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))


#define PQ priority_queue<ll, vector<ll>, greater<ll> >
#define tos to_string

using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int,int>
#define umapp unordered_map<P,int>
#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<T>::max(); }
t_T T MIN() { return numeric_limits<T>::min(); }
const int inf = (int) 1e9 + 100;
const ll linf = (ll) 1e18 + 100;
t_T T INF() { return MAX<T>() / 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<ll>::min();
ll mi = numeric_limits<ll>::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<T> &a) { sort(all(a)); };
t_T inline void rsort(vector<T> &a) { sort(all(a), greater<T>()); };
template<class U> inline void sortp(vector<U> &a, vector<U> &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<T>
//例えばreturn p.fi + p.se;
template<class U> inline void rsortp(vector<U> &a, vector<U> &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<typename V, typename T>
void fill(V &xx, const T vall) {xx = vall;}
template<typename V, typename T>
void fill(vector<V> &vecc, const T vall) {for (auto &&vx: vecc) fill(vx, vall);}

//@汎用便利関数 入力
template<typename T = int> 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<typename W, typename H> void resize(W &vec_, const H head) { vec_.resize(head); }
template<typename W, typename H, typename ... T> void resize(W &vec_, const H &head, const T ... tail) {vec_.resize(head);for (auto &v: vec_)resize(v, tail...);}


string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
t_T void na(vector<T> &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<T> &a, ll n) {a.resize(n);rep(i, n)cin >> a[i], a[i]--;}
template<class T, class U> void na2(vector<T> &a, vector<U> &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<class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &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<class T> string deb_tos(const T &a) {stringstream ss;ss << a;return ss.str();}
template<class T> string deb_tos(const vector<T> &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<class T> string deb_tos(const vector<vector<T> > &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<class T> string deb_tos(const vector<vector<T> > &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<class T> string deb_tos(const vector<vector<vector<T> > > &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<class T, size_t A> string deb_tos(T (&a)[A]) { return deb_tos(vector<T>(begin(a), end(a))); }
template<class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) { return deb_tos(vector<vector<T> >(begin(a), end(a))); }
template<class T, size_t A, size_t B, size_t C> string deb_tos(T (&a)[A][B][C]) { return deb_tos(vector<vector<vector<T> > >(begin(a), end(a))); }

#define debugName(VariableName) # VariableName

#define _deb1(x) cerr <<  debugName(x)<<" = "<<deb_tos(x) << endl
#define _deb2(x, y) cerr <<  debugName(x)<<" = "<<deb_tos(x)<<", "<< debugName(y)<<" = "<<deb_tos(y)<< endl
#define _deb3(x, y, z) cerr <<  debugName(x)<<" = "<<deb_tos(x)  << ", " <<  debugName(y)<<" = "<<deb_tos(y) <<", " debugName(z)<<" = "<<deb_tos(z) <<endl
#define _deb4(x, y, z, a) cerr <<  debugName(x)<<" = "<<deb_tos(x) <<", " <<   debugName(y)<<" = "<<deb_tos(y) <<", " <<  debugName(z)<<" = "<<deb_tos(z) <<", " <<  debugName(a)<<" = "<<deb_tos(a)<<endl
#define _deb5(x, y, z, a, b) cerr <<  debugName(x)<<" = "<<deb_tos(x) <<", " <<   debugName(y)<<" = "<<deb_tos(y) <<", " <<  debugName(z)<<" = "<<deb_tos(z) <<", " <<  debugName(a)<<" = "<<deb_tos(a)<<", " <<  debugName(b)<<" = "<<deb_tos(b)<<endl


#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__)
#ifdef _DEBUG
#define deb(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__)
#else
#define deb(...) ;
#endif


#define debugline(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n'


ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {ll res = b[0];for (auto &&v :b)res = gcd(v, res);return res;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {    ll res = 0;    while (a) {        res *= 10;        res += a % 10;        a /= 10;    }    return res;}
template<class T> auto rev(const T &v, int i) {return v[sz(v) - 1 - i];}
t_T vector<T> rev(vector<T> &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"<<endl;exit(1);return -1;} else return (a + b - 1) / b;}
ll sqrt(ll a) {    if (a < 0) {        cerr<<"sqrt"<<endl;        exit(1);    }    ll res = (ll) std::sqrt(a);    while (res * res < a)res++;    return res;}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }

vi divisors(int v) {    vi res;    double lim = std::sqrt(v);    rep(i, 1, lim+1){        if (v % i == 0) {            res.pb(i);            if (i != v / i)res.pb(v / i);        }    }    return res;}
vb isPrime;
vi primes;

void setPrime() {    int len = 4010101;    isPrime.resize(4010101);    fill(isPrime, true);    isPrime[0] = isPrime[1] = false;    for (int i = 2; i <= sqrt(len) + 5; ++i) {        if (!isPrime[i])continue;        for (int j = 2; i * j < len; ++j) {            isPrime[i * j] = false;        }    }    rep(i, len)if (isPrime[i])primes.pb(i);}

vi factorization(int v) {    int tv = v;    vi res;    if (isPrime.size() == 0)setPrime();    for (auto &&p :primes) {        if (v % p == 0)res.push_back(p);        while (v % p == 0) {            v /= p;        }        if (v == 1 || p * p > 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<T> ruiv(vector<T> &a) {    vector<T> 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<T> imo(vector<T> &v) {    vector<T> 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<T> u(const vector<T> &a) {
    vector<T> ret = a;fora(v, ret)v = u0(v);return ret;}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
template<class T, class U> pair<T,U> operator+(const pair<T,U> &a, const pair<T,U> & b) {return pair<T,U>(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<class T, class U> set<T> &operator+=(set<T> &a, U v) {
    a.insert(v);
    return a;
}
template<class T, class U> vector<T> &operator+=(vector<T> &a, U v) {
    a.push_back(v);
    return a;
}
template<class T> vector<T>& operator+=(vector<T> & a,const vector<T> & b){
    rep(i, sz(b))a += b[i];
    return a;
}
t_T T sum(vector<T> &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<class F> 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<class F> 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<typename T> ostream &operator<<(ostream &os, vector<T> &m) {    rep(i, sz(m)){os << m[i];if(i < sz(m)-1)os<<" ";}   return os;}
ostream &operator<<(ostream &os, vector<char> &m) {rep(i, sz(m)){os << m[i];}   return os;}
template<typename T> ostream &operator<<(ostream &os, vector<vector<T>> &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<class T, class U> inline bool chma(T &a, const U &b) {if (a < b) {a = b;return true;}return false;}
template<class U> inline bool chma(const U &b) { return chma(ma, b); }
template<class T, class U> inline bool chmi(T &a, const U &b) {    if (b < a) {        a = b;        return true;    }    return false;}
template<class U> 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<typename T> class fixed_point        : T {public:    explicit constexpr fixed_point(T &&t) noexcept: T(std::forward<T>(t)) {}    template<typename... Args> constexpr decltype(auto) operator()(Args &&... args) const { return T::operator()(*this, std::forward<Args>(args)...); }};template<typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(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<sz(p)? h + p[i].fi : 0, nw = i<sz(p)? w + p[i].se : 0; i < sz(p); i=mei_incv(h,w,H,W,i,p), nh = h+p[i].fi, nw = w+p[i].se)
vi compress(vi &A) {    vi B = A;    sort(B);    unique(B);    vi inds(sz(B));    rep(i, sz(A)) {        int ind = lower_bound(all(B), A[i]) - B.begin();        inds[ind] = A[i];        A[i] = ind;    }    return inds;}
vi compress(vi &A, umapi& map){    vi i_v = compress(A);    rep(i, sz(i_v)){        map[i_v[i]] = i;    }    return i_v;}

t_T void out2(T head) {    cout << head;}
template<class T, class... U> void out2(T head, U ... tail) {    cout << head << " ";      out2(tail...);}
template<class T, class... U> 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<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); }
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define VEC vector

/*@formatter:on*/
int N, M, H, W;
vi A, B, C;

#ifndef MINT
//@formatter:off
    template<typename T> T minv(T a, T m);
    template<typename T> T minv(T a);

    template<typename T>
    class Modular {
    public:
        using Type = typename decay<decltype(T::value)>::type;    constexpr Modular() : value() {}    template<typename U>    Modular(const U &x) {        value = normalize(x);    }    template<typename U>    static Type normalize(const U &x) {        Type v;        if (-mod() <= x && x < mod()) v = static_cast<Type>(x);        else v = static_cast<Type>(x % mod());        if (v < 0) v += mod();        return v;    }    const Type &operator()() const { return value; }    template<typename U>explicit operator U() const { return static_cast<U>(value); }    constexpr static Type mod() { return T::value; }    Modular &operator+=(const Modular &other) {        if ((value += other.value) >= mod()) value -= mod();        return *this;    }    Modular &operator-=(const Modular &other) {        if ((value -= other.value) < 0) value += mod();        return *this;    }    template<typename U> Modular &operator+=(const U &other) { return *this += Modular(other); }    template<typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); }    Modular &operator++() { return *this += 1; }    Modular &operator--() { return *this -= 1; }    Modular operator++(signed) {        Modular result(*this);        *this += 1;        return result;    }    Modular operator--(signed) {        Modular result(*this);        *this -= 1;        return result;    }    Modular operator-() const { return Modular(-value); }
        template<typename U = T>typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type &operator*=(const Modular &rhs) {
#ifdef _WIN32
            uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;asm("divl %4; \n\t": "=a" (d), "=d" (m): "d" (xh), "a" (xl), "r" (mod()));value = m;
#else
            value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
            return *this;
        }
        template<typename U = T>    typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type &operator*=(const Modular &rhs) {        int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod());        value = normalize(value * rhs.value - q * mod());        return *this;    }    template<typename U = T>    typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type &operator*=(const Modular &rhs) {        value = normalize(value * rhs.value);        return *this;    }    Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); }    template<typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);    template<typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);    template<typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number);    operator int() { return value; }private:    Type value;
    };
    template<typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; }template<typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); }template<typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; }template<typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); }template<typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); }template<typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); }template<typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; }template<typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; }template<typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; }template<typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; }template<typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; }template<typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; }template<typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; }template<typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; }template<typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; }template<typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; }template<typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; }template<typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; }template<typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; }

    constexpr signed MOD =
            998244353;
//    1e9 + 7;//MOD
    using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>;
    constexpr int mint_len = 1400001;
    vi fac, finv, inv;
    vi p2;
    mint com(int n, int r) {    if (r < 0 || r > n) return 0;    /*nが大きくてrが小さい場合、nを上からr個掛ける*/    if (n >= mint_len) {        int fa = finv[r];        rep(i, r)fa *= n - i, fa %= MOD;        return mint(fa);    }    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]);}
    int 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;}
    int 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個の物を置く
    mint nhr(int n, int r) { if(n==0&&r==0)return 1; else return com(n + r - 1, r); }
    mint hom(int n, int r) { if(n==0&&r==0)return 1; else return com(n + r - 1, r); }
    int 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<typename T> 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<typename T> T minv(T a) {    if (a < mint_len)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;}
    template<typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) {    assert(b >= 0);    int x = a(), res = 1;    U p = b;    while (p > 0) {        if (p & 1) (res *= x) %= MOD;        (x *= x) %= MOD;        p >>= 1;    }    return res;}
    template<typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) {    assert(b >= 0);    int x = a, res = 1;    U p = b;    while (p > 0) {        if (p & 1) (res *= x) %= m;        (x *= x) %= m;        p >>= 1;    }    return res;}
    //-k乗出来る
    template<typename T, typename U> mint mpow(const T a, const U b) {/*    assert(b >= 0);*/    if(b<0){        return minv(mpow(a,-b));    }    int x = a, res = 1;    U p = b;    while (p > 0) {        if (p & 1) (res *= x) %= MOD;        (x *= x) %= MOD;        p >>= 1;    }    return res;}
    template<typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) {    assert(b >= 0);    int x = a, res = 1;    U p = b;    while (p > 0) {        if (p & 1) (res *= x) %= m;        (x *= x) %= m;        p >>= 1;    }    return res;}
    template<typename T> string to_string(const Modular<T> &number) {    return to_string(number());}
#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<typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {stream << number();
#ifdef _DEBUG
    //    stream << " -> " << yuri(number);
#endif
        return stream;
    }
    //@formatter:off
    template<typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) {    typename common_type<typename Modular<T>::Type, int64_t>::type x;    stream >> x;    number.value = Modular<T>::normalize(x);    return stream;}
    using PM = pair<mint, mint>;
    using vm = vector<mint>;
    using mapm = map<int, mint>;
    //using umapm = umap<int, mint>;
#define vvm(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(mint,__VA_ARGS__)

#define vnm(name, ...) auto name = make_v<mint>(__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();}
    struct setmod{
        setmod() {
        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;
    //@formatter:on
    //nhr n個の場所にr個の物を分ける
    mint m1 = (mint) 1;
mint half = (mint) 1 / 2;

#endif
/*@formatter:off*/
namespace FastFourierTransform {    using real = sig_dou;    struct C {        real x, y;        C() : x(0), y(0) {}        C(real x, real y) : x(x), y(y) {}        inline C operator+(const C &c) const { return C(x + c.x, y + c.y); }        inline C operator-(const C &c) const { return C(x - c.x, y - c.y); }        inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); }        inline C conj() const { return C(x, -y); }    };    const real PI = acosl(-1);    signed base = 1;    vector<C> rts = {{0, 0},                     {1, 0}};    vector<signed> rev = {0, 1};    void ensure_base(signed nbase) {if (nbase <= base) return;        rev.resize(1 << nbase);        rts.resize(1 << nbase);        for (signed i = 0; i < (1 << nbase); i++) {            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));        }        while (base < nbase) {            real angle = PI * 2.0 / (1 << (base + 1));            for (signed i = 1 << (base - 1); i < (1 << base); i++) {                rts[i << 1] = rts[i];                real angle_i = angle * (2 * i + 1 - (1 << base));                rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));            }            ++base;        }    }    void fft(vector<C> &a, signed n) {        assert((n & (n - 1)) == 0);        signed zeros = __builtin_ctz(n);        ensure_base(zeros);        signed shift = base - zeros;        for (signed i = 0; i < n; i++) {            if (i < (rev[i] >> shift)) {                swap(a[i], a[rev[i] >> shift]);            }        }        for (signed k = 1; k < n; k <<= 1) {            for (signed i = 0; i < n; i += 2 * k) {                for (signed j = 0; j < k; j++) {                    C z = a[i + j + k] * rts[j + k];                    a[i + j + k] = a[i + j] - z;                    a[i + j] = a[i + j] + z;                }            }        }    }    vector<int64_t> multiply(const vector<signed> &a, const vector<signed> &b) {        signed need = (signed) a.size() + (signed) b.size() - 1;        signed nbase = 1;        while ((1 << nbase) < need) nbase++;        ensure_base(nbase);        signed sz = 1 << nbase;        vector<C> fa(sz);        for (signed i = 0; i < sz; i++) {            signed x = (i < (signed) a.size() ? a[i] : 0);            signed y = (i < (signed) b.size() ? b[i] : 0);            fa[i] = C(x, y);        }        fft(fa, sz);        C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);        for (signed i = 0; i <= (sz >> 1); i++) {            signed j = (sz - i) & (sz - 1);            C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;            fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;            fa[i] = z;        }        for (signed i = 0; i < (sz >> 1); i++) {            C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;            C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i];            fa[i] = A0 + A1 * s;        }        fft(fa, sz >> 1);        vector<int64_t> ret(need);        for (signed i = 0; i < need; i++) {            ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x);        }        return ret;    }};
template<typename T>struct ArbitraryModConvolution {using real = FastFourierTransform::real;using C = FastFourierTransform::C;ArbitraryModConvolution() = default;vector<T> multiply(const vector<T> &a, const vector<T> &b, signed need = -1) {        if (need == -1) need = a.size() + b.size() - 1;        signed nbase = 0;        while ((1 << nbase) < need) nbase++;        FastFourierTransform::ensure_base(nbase);        signed sz = 1 << nbase;        vector<C> fa(sz);        for (signed i = 0; i < a.size(); i++) {            fa[i] = C((signed) a[i] & ((1 << 15) - 1), (signed) a[i] >> 15);        }        fft(fa, sz);        vector<C> fb(sz);        if (a == b) {            fb = fa;        } else {            for (signed i = 0; i < b.size(); i++) {                fb[i] = C((signed) b[i] & ((1 << 15) - 1), (signed) b[i] >> 15);            }            fft(fb, sz);        }        real ratio = 0.25 / sz;        C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1);        for (signed i = 0; i <= (sz >> 1); i++) {            signed j = (sz - i) & (sz - 1);            C a1 = (fa[i] + fa[j].conj());            C a2 = (fa[i] - fa[j].conj()) * r2;            C b1 = (fb[i] + fb[j].conj()) * r3;            C b2 = (fb[i] - fb[j].conj()) * r4;            if (i != j) {                C c1 = (fa[j] + fa[i].conj());                C c2 = (fa[j] - fa[i].conj()) * r2;                C d1 = (fb[j] + fb[i].conj()) * r3;                C d2 = (fb[j] - fb[i].conj()) * r4;                fa[i] = c1 * d1 + c2 * d2 * r5;                fb[i] = c1 * d2 + c2 * d1;            }            fa[j] = a1 * b1 + a2 * b2 * r5;            fb[j] = a1 * b2 + a2 * b1;        }        fft(fa, sz);        fft(fb, sz);        vector<T> ret(need);        for (signed i = 0; i < need; i++) {            int64_t aa = llround(fa[i].x);            int64_t bb = llround(fb[i].x);            int64_t cc = llround(fa[i].y);            aa = T(aa), bb = T(bb), cc = T(cc);            ret[i] = aa + (bb << 15) + (cc << 30);        }        return ret;    }};
template<typename T>struct FormalPowerSeries : vector<T> {
    using vector< T >::vector;
    using P = FormalPowerSeries;
    using MULT = function< P(P, P) >;
    static MULT &get_mult() {    static MULT mult = nullptr;    return mult;  }
    static void set_fft(MULT f) {    get_mult() = f;      }
    template< typename E >      FormalPowerSeries(const vector< E > &x) : vector< T >(begin(x), end(x)) {}
    void shrink() {        while(this->size() && this->back() == T(0)) this->pop_back();      }
    P operator+(const P &r) const { return P(*this) += r; }
    P operator+(const T &v) const { return P(*this) += v; }
    P operator-(const P &r) const { return P(*this) -= r; }
    P operator-(const T &v) const { return P(*this) -= v; }
    P operator*(const P &r) const { return P(*this) *= r; }
    P operator*(const T &v) const { return P(*this) *= v; }
    P operator/(const P &r) const { return P(*this) /= r; }
    P operator%(const P &r) const { return P(*this) %= r; }
    P &operator+=(const P &r) {        if(r.size() > this->size()) this->resize(r.size());        for(int i = 0; i < r.size(); i++) (*this)[i] += r[i];        return *this;      }
    P &operator+=(const T &r) {        if(this->empty()) this->resize(1);        (*this)[0] += r;        return *this;      }
    P &operator-=(const P &r) {        if(r.size() > this->size()) this->resize(r.size());        for(int i = 0; i < r.size(); i++) (*this)[i] -= r[i];        shrink();        return *this;      }
    P &operator-=(const T &r) {if(this->empty()) this->resize(1);    (*this)[0] -= r;    shrink();    return *this;  }
    P &operator*=(const T &v) {    const int n = (int) this->size();    for(int k = 0; k < n; k++) (*this)[k] *= v;    return *this;  }
    P &operator*=(const P &r) {    if(this->empty() || r.empty()) {      this->clear();      return *this;    }    assert(get_mult() != nullptr);    return *this = get_mult()(*this, r);  }
    P &operator%=(const P &r) {return *this -= *this / r * r;}
    P operator-() const {    P ret(this->size());    for(int i = 0; i < this->size(); i++) ret[i] = -(*this)[i];    return ret;  }
    //謎が多い あまり使いたくない
    P &operator/=(P r) {        while((*this).back()==T(0))(*this).pop_back();        while(r.back()==T(0))r.pop_back();        if (this->size() < r.size()) {            this->clear();            return *this;        }        int n = this->size() - r.size() + 1;        return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n);    }
    P pre(int sz) const {    return P(begin(*this), begin(*this) + min((int) this->size(), sz));  }
    P operator>>(int sz) const {    if(this->size() <= sz) return {};    P ret(*this);    ret.erase(ret.begin(), ret.begin() + sz);    return ret;  }
    P operator<<(int sz) const {    P ret(*this);    ret.insert(ret.begin(), sz, T(0));    return ret;  }
    P rev(int deg = -1) const {    P ret(*this);    if(deg != -1) ret.resize(deg, T(0));    reverse(begin(ret), end(ret));    return ret;  }
    P diff() const {    const int n = (int) this->size();    P ret(max(0ll, n - 1));    for(int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i);    return ret;  }
    P integral() const {    const int n = (int) this->size();    P ret(n + 1);    ret[0] = T(0);    for(int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1);    return ret;  }
    // F(0) must not be 0
    P inv(int deg = -1) const {    assert(((*this)[0]) != T(0));    const int n = (int) this->size();    if(deg == -1) deg = n;    P ret({T(1) / (*this)[0]});    for(int i = 1; i < deg; i <<= 1) {      ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1);    }    return ret.pre(deg);  }
    // F(0) must be 1
    P log(int deg = -1) const {    assert((*this)[0] == T(1));    const int n = (int) this->size();    if(deg == -1) deg = n;    return (this->diff() * this->inv(deg)).pre(deg - 1).integral();  }
    P sqrt(int deg = -1) const {    const int n = (int) this->size();    if(deg == -1) deg = n;    if((*this)[0] == T(0)) {      for(int i = 1; i < n; i++) {        if((*this)[i] != T(0)) {          if(i & 1) return {};          if(deg - i / 2 <= 0) break;          auto ret = (*this >> i).sqrt(deg - i / 2) << (i / 2);          if(ret.size() < deg) ret.resize(deg, T(0));          return ret;        }      }      return P(deg, 0);    }P ret({T(1)});    T inv2 = T(1) / T(2);    for(int i = 1; i < deg; i <<= 1) {      ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2;    }    return ret.pre(deg);  }
    // F(0) must be 0
    P exp(int deg = -1) const {    assert((*this)[0] == T(0));    const int n = (int) this->size();    if(deg == -1) deg = n;    P ret({T(1)});    for(int i = 1; i < deg; i <<= 1) {      ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1);    }    return ret.pre(deg);  }
    P pow(int64_t k, int deg = -1) const {    const int n = (int) this->size();    if(deg == -1) deg = n;    for(int i = 0; i < n; i++) {      if((*this)[i] != T(0)) {        T rev = T(1) / (*this)[i];        P C(*this * rev);        P D(n - i);        for(int j = i; j < n; j++) D[j - i] = C[j];        D = (D.log() * k).exp() * mpow((*this)[i],(k));        P E(deg);        if(i * k > deg) return E;        auto S = i * k;        for(int j = 0; j + S < deg && j < D.size(); j++) E[j + S] = D[j];        return E;      }    }    return *this;  }
    T eval(T x) const {    T r = 0, w = 1;    for(auto &v : *this) {      r += w * v;      w *= x;    }    return r;  }
    // *x^x
    P& rshift(int k){        int an= this->size();        this->resize(an+k);        rer(i, an+k-1,k){            (*this)[i] = (*this)[i-k];        }        rep(i,k){            (*this)[i] = 0;        }        return (*this);    }
    // /x^k
    P& lshift(int k){        int an= this->size();        rep(i,an-k){            (*this)[i] =(*this)[i+k];        }        this->resize(an-k);        return (*this);    }
    //x = rx^dを代入する
    P &assign(int r, int d, int msize) {        P ret(msize + 1);        T rv = 1;        rep(i, min((int)this->size(), msize / d + 1)) {            ret[i * d] = (*this)[i] * rv;            rv *= r;        }        return *this = ret;    }
};
ArbitraryModConvolution<mint> fft;
using fps = FormalPowerSeries<mint>;
struct FPS_init {FPS_init() {fps::set_fft([](const fps::P &a, const fps::P &b) { return fft.multiply(a, b); });};} initooonv; struct helper_fps {        int i;        mint k;        helper_fps(int i, mint k) : i(i), k(k) {}        operator fps() {            fps ret(i + 1);            ret[i] = k;            return ret;        }        fps operator+(mint b) { return fps(*this) + b; }        fps operator+(helper_fps b) { return fps(*this) + fps(b); }        fps operator-(mint b) { return fps(*this) - b; }        fps operator-(helper_fps b) { return fps(*this) - fps(b); }        helper_fps operator*(mint b) { return helper_fps(i, k * b); }        helper_fps operator*(helper_fps b) { return helper_fps(i + b.i, k * b.k); }        helper_fps operator^(int b) { return helper_fps(i * b, mpow(k, b)); }    } X(1, 1);    fps operator+(mint a, helper_fps b) { return b + a; }    fps operator+(const fps &a, helper_fps b) { return a + (fps) b; }    fps operator-(mint a, helper_fps b) { return b + a; }    fps operator-(const fps &a, helper_fps b) { return a - (fps) b; }    helper_fps operator*(mint a, helper_fps b) { return b * a; }
//vectorとして使える
fps operator+(fps &l, signed v) {    fps ret = l;    ret[0] += v;    return ret;}fps operator+(fps &l, ll v) {    fps ret = l;    ret[0] += v;    return ret;}fps operator+=(fps &l, signed v) {    l[0] += v;    return l;}fps operator+=(fps &l, ll v) {    l[0] += v;    return l;}fps operator-(fps &l, signed v) { return operator+(l, -v); }fps operator-(fps &l, ll v) { return operator+(l, -v); }fps operator-=(fps &l, signed v) { return operator+=(l, -v); }fps operator-=(fps &l, ll v) { return operator+=(l, -v); }fps operator*(fps &l, signed v) {    fps ret = l;    for (int i = 0; i < l.size(); i++)ret[i] *= v;    return ret;}fps operator*(fps &l, ll v) {    fps ret = l;    for (int i = 0; i < l.size(); i++)ret[i] *= v;    return ret;}fps operator*=(fps &l, signed v) {    for (int i = 0; i < l.size(); i++)l[i] *= v;    return l;}fps operator*=(fps &l, ll v) {    for (int i = 0; i < l.size(); i++)l[i] *= v;    return l;}fps operator/(fps &l, signed v) { return operator*(l, minv(v)); }fps operator/(fps &l, ll v) { return operator*(l, minv(v)); }fps operator/=(fps &l, signed v) { return operator*=(l, minv(v)); }fps operator/=(fps &l, ll v) { return operator*=(l, minv(v)); }fps operator+(signed l, fps &v) { return operator+(v, l); }fps operator+(ll l, fps &v) { return operator+(v, l); }fps operator-(signed l, fps v) {v *= -1;return operator+(v, l);}fps operator-(ll l, fps v) {v *= -1;return operator+(v, l);}fps operator*(signed l, fps &v) { return operator*(v, l); }fps operator*(ll l, fps &v) { return operator*(v, l); }fps operator/(signed l, fps v) {    v = v.inv();    return operator*(v, l);}fps operator/(ll l, fps v) {    v = v.inv();    return operator*(v, l);}fps cut(fps l, fps r) {    fps ret = l;    ret *= r;    ret.resize(max(l.size(), r.size()));    return ret;}
template<class... T> fps cut(fps l, T... r) {    return cut(l, cut(r...));}
//initializerlist operatorに
fps cutf(fps &l, fps &r) {    fps ret = l;    ret *= r;    ret.resize(max(l.size(), r.size()));    return ret;}

//合計がSになる組合せを返す
template<class T> T fft_get(vector<T> &a, vector<T> &b, int S) {    int bn = sz(b);    T res = 0;    rep(l, sz(a) + 1) {        int r = S - l;        if (r >= bn || r < 0)con;        res += a[l] * b[r];    }    return res;}
//右から疎なfpsを掛ける、O(N) * 右の項数
fps mul_sparse(const fps &l, const fps &r, int msize = -1) {        vi inds;        rep(i, r.size()) { if (r[i]) { inds.push_back(i); }}        assert(sz(inds));        int n = l.size() + inds.back();        if (msize == -1) { msize = n - 1; }        fps ret(msize + 1);        rep(i, l.size()) {            for (auto d : inds) {                if (i + d > msize)break;                ret[i + d] += l[i] * r[d];            }        }        return ret;    }
//右から疎なfpsで割る、O(N) * 右の項数
fps div_sparse(const fps& a, const fps &r, int msize = -1) {        fps l = a;        vi inds;        rep(i, r.size()) { if (r[i]) { inds.push_back(i); }}        assert(sz(inds));        int n = l.size();        if (msize == -1) { msize = n - 1; }        msize = min(msize, n - 1);        fps ret(msize + 1);        rep(i, l.size()) {            if (l[i]) {                assert(i >= inds[0]);                mint dec_k = l[i] / r[inds[0]];                if (i - inds[0] > msize)break;                ret[i - inds[0]] += dec_k;                for (auto d : inds) {                    if (i + d - inds[0] >= n)break;                    l[i + d - inds[0]] -= dec_k * r[d];                }            }        }        return ret;    }
//Π(1-x^n) (1<=n<=inf) = 1 + Σ -1^i * (x^(i*(3i-1)/2) + x^(i*(3i+1)/2)   (1<=i<=inf)http://math.josai.ac.jp/~oshima/s2004.pdf P2
fps pow_sigma(int msize){vi reti(msize+1);    reti[0] = 1;    for (int i = 2; i < inf ; i+=2){        int a = (i*(3*i-1))>>1 ;        if(a > msize)break;        reti[a]++;        a = (i*(3*i+1))>>1 ;        if(a > msize)continue;        reti[a]++;    }    for (int i = 1; i < inf ; i+=2){        int a = (i*(3*i-1))>>1 ;        if(a > msize)break;        reti[a]--;        a = (i*(3*i+1))>>1 ;        if(a > msize)continue;        reti[a]--;    }    return reti;}
//閉区間 sigma x^(0~r) = (1 - x^(r+1)) / (1 - x)
//N乗はcombinationで分かる
fps operator*(const fps& a, helper_fps b) { return  mul_sparse(a, (fps)b);}

//Σx^(0~r) = (1-x^(r+1)) / (1-x)
//
// (1-x^r)^nを返す  sigma x^(0~r) = (1 - x^(r+1)) / (1 - x)に注意!  (0~rは r+1乗になる)
fps pow_term2(int r, int n, int msize) {    fps ret(msize + 1);    rep(i, n + 1) {        if (i * r <= msize) {            ret[i * r] = com(n, i);            if (i & 1)ret[i * r] = -ret[i * r];        }    }    return ret;}
// (x^l - x^r)^n
fps pow_term2(int l,int r,int n,int msize){    fps ret = pow_term2(r-l,n,msize);    /*(x^l)^n * (1-x^(r-l))^n*/    ret.rshift(l*n);    if(sz(ret)>msize+1)ret.resize(msize+1);    return ret;}
//1/(1-rx)^nを返す  [x^i]f = nhi * r^i
fps pow_term2_inv(int r, int n, int msize) {/*http://math.josai.ac.jp/~oshima/s2004.pdf P2 */    /*wolfram Series (1/(1-r*x)^m) 級数表現*/    fps ret(msize + 1);    if (r == 1) {        for (int i = 0; i < msize + 1; i++)ret[i] = nhr(n, i);        return ret;    }    mint rb = 1;    for (int i = 0; i < msize + 1; i++) {        ret[i] = nhr(n, i) * rb;        rb *= r;    }    return ret;}
//1/(1-x)^n
fps pow_term2_inv(int n,int msize){return pow_term2_inv(1,n,msize);}
//1/(1-rx^d)^nを返す    1/(1-rx)^n にx=x^dを代入
fps pow_term2_inv(int r, int d, int n, int msize) {    fps ret = pow_term2_inv(r, n, msize);    ret.assign(1, d, msize);    return ret;}/*@formatter:on*/

//割り算の使い方が怪しい
//逆元は使えるため、逆元を掛けたほうがいいかも
//invは長さが元のだけ必要

void solve() {
    din(N, Q);
    na(A, N);
    auto dp =
    fix([&](auto ds, int l, int r) -> fps {
        if (r - l == 1) {
            return {(mint) A[l] - 1, 1};
        } else {
            return ds(l, (l + r) / 2) * ds((l + r) / 2, r);
        }
    })(0, N);
    while(Q--){
        din(b);
        out(dp[b]);
    }
}

signed main() {
    solve();
    return 0;
}
0