#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector< ll> poly;
ll pw( ll a, ll b, ll mod) {
ll ret = 1 ;
while ( b) {
if ( b & 1 ) ret = ret * a % mod;
b >>= 1 ; a = a * a % mod;
}
return ret;
}
template < ll mod, ll w>
class NTT{
public :
void ntt( poly & f, bool inv = 0 ) {
int n = f.size ( ) , j = 0 ;
vector< ll> root( n >> 1 ) ;
for ( int i= 1 ; i< n; i++ ) {
int bit = ( n >> 1 ) ;
while ( j >= bit) {
j - = bit; bit >>= 1 ;
}
j + = bit;
if ( i < j) swap( f[ i] , f[ j] ) ;
}
ll ang = pw( w, ( mod - 1 ) / n, mod) ; if ( inv) ang = pw( ang, mod - 2 , mod) ;
root[ 0 ] = 1 ; for ( int i= 1 ; i< ( n >> 1 ) ; i++ ) root[ i] = root[ i- 1 ] * ang % mod;
for ( int i= 2 ; i<= n; i<<= 1 ) {
int step = n / i;
for ( int j= 0 ; j< n; j+ = i) {
for ( int k= 0 ; k< ( i >> 1 ) ; k++ ) {
ll u = f[ j | k] , v = f[ j | k | i >> 1 ] * root[ step * k] % mod;
f[ j | k] = ( u + v) % mod;
f[ j | k | i >> 1 ] = ( u - v) % mod;
if ( f[ j | k | i >> 1 ] < 0 ) f[ j | k | i >> 1 ] + = mod;
}
}
}
ll t = pw( n, mod - 2 , mod) ;
if ( inv) for ( int i= 0 ; i< n; i++ ) f[ i] = f[ i] * t % mod;
}
vector< ll> multiply( poly & _a, poly & _b) {
vector< ll> a( all( _a) ) , b( all( _b) ) ;
int n = 2 ;
while ( n < a.size ( ) + b.size ( ) ) n <<= 1 ;
a.resize ( n) ; b.resize ( n) ;
ntt( a) ; ntt( b) ;
for ( int i= 0 ; i< n; i++ ) a[ i] = a[ i] * b[ i] % mod;
ntt( a, 1 ) ;
return a;
}
} ;
ll ext_gcd( ll a, ll b, ll & x, ll & y) { //ax + by = gcd(a, b)
ll g = a; x = 1 , y = 0 ;
if ( b) g = ext_gcd( b, a % b, y, x) , y - = a / b * x;
return g;
}
const ll m1 = 2281701377 , m2 = 2483027969 , m3 = 998244353 ;
NTT< m1, 3 > ntt1;
NTT< m2, 3 > ntt2;
NTT< m3, 3 > ntt3;
ll f( const vector< ll> & a, ll mod) {
int sz = a.size ( ) ;
vector< ll> rmn( sz) , lm( sz, 1 ) ;
ll ans = 0 , M = 1 ;
vector< ll> m( { m1, m2, m3} ) ; //prime list
for ( int i= 0 ; i< sz; i++ ) {
ll k = a[ i] - rmn[ i] ; k % = m[ i] ;
if ( k < 0 ) k + = m[ i] ;
ll x, y;
ext_gcd( lm[ i] , m[ i] , x, y) ;
k * = x; k % = m[ i] ;
if ( k < 0 ) k + = m[ i] ;
ans + = k * M % mod;
ans % = mod;
for ( int t= i+ 1 ; t< sz; t++ ) {
rmn[ t] + = lm[ t] * k;
rmn[ t] % = m[ t] ;
lm[ t] * = m[ i] ;
lm[ t] % = m[ t] ;
}
M * = m[ i] ; M % = mod;
}
return ans;
}
poly multiply( poly & a, poly & b, ll mod) {
poly a1( a) , a2( a) , a3( a) ;
poly b1( b) , b2( b) , b3( b) ;
poly res1 = ntt1.multiply ( a1, b1) ;
poly res2 = ntt2.multiply ( a2, b2) ;
poly res3 = ntt3.multiply ( a3, b3) ;
poly ret( res1.size ( ) ) ;
for ( int i= 0 ; i< res1.size ( ) ; i++ ) {
ret[ i] = f( { res1[ i] , res2[ i] , res3[ i] } , mod) ;
}
return ret;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgYWxsKHYpIHYuYmVnaW4oKSwgdi5lbmQoKQp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgdmVjdG9yPGxsPiBwb2x5OwoKbGwgcHcobGwgYSwgbGwgYiwgbGwgbW9kKXsKICAgIGxsIHJldCA9IDE7CiAgICB3aGlsZShiKXsKICAgICAgICBpZihiICYgMSkgcmV0ID0gcmV0ICogYSAlIG1vZDsKICAgICAgICBiID4+PSAxOyBhID0gYSAqIGEgJSBtb2Q7CiAgICB9CiAgICByZXR1cm4gcmV0Owp9Cgp0ZW1wbGF0ZTxsbCBtb2QsIGxsIHc+CmNsYXNzIE5UVHsKcHVibGljOgogICAgdm9pZCBudHQocG9seSAmZiwgYm9vbCBpbnYgPSAwKXsKICAgICAgICBpbnQgbiA9IGYuc2l6ZSgpLCBqID0gMDsKICAgICAgICB2ZWN0b3I8bGw+IHJvb3QobiA+PiAxKTsKICAgICAgICBmb3IoaW50IGk9MTsgaTxuOyBpKyspewogICAgICAgICAgICBpbnQgYml0ID0gKG4gPj4gMSk7CiAgICAgICAgICAgIHdoaWxlKGogPj0gYml0KXsKICAgICAgICAgICAgICAgIGogLT0gYml0OyBiaXQgPj49IDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaiArPSBiaXQ7CiAgICAgICAgICAgIGlmKGkgPCBqKSBzd2FwKGZbaV0sIGZbal0pOwogICAgICAgIH0KICAgICAgICBsbCBhbmcgPSBwdyh3LCAobW9kIC0gMSkgLyBuLCBtb2QpOyBpZihpbnYpIGFuZyA9IHB3KGFuZywgbW9kIC0gMiwgbW9kKTsKICAgICAgICByb290WzBdID0gMTsgZm9yKGludCBpPTE7IGk8KG4gPj4gMSk7IGkrKykgcm9vdFtpXSA9IHJvb3RbaS0xXSAqIGFuZyAlIG1vZDsKICAgICAgICBmb3IoaW50IGk9MjsgaTw9bjsgaTw8PTEpewogICAgICAgICAgICBpbnQgc3RlcCA9IG4gLyBpOwogICAgICAgICAgICBmb3IoaW50IGo9MDsgajxuOyBqKz1pKXsKICAgICAgICAgICAgICAgIGZvcihpbnQgaz0wOyBrPChpID4+IDEpOyBrKyspewogICAgICAgICAgICAgICAgICAgIGxsIHUgPSBmW2ogfCBrXSwgdiA9IGZbaiB8IGsgfCBpID4+IDFdICogcm9vdFtzdGVwICoga10gJSBtb2Q7CiAgICAgICAgICAgICAgICAgICAgZltqIHwga10gPSAodSArIHYpICUgbW9kOwogICAgICAgICAgICAgICAgICAgIGZbaiB8IGsgfCBpID4+IDFdID0gKHUgLSB2KSAlIG1vZDsKICAgICAgICAgICAgICAgICAgICBpZihmW2ogfCBrIHwgaSA+PiAxXSA8IDApIGZbaiB8IGsgfCBpID4+IDFdICs9IG1vZDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBsbCB0ID0gcHcobiwgbW9kIC0gMiwgbW9kKTsKICAgICAgICBpZihpbnYpIGZvcihpbnQgaT0wOyBpPG47IGkrKykgZltpXSA9IGZbaV0gKiB0ICUgbW9kOwogICAgfQogICAgdmVjdG9yPGxsPiBtdWx0aXBseShwb2x5ICZfYSwgcG9seSAmX2IpewogICAgICAgIHZlY3RvcjxsbD4gYShhbGwoX2EpKSwgYihhbGwoX2IpKTsKICAgICAgICBpbnQgbiA9IDI7CiAgICAgICAgd2hpbGUobiA8IGEuc2l6ZSgpICsgYi5zaXplKCkpIG4gPDw9IDE7CiAgICAgICAgYS5yZXNpemUobik7IGIucmVzaXplKG4pOwogICAgICAgIG50dChhKTsgbnR0KGIpOwogICAgICAgIGZvcihpbnQgaT0wOyBpPG47IGkrKykgYVtpXSA9IGFbaV0gKiBiW2ldICUgbW9kOwogICAgICAgIG50dChhLCAxKTsKICAgICAgICByZXR1cm4gYTsKICAgIH0KfTsKCmxsIGV4dF9nY2QobGwgYSwgbGwgYiwgbGwgJngsIGxsICZ5KSB7IC8vYXggKyBieSA9IGdjZChhLCBiKQogIGxsIGcgPSBhOyB4ID0gMSwgeSA9IDA7CiAgaWYgKGIpIGcgPSBleHRfZ2NkKGIsIGEgJSBiLCB5LCB4KSwgeSAtPSBhIC8gYiAqIHg7CiAgcmV0dXJuIGc7Cn0KCmNvbnN0IGxsIG0xID0gMjI4MTcwMTM3NywgbTIgPSAyNDgzMDI3OTY5LCBtMyA9IDk5ODI0NDM1MzsKTlRUPG0xLCAzPiBudHQxOwpOVFQ8bTIsIDM+IG50dDI7Ck5UVDxtMywgMz4gbnR0MzsKCmxsIGYoY29uc3QgdmVjdG9yPGxsPiAmYSwgbGwgbW9kKXsKICAgIGludCBzeiA9IGEuc2l6ZSgpOwogICAgdmVjdG9yPGxsPiBybW4oc3opLCBsbShzeiwgMSk7CiAgICBsbCBhbnMgPSAwLCBNID0gMTsKICAgIHZlY3RvcjxsbD4gbSh7bTEsIG0yLCBtM30pOyAvL3ByaW1lIGxpc3QKCiAgICBmb3IoaW50IGk9MDsgaTxzejsgaSsrKXsKICAgICAgICBsbCBrID0gYVtpXSAtIHJtbltpXTsgayAlPSBtW2ldOwogICAgICAgIGlmKGsgPCAwKSBrICs9IG1baV07CiAgICAgICAgbGwgeCwgeTsKICAgICAgICBleHRfZ2NkKGxtW2ldLCBtW2ldLCB4LCB5KTsKICAgICAgICBrICo9IHg7IGsgJT0gbVtpXTsKICAgICAgICBpZihrIDwgMCkgayArPSBtW2ldOwogICAgICAgIGFucyArPSBrICogTSAlIG1vZDsKICAgICAgICBhbnMgJT0gbW9kOwogICAgICAgIGZvcihpbnQgdD1pKzE7IHQ8c3o7IHQrKyl7CiAgICAgICAgICAgIHJtblt0XSArPSBsbVt0XSAqIGs7CiAgICAgICAgICAgIHJtblt0XSAlPSBtW3RdOwogICAgICAgICAgICBsbVt0XSAqPSBtW2ldOwogICAgICAgICAgICBsbVt0XSAlPSBtW3RdOwogICAgICAgIH0KICAgICAgICBNICo9IG1baV07IE0gJT0gbW9kOwogICAgfQogICAgcmV0dXJuIGFuczsKfQoKcG9seSBtdWx0aXBseShwb2x5ICZhLCBwb2x5ICZiLCBsbCBtb2QpewogICAgcG9seSBhMShhKSwgYTIoYSksIGEzKGEpOwogICAgcG9seSBiMShiKSwgYjIoYiksIGIzKGIpOwogICAgcG9seSByZXMxID0gbnR0MS5tdWx0aXBseShhMSwgYjEpOwogICAgcG9seSByZXMyID0gbnR0Mi5tdWx0aXBseShhMiwgYjIpOwogICAgcG9seSByZXMzID0gbnR0My5tdWx0aXBseShhMywgYjMpOwogICAgcG9seSByZXQocmVzMS5zaXplKCkpOwogICAgZm9yKGludCBpPTA7IGk8cmVzMS5zaXplKCk7IGkrKyl7CiAgICAgICAgcmV0W2ldID0gZih7cmVzMVtpXSwgcmVzMltpXSwgcmVzM1tpXX0sIG1vZCk7CiAgICB9CiAgICByZXR1cm4gcmV0Owp9