#ifdef DEBUG #define _GLIBCXX_DEBUG #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ); signal( SIGABRT , &AlertAbort ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define CERR( MESSAGE ) cerr << MESSAGE << endl; #define COUT( ANSWER ) cout << ANSWER << endl #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " << ( MIN ) << ( ( MIN ) <= A ? "<=" : ">" ) << A << ( A <= ( MAX ) ? "<=" : ">" ) << ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define LIBRARY_SEARCH bool searched_library = false; LibrarySearch( searched_library ); if( searched_library ){ QUIT; }; #define START_WATCH( PROCESS_NAME ) StartWatch( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) StopWatch( HOW_MANY_TIMES ) #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define CERR( MESSAGE ) #define COUT( ANSWER ) cout << ANSWER << "\n" #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define LIBRARY_SEARCH #define START_WATCH( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) #endif // #define RANDOM_TEST #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define GETLINE( A ) string A; getline( cin , A ) #define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ ) #define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- ) #define AUTO_ITR( ARRAY ) auto itr_ ## ARRAY = ARRAY .begin() , end_ ## ARRAY = ARRAY .end() #define FOR_ITR( ARRAY ) for( AUTO_ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS_ ) #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void StartWatch( const string& process_name = "nothing" ); void StopWatch( const int& how_many_times = 1 ); #endif #if defined( DEBUG ) && defined( RANDOM_TEST ) inline CEXPR( int , bound_random_test_num , 1000 ); #define START_MAIN FOR( random_test_num , 0 , bound_random_test_num ){ CERR( "(" << random_test_num << ")" ); ll GetRand( const ll& Rand_min , const ll& Rand_max ); #define SET_ASSERT( A , MIN , MAX ) CERR( #A << " = " << ( A = GetRand( MIN , MAX ) ) ) #define RETURN( ANSWER ) if( ( ANSWER ) == guchoku ){ CERR( ( ANSWER ) << " == " << guchoku ); continue; } else { CERR( ( ANSWER ) << " != " << guchoku ); QUIT; } #define FINISH_MAIN CERR( "" ); } #else #define START_MAIN #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #define FINISH_MAIN #endif template inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same::value && ! is_same::value ); \ TYPE_OF( ARGUMENT ) ANSWER{ 1 }; \ { \ TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER; \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO ) \ ll ANSWER{ 1 }; \ { \ ll ARGUMENT_FOR_SQUARE_FOR_POWER = ( ( MODULO ) + ( ( ARGUMENT ) % ( MODULO ) ) ) % ( MODULO ); \ TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT ); \ while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){ \ if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){ \ ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ } \ ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % ( MODULO ); \ EXPONENT_FOR_SQUARE_FOR_POWER /= 2; \ } \ } \ #define FACTORIAL_MOD( ANSWER , ANSWER_INV , INVERSE , MAX_INDEX , CONSTEXPR_LENGTH , MODULO ) \ static ll ANSWER[CONSTEXPR_LENGTH]; \ static ll ANSWER_INV[CONSTEXPR_LENGTH]; \ static ll INVERSE[CONSTEXPR_LENGTH]; \ { \ ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL; \ FOREQ( i , 1 , MAX_INDEX ){ \ ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= ( MODULO ); \ } \ ANSWER_INV[0] = ANSWER_INV[1] = INVERSE[1] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ FOREQ( i , 2 , MAX_INDEX ){ \ ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = ( MODULO ) - ( ( ( ( MODULO ) / i ) * INVERSE[ ( MODULO ) % i ] ) % ( MODULO ) ) ) %= ( MODULO ); \ } \ } \ // 二分探索テンプレート // EXPRESSIONがANSWERの広義単調関数の時、EXPRESSION >= TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ static_assert( ! is_same::value && ! is_same::value ); \ ll ANSWER = MINIMUM; \ if( MINIMUM <= MAXIMUM ){ \ ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM; \ ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM; \ ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \ ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \ while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \ VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \ CERR( "二分探索中: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \ if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH INEQUALITY_FOR_CHECK 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = UPDATE_U; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ CERR( "二分探索終了: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << ( EXPRESSION > TARGET ? ">" : EXPRESSION < TARGET ? "<" : "=" ) << TARGET ); \ CERR( ( EXPRESSION DESIRED_INEQUALITY TARGET ? "二分探索成功" : "二分探索失敗" ) ); \ assert( EXPRESSION DESIRED_INEQUALITY TARGET ); \ } else { \ CERR( "二分探索失敗: " << MINIMUM << ">" << MAXIMUM ); \ assert( MINIMUM <= MAXIMUM ); \ } \ // 単調増加の時にEXPRESSION >= TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , >= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調増加の時にEXPRESSION <= TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , > , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION >= TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , < , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION <= TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , <= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 圧縮用 #define TE template #define TY typename #define US using #define ST static #define IN inline #define CL class #define PU public #define OP operator #define CE constexpr #define CO const #define NE noexcept #define RE return #define WH while #define VO void #define VE vector #define LI list #define BE begin #define EN end #define SZ size #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& // 配列の各要素がint型の範疇でも総和がそうでない場合はTをint型にすると正しく動作しないことに注意。 // InitialSegmentSumで負の入力を扱うためにuintではなくintをテンプレート引数にする。 // 使用演算: // T& T::operator=( const T& ) // T& T::operator+=( const T& ) // T operator-( const T& , const T& )(ただしIntervalSumを用いない場合は不要) // T operator<( const T& , const T& )(ただしBinarySearchを用いない場合は不要) template class BIT { private: T m_fenwick[N + 1]; public: inline BIT(); BIT( const T ( & a )[N] ); // const参照でないことに注意。 inline T Get( const int& i ) const; inline void Set( const int& i , const T& n ); inline BIT& operator+=( const T ( & a )[N] ); void Add( const int& i , const T& n ); T InitialSegmentSum( const int& i_final ) const; inline T IntervalSum( const int& i_start , const int& i_final ) const; // operator+=の単位元T()より小さくない要素のみを成分に持つ場合のみサポート。 // InitialSegmentSum( i )がn以上となるiが存在する場合にその最小値を2進法で探索。 int BinarySearch( const T& n ) const; // IntervalSum( i_start , i )がt以上となるi_start以上のiが存在する場合にその最小値を2進法で探索。 inline int BinarySearch( const int& i_start , const T& n ) const; }; template inline BIT::BIT() : m_fenwick() {} template BIT::BIT( const T ( & a )[N] ) : m_fenwick() { for( int j = 1 ; j <= N ; j++ ){ T& fenwick_j = m_fenwick[j]; int i = j - 1; fenwick_j = a[i]; int i_lim = j - ( j & -j ); while( i != i_lim ){ fenwick_j += m_fenwick[i]; i -= ( i & -i ); } } } template inline T BIT::Get( const int& i ) const { return IntervalSum( i , i ); } template inline void BIT::Set( const int& i , const T& n ) { Add( i , n - IntervalSum( i , i ) ); } template inline BIT& BIT::operator+=( const T ( & a )[N] ) { for( int i = 0 ; i < N ; i++ ){ Add( i , a[i] ); } return *this; } template void BIT::Add( const int& i , const T& n ) { int j = i + 1; while( j <= N ){ m_fenwick[j] += n; j += ( j & -j ); } return; } template T BIT::InitialSegmentSum( const int& i_final ) const { T sum = 0; int j = ( i_final < N ? i_final : N - 1 ) + 1; while( j > 0 ){ sum += m_fenwick[j]; j -= j & -j; } return sum; } template inline T BIT::IntervalSum( const int& i_start , const int& i_final ) const { return InitialSegmentSum( i_final ) - InitialSegmentSum( i_start - 1 ); } // inline CEXPR( int , bound_N , 10 ); inline DEXPR( int , bound_N , 2000 , 10 ); // inline CEXPR( int , bound_N , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 inline CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // inline CEXPR( int , bound_M , 10 ); // inline DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // inline CEXPR( int , bound_M , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 inline CEXPR( TYPE_OF( bound_N ) , bound_K , bound_N ); // inline DEXPR( int , bound_H , 1000 , 10 ); // // inline DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // // inline CEXPR( int , bound_H , 1000000000 ); // 0が9個 // inline CEXPR( int , bound_W , bound_H ); // #if bound_H < ( 1 << 16 ) // inline CEXPR( ll , bound_HW , ll( bound_H ) * bound_W ); // #else // inline CEXPR( int , bound_HW , bound_H * bound_W ); // #endif // // CEXPR( int , bound_HW , 100000 ); // 0が5個 // // CEXPR( int , bound_HW , 1000000000 ); // 0が5個 // list e[bound_N]; // list E( const int& i ) // { // list answer = e[i]; // // 入力によらない処理 // return answer; // } // inline CEXPR( ll , P , 998244353 ); // inline CEXPR( ll , P , 1000000007 ); US ull = unsigned long long;IN CEXPR(uint,P,998244353);TE IN CE INT& RS(INT& n)NE{RE n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n %= M;}TE IN CE uint& RS(uint& n)NE{RE n %= M;}TE IN CE ull& RS(ull& n)NE{RE n %= M;}TE IN CE INT& RSP(INT& n)NE{CE CO uint trunc = (1 << 23)- 1;INT n_u = n >> 23;n &= trunc;INT n_uq = (n_u / 7)/ 17;n_u -= n_uq * 119;n += n_u << 23;RE n < n_uq?n += P - n_uq:n -= n_uq;}TE <> IN CE ull& RS(ull& n)NE{CE CO ull Pull = P;CE CO ull Pull2 = (Pull - 1)* (Pull - 1);RE RSP(n > Pull2?n -= Pull2:n);}TE IN CE INT RS(INT&& n)NE{RE MO(RS(n));}TE IN CE INT RS(CO INT& n)NE{RE RS(INT(n));} #define SFINAE_FOR_MOD(DEFAULT)TY T,enable_if_t >::value>* DEFAULT #define DC_OF_CM_FOR_MOD(FUNC)IN bool OP FUNC(CO Mod& n)CO NE #define DC_OF_AR_FOR_MOD(FUNC)IN Mod OP FUNC(CO Mod& n)CO NE;TE IN Mod OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MOD(FUNC)TE IN bool Mod::OP FUNC(CO Mod& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MOD(FUNC,FORMULA)TE IN Mod Mod::OP FUNC(CO Mod& n)CO NE{RE MO(Mod(*TH)FUNC ## = n);}TE TE IN Mod Mod::OP FUNC(T&& n)CO NE{RE FORMULA;}TE IN Mod OP FUNC(T&& n0,CO Mod& n1)NE{RE MO(Mod(forward(n0))FUNC ## = n1);} TE CL Mod{PU:uint m_n;IN CE Mod()NE;IN CE Mod(CO Mod& n)NE;IN CE Mod(Mod& n)NE;IN CE Mod(Mod&& n)NE;TE IN CE Mod(CO T& n)NE;TE IN CE Mod(T& n)NE;TE IN CE Mod(T&& n)NE;IN CE Mod& OP=(CO Mod& n)NE;IN CE Mod& OP=(Mod&& n)NE;IN CE Mod& OP+=(CO Mod& n)NE;IN CE Mod& OP-=(CO Mod& n)NE;IN CE Mod& OP*=(CO Mod& n)NE;IN Mod& OP/=(CO Mod& n);IN CE Mod& OP<<=(int n)NE;IN CE Mod& OP>>=(int n)NE;IN CE Mod& OP++()NE;IN CE Mod OP++(int)NE;IN CE Mod& OP--()NE;IN CE Mod OP--(int)NE;DC_OF_CM_FOR_MOD(==);DC_OF_CM_FOR_MOD(!=);DC_OF_CM_FOR_MOD(<);DC_OF_CM_FOR_MOD(<=);DC_OF_CM_FOR_MOD(>);DC_OF_CM_FOR_MOD(>=);DC_OF_AR_FOR_MOD(+);DC_OF_AR_FOR_MOD(-);DC_OF_AR_FOR_MOD(*);DC_OF_AR_FOR_MOD(/);IN CE Mod OP<<(int n)CO NE;IN CE Mod OP>>(int n)CO NE;IN CE Mod OP-()CO NE;IN CE Mod& SignInvert()NE;IN CE Mod& Double()NE;IN CE Mod& Halve()NE;IN Mod& Invert();TE IN CE Mod& PositivePW(T&& EX)NE;TE IN CE Mod& NonNegativePW(T&& EX)NE;TE IN CE Mod& PW(T&& EX);IN CE VO swap(Mod& n)NE;IN CE CO uint& RP()CO NE;ST IN CE Mod DeRP(CO uint& n)NE;ST IN CE uint& Normalise(uint& n)NE;ST IN CO Mod& Inverse(CO uint& n)NE;ST IN CO Mod& Factorial(CO uint& n)NE;ST IN CO Mod& FactorialInverse(CO uint& n)NE;ST IN CO Mod& zero()NE;ST IN CO Mod& one()NE;TE IN CE Mod& Ref(T&& n)NE;}; #define SFINAE_FOR_MN(DEFAULT)TY T,enable_if_t,decay_t >::value>* DEFAULT #define DC_OF_AR_FOR_MN(FUNC)IN MN OP FUNC(CO MN& n)CO NE;TE IN MN OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MN(FUNC)TE IN bool MN::OP FUNC(CO MN& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MN(FUNC,FORMULA)TE IN MN MN::OP FUNC(CO MN& n)CO NE{RE MO(MN(*TH)FUNC ## = n);}TE TE IN MN MN::OP FUNC(T&& n)CO NE{RE FORMULA;}TE IN MN OP FUNC(T&& n0,CO MN& n1)NE{RE MO(MN(forward(n0))FUNC ## = n1);} TE CL MN :PU Mod{PU:IN CE MN()NE;IN CE MN(CO MN& n)NE;IN CE MN(MN& n)NE;IN CE MN(MN&& n)NE;TE IN CE MN(CO T& n)NE;TE IN CE MN(T&& n)NE;IN CE MN& OP=(CO MN& n)NE;IN CE MN& OP=(MN&& n)NE;IN CE MN& OP+=(CO MN& n)NE;IN CE MN& OP-=(CO MN& n)NE;IN CE MN& OP*=(CO MN& n)NE;IN MN& OP/=(CO MN& n);IN CE MN& OP<<=(int n)NE;IN CE MN& OP>>=(int n)NE;IN CE MN& OP++()NE;IN CE MN OP++(int)NE;IN CE MN& OP--()NE;IN CE MN OP--(int)NE;DC_OF_AR_FOR_MN(+);DC_OF_AR_FOR_MN(-);DC_OF_AR_FOR_MN(*);DC_OF_AR_FOR_MN(/);IN CE MN OP<<(int n)CO NE;IN CE MN OP>>(int n)CO NE;IN CE MN OP-()CO NE;IN CE MN& SignInvert()NE;IN CE MN& Double()NE;IN CE MN& Halve()NE;IN CE MN& Invert();TE IN CE MN& PositivePW(T&& EX)NE;TE IN CE MN& NonNegativePW(T&& EX)NE;TE IN CE MN& PW(T&& EX);IN CE uint RP()CO NE;IN CE Mod Reduce()CO NE;ST IN CE MN DeRP(CO uint& n)NE;ST IN CO MN& Formise(CO uint& n)NE;ST IN CO MN& Inverse(CO uint& n)NE;ST IN CO MN& Factorial(CO uint& n)NE;ST IN CO MN& FactorialInverse(CO uint& n)NE;ST IN CO MN& zero()NE;ST IN CO MN& one()NE;ST IN CE uint Form(CO uint& n)NE;ST IN CE ull& Reduction(ull& n)NE;ST IN CE ull& ReducedMU(ull& n,CO uint& m)NE;ST IN CE uint MU(CO uint& n0,CO uint& n1)NE;ST IN CE uint BaseSquareTruncation(uint& n)NE;TE IN CE MN& Ref(T&& n)NE;};TE IN CE MN Twice(CO MN& n)NE;TE IN CE MN Half(CO MN& n)NE;TE IN CE MN Inverse(CO MN& n);TE IN CE MN PW(CO MN& n,CO T& EX);TE IN CE MN<2> PW(CO MN<2>& n,CO T& p);TE IN CE T Square(CO T& t);TE <> IN CE MN<2> Square >(CO MN<2>& t);TE IN CE VO swap(MN& n0,MN& n1)NE;TE IN string to_string(CO MN& n)NE;TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n); TE CL COantsForMod{PU:COantsForMod()= delete;ST CE CO bool g_even = ((M & 1)== 0);ST CE CO uint g_memory_bound = 1000000;ST CE CO uint g_memory_LE = M < g_memory_bound?M:g_memory_bound;ST IN CE ull MNBasePW(ull&& EX)NE;ST CE uint g_M_minus = M - 1;ST CE uint g_M_minus_2 = M - 2;ST CE uint g_M_minus_2_neg = 2 - M;ST CE CO int g_MN_digit = 32;ST CE CO ull g_MN_base = ull(1)<< g_MN_digit;ST CE CO uint g_MN_base_minus = uint(g_MN_base - 1);ST CE CO uint g_MN_digit_half = (g_MN_digit + 1)>> 1;ST CE CO uint g_MN_base_sqrt_minus = (1 << g_MN_digit_half)- 1;ST CE CO uint g_MN_M_neg_inverse = uint((g_MN_base - MNBasePW((ull(1)<< (g_MN_digit - 1))- 1))& g_MN_base_minus);ST CE CO uint g_MN_base_mod = uint(g_MN_base % M);ST CE CO uint g_MN_base_square_mod = uint(((g_MN_base % M)* (g_MN_base % M))% M);};TE IN CE ull COantsForMod::MNBasePW(ull&& EX)NE{ull prod = 1;ull PW = M;WH(EX != 0){(EX & 1)== 1?(prod *= PW)&= g_MN_base_minus:prod;EX >>= 1;(PW *= PW)&= g_MN_base_minus;}RE prod;} #include #define SET_VE_32_128_FOR_SIMD(UINT,VE_NAME,SCALAR0,SCALAR1,SCALAR2,SCALAR3)CE CO UINT VE_NAME ## _copy[4] ={SCALAR0,SCALAR1,SCALAR2,SCALAR3};ST CO __m128i v_ ## VE_NAME = _mm_load_si128((__m128i*)VE_NAME ##_copy) #define SET_VE_64_128_FOR_SIMD(UINT,VE_NAME,SCALAR0,SCALAR1)CE CO UINT VE_NAME ## _copy[2] ={SCALAR0,SCALAR1};ST CO __m128i v_ ## VE_NAME = _mm_load_si128((__m128i*)VE_NAME ##_copy) #define SET_VE_64_256_FOR_SIMD(ULL,VE_NAME,SCALAR0,SCALAR1,SCALAR2,SCALAR3)CE CO ULL VE_NAME ## _copy[4] ={SCALAR0,SCALAR1,SCALAR2,SCALAR3};ST CO __m256i v_ ## VE_NAME = _mm256_load_si256((__m256i*)VE_NAME ##_copy) #define SET_CO_VE_32_128_FOR_SIMD(UINT,VE_NAME,SCALAR)SET_VE_32_128_FOR_SIMD(UINT,VE_NAME,SCALAR,SCALAR,SCALAR,SCALAR) #define SET_CO_VE_64_128_FOR_SIMD(ULL,VE_NAME,SCALAR)SET_VE_64_128_FOR_SIMD(ULL,VE_NAME,SCALAR,SCALAR) #define SET_CO_VE_64_256_FOR_SIMD(ULL,VE_NAME,SCALAR)SET_VE_64_256_FOR_SIMD(ULL,VE_NAME,SCALAR,SCALAR,SCALAR,SCALAR) TE CL COantsForSIMDForMod{PU:COantsForSIMDForMod()= delete;ST IN CO __m128i& v_M()NE;ST IN CO __m128i& v_Mull()NE;ST IN CO __m128i& v_M_minus()NE;ST IN CO __m128i& v_M_neg_inverse()NE;ST IN CO __m128i& v_digitull()NE;};TE IN CO __m128i& COantsForSIMDForMod::v_M()NE{SET_CO_VE_32_128_FOR_SIMD(uint,M,M);RE v_M;}TE IN CO __m128i& COantsForSIMDForMod::v_Mull()NE{SET_CO_VE_64_128_FOR_SIMD(ull,Mull,M);RE v_Mull;}TE IN CO __m128i& COantsForSIMDForMod::v_M_minus()NE{SET_CO_VE_32_128_FOR_SIMD(uint,M_minus,M - 1);RE v_M_minus;}TE IN CO __m128i& COantsForSIMDForMod::v_M_neg_inverse()NE{SET_CO_VE_32_128_FOR_SIMD(uint,M_neg_inverse,COantsForMod::g_MN_M_neg_inverse);RE v_M_neg_inverse;}TE IN CO __m128i& COantsForSIMDForMod::v_digitull()NE{SET_CO_VE_64_128_FOR_SIMD(ull,digitull,COantsForMod::g_MN_digit);RE v_digitull;}TE IN __m128i& SIMD_RS_32_128(__m128i& v)NE{CO __m128i& v_M = COantsForSIMDForMod::v_M();RE v -= v_M * _mm_cmpgt_epi32(v,v_M);}TE IN __m128i& SIMD_RS_64_128(__m128i& v)NE{ull v_copy[2];_mm_store_si128((__m128i*)v_copy,v);for(uint i = 0;i < 2;i++){ull& v_copy_i = v_copy[i];v_copy_i = (v_copy_i < M?0:M);}RE v -= _mm_load_si128((__m128i*)v_copy);}TE IN __m256i& SIMD_RS_64_256(__m256i& v)NE{ull v_copy[4];_mm256_store_si256((__m256i*)v_copy,v);for(uint i = 0;i < 4;i++){ull& v_copy_i = v_copy[i];v_copy_i = (v_copy_i < M?0:M);}RE v -= _mm256_load_si256((__m256i*)v_copy);}IN CE int SIMD_Shuffle(CRI a0,CRI a1,CRI a2,CRI a3)NE{RE (a0 << (0 << 1))+ (a1 << (1 << 1))+ (a2 << (2 << 1))+ (a3 << (3 << 1));}TE IN VO SIMD_Addition_32_64(CO Mod& a0,CO Mod& a1,CO Mod& b0,CO Mod& b1,Mod& c0,Mod& c1)NE{uint a_copy[4] ={a0.m_n,a1.m_n,0,0};uint b_copy[4] ={b0.m_n,b1.m_n,0,0};__m128i v_a = _mm_load_si128((__m128i*)a_copy);v_a += _mm_load_si128((__m128i*)b_copy);ST CO __m128i& v_M_minus = COantsForSIMDForMod::v_M_minus();ST CO __m128i& v_M = COantsForSIMDForMod::v_M();v_a += _mm_cmpgt_epi32(v_a,v_M_minus)& v_M;_mm_store_si128((__m128i*)a_copy,v_a);c0.m_n = MO(a_copy[0]);c1.m_n = MO(a_copy[1]);RE;}TE IN VO SIMD_Addition_32_128(CO Mod& a0,CO Mod& a1,CO Mod& a2,CO Mod& a3,CO Mod& b0,CO Mod& b1,CO Mod& b2,CO Mod& b3,Mod& c0,Mod& c1,Mod& c2,Mod& c3)NE{uint a_copy[4] ={a0.m_n,a1.m_n,a2.m_n,a3.m_n};uint b_copy[4] ={b0.m_n,b1.m_n,b2.m_n,b3.m_n};__m128i v_a = _mm_load_si128((__m128i*)a_copy)+ _mm_load_si128((__m128i*)b_copy);_mm_store_si128((__m128i*)a_copy,v_a);for(uint i = 0;i < 4;i++){b_copy[i] = a_copy[i] < M?0:M;}v_a -= _mm_load_si128((__m128i*)b_copy);_mm_store_si128((__m128i*)a_copy,v_a);c0.m_n = MO(a_copy[0]);c1.m_n = MO(a_copy[1]);c2.m_n = MO(a_copy[2]);c3.m_n = MO(a_copy[3]);RE;}TE IN VO SIMD_Substracition_32_64(CO Mod& a0,CO Mod& a1,CO Mod& b0,CO Mod& b1,Mod& c0,Mod& c1)NE{uint a_copy[4] ={a0.m_n,a1.m_n,0,0};uint b_copy[4] ={b0.m_n,b1.m_n,0,0};__m128i v_a = _mm_load_si128((__m128i*)a_copy);__m128i v_b = _mm_load_si128((__m128i*)b_copy);_mm_store_si128((__m128i*)a_copy,v_a);for(uint i = 0;i < 2;i++){b_copy[i] = a_copy[i] < b_copy[i]?M:0;}(v_a += _mm_load_si128((__m128i*)b_copy))-= v_b;_mm_store_si128((__m128i*)a_copy,v_a);c0.m_n = MO(a_copy[0]);c1.m_n = MO(a_copy[1]);RE;}TE IN VO SIMD_Subtraction_32_128(CO Mod& a0,CO Mod& a1,CO Mod& a2,CO Mod& a3,CO Mod& b0,CO Mod& b1,CO Mod& b2,CO Mod& b3,Mod& c0,Mod& c1,Mod& c2,Mod& c3)NE{uint a_copy[4] ={a0.m_n,a1.m_n,a2.m_n,a3.m_n};uint b_copy[4] ={b0.m_n,b1.m_n,b2.m_n,b3.m_n};__m128i v_a = _mm_load_si128((__m128i*)a_copy);__m128i v_b = _mm_load_si128((__m128i*)b_copy);_mm_store_si128((__m128i*)a_copy,v_a);for(uint i = 0;i < 4;i++){b_copy[i] = a_copy[i] < b_copy[i]?M:0;}(v_a += _mm_load_si128((__m128i*)b_copy))-= v_b;_mm_store_si128((__m128i*)a_copy,v_a);c0.m_n = MO(a_copy[0]);c1.m_n = MO(a_copy[1]);c2.m_n = MO(a_copy[2]);c3.m_n = MO(a_copy[3]);RE;} US MP = Mod

;US MNP = MN

;TE IN CE uint MN::Form(CO uint& n)NE{ull n_copy = n;RE uint(MO(Reduction(n_copy *= COantsForMod::g_MN_base_square_mod)));}TE IN CE ull& MN::Reduction(ull& n)NE{ull n_sub = n & COantsForMod::g_MN_base_minus;RE ((n += ((n_sub *= COantsForMod::g_MN_M_neg_inverse)&= COantsForMod::g_MN_base_minus)*= M)>>= COantsForMod::g_MN_digit)< M?n:n -= M;}TE IN CE ull& MN::ReducedMU(ull& n,CO uint& m)NE{RE Reduction(n *= m);}TE IN CE uint MN::MU(CO uint& n0,CO uint& n1)NE{ull n0_copy = n0;RE uint(MO(ReducedMU(ReducedMU(n0_copy,n1),COantsForMod::g_MN_base_square_mod)));}TE IN CE uint MN::BaseSquareTruncation(uint& n)NE{CO uint n_u = n >> COantsForMod::g_MN_digit_half;n &= COantsForMod::g_MN_base_sqrt_minus;RE n_u;}TE IN CE MN::MN()NE:Mod(){static_assert(! COantsForMod::g_even);}TE IN CE MN::MN(CO MN& n)NE:Mod(n){}TE IN CE MN::MN(MN& n)NE:Mod(n){}TE IN CE MN::MN(MN&& n)NE:Mod(MO(n)){}TE TE IN CE MN::MN(CO T& n)NE:Mod(n){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE TE IN CE MN::MN(T&& n)NE:Mod(forward(n)){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE IN CE MN& MN::OP=(CO MN& n)NE{RE Ref(Mod::OP=(n));}TE IN CE MN& MN::OP=(MN&& n)NE{RE Ref(Mod::OP=(MO(n)));}TE IN CE MN& MN::OP+=(CO MN& n)NE{RE Ref(Mod::OP+=(n));}TE IN CE MN& MN::OP-=(CO MN& n)NE{RE Ref(Mod::OP-=(n));}TE IN CE MN& MN::OP*=(CO MN& n)NE{ull m_n_copy = Mod::m_n;RE Ref(Mod::m_n = MO(ReducedMU(m_n_copy,n.m_n)));}TE IN MN& MN::OP/=(CO MN& n){RE OP*=(MN(n).Invert());}TE IN CE MN& MN::OP<<=(int n)NE{RE Ref(Mod::OP<<=(n));}TE IN CE MN& MN::OP>>=(int n)NE{RE Ref(Mod::OP>>=(n));}TE IN CE MN& MN::OP++()NE{RE Ref(Mod::Normalise(Mod::m_n += COantsForMod::g_MN_base_mod));}TE IN CE MN MN::OP++(int)NE{MN n{*TH};OP++();RE n;}TE IN CE MN& MN::OP--()NE{RE Ref(Mod::m_n < COantsForMod::g_MN_base_mod?((Mod::m_n += M)-= COantsForMod::g_MN_base_mod):Mod::m_n -= COantsForMod::g_MN_base_mod);}TE IN CE MN MN::OP--(int)NE{MN n{*TH};OP--();RE n;}DF_OF_AR_FOR_MN(+,MN(forward(n))+= *TH);DF_OF_AR_FOR_MN(-,MN(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MN(*,MN(forward(n))*= *TH);DF_OF_AR_FOR_MN(/,MN(forward(n)).Invert()*= *TH);TE IN CE MN MN::OP<<(int n)CO NE{RE MO(MN(*TH)<<= n);}TE IN CE MN MN::OP>>(int n)CO NE{RE MO(MN(*TH)>>= n);}TE IN CE MN MN::OP-()CO NE{RE MO(MN(*TH).SignInvert());}TE IN CE MN& MN::SignInvert()NE{RE Ref(Mod::m_n > 0?Mod::m_n = M - Mod::m_n:Mod::m_n);}TE IN CE MN& MN::Double()NE{RE Ref(Mod::Double());}TE IN CE MN& MN::Halve()NE{RE Ref(Mod::Halve());}TE IN CE MN& MN::Invert(){assert(Mod::m_n > 0);RE PositivePW(uint(COantsForMod::g_M_minus_2));}TE TE IN CE MN& MN::PositivePW(T&& EX)NE{MN PW{*TH};(--EX)%= COantsForMod::g_M_minus_2;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE TE IN CE MN& MN::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(Mod::m_n = 1):PositivePW(forward(EX));}TE TE IN CE MN& MN::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod::m_n == 0));RE neg?PositivePW(forward(EX *= COantsForMod::g_M_minus_2_neg)):NonNegativePW(forward(EX));}TE IN CE uint MN::RP()CO NE{ull m_n_copy = Mod::m_n;RE MO(Reduction(m_n_copy));}TE IN CE Mod MN::Reduce()CO NE{ull m_n_copy = Mod::m_n;RE Mod::DeRP(MO(Reduction(m_n_copy)));}TE IN CE MN MN::DeRP(CO uint& n)NE{RE MN(Mod::DeRP(n));}TE IN CO MN& MN::Formise(CO uint& n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = DeRP(LE_curr);LE_curr++;}RE memory[n];}TE IN CO MN& MN::Inverse(CO uint& n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN(Mod::Inverse(LE_curr));LE_curr++;}RE memory[n];}TE IN CO MN& MN::Factorial(CO uint& n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};MN val_last{one()};WH(LE_curr <= n){memory[LE_curr++] = val_curr *= ++val_last;}RE memory[n];}TE IN CO MN& MN::FactorialInverse(CO uint& n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};MN val_last{one()};WH(LE_curr <= n){memory[LE_curr] = val_curr *= Inverse(LE_curr);LE_curr++;}RE memory[n];}TE IN CO MN& MN::zero()NE{ST CE CO MN z{};RE z;}TE IN CO MN& MN::one()NE{ST CE CO MN o{DeRP(1)};RE o;}TE TE IN CE MN& MN::Ref(T&& n)NE{RE *TH;}TE IN CE MN Twice(CO MN& n)NE{RE MO(MN(n).Double());}TE IN CE MN Half(CO MN& n)NE{RE MO(MN(n).Halve());}TE IN CE MN Inverse(CO MN& n){RE MO(MN(n).Invert());}TE IN CE MN PW(CO MN& n,CO T& EX){RE MO(MN(n).PW(T(EX)));}TE IN CE VO swap(MN& n0,MN& n1)NE{n0.swap(n1);}TE IN string to_string(CO MN& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n){RE os << n.RP();} TE IN CE Mod::Mod()NE:m_n(){}TE IN CE Mod::Mod(CO Mod& n)NE:m_n(n.m_n){}TE IN CE Mod::Mod(Mod& n)NE:m_n(n.m_n){}TE IN CE Mod::Mod(Mod&& n)NE:m_n(MO(n.m_n)){}TE TE IN CE Mod::Mod(CO T& n)NE:m_n(RS(n)){}TE TE IN CE Mod::Mod(T& n)NE:m_n(RS(decay_t(n))){}TE TE IN CE Mod::Mod(T&& n)NE:m_n(RS(forward(n))){}TE IN CE Mod& Mod::OP=(CO Mod& n)NE{RE Ref(m_n = n.m_n);}TE IN CE Mod& Mod::OP=(Mod&& n)NE{RE Ref(m_n = MO(n.m_n));}TE IN CE Mod& Mod::OP+=(CO Mod& n)NE{RE Ref(Normalise(m_n += n.m_n));}TE IN CE Mod& Mod::OP-=(CO Mod& n)NE{RE Ref(m_n < n.m_n?(m_n += M)-= n.m_n:m_n -= n.m_n);}TE IN CE Mod& Mod::OP*=(CO Mod& n)NE{RE Ref(m_n = COantsForMod::g_even?RS(ull(m_n)* n.m_n):MN::MU(m_n,n.m_n));}TE <> IN CE MP& MP::OP*=(CO MP& n)NE{ull m_n_copy = m_n;RE Ref(m_n = MO((m_n_copy *= n.m_n)< P?m_n_copy:RSP(m_n_copy)));}TE IN Mod& Mod::OP/=(CO Mod& n){RE OP*=(Mod(n).Invert());}TE IN CE Mod& Mod::OP<<=(int n)NE{WH(n-- > 0){Normalise(m_n <<= 1);}RE *TH;}TE IN CE Mod& Mod::OP>>=(int n)NE{WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += M)>>= 1;}RE *TH;}TE IN CE Mod& Mod::OP++()NE{RE Ref(m_n < COantsForMod::g_M_minus?++m_n:m_n = 0);}TE IN CE Mod Mod::OP++(int)NE{Mod n{*TH};OP++();RE n;}TE IN CE Mod& Mod::OP--()NE{RE Ref(m_n == 0?m_n = COantsForMod::g_M_minus:--m_n);}TE IN CE Mod Mod::OP--(int)NE{Mod n{*TH};OP--();RE n;}DF_OF_CM_FOR_MOD(==);DF_OF_CM_FOR_MOD(!=);DF_OF_CM_FOR_MOD(>);DF_OF_CM_FOR_MOD(>=);DF_OF_CM_FOR_MOD(<);DF_OF_CM_FOR_MOD(<=);DF_OF_AR_FOR_MOD(+,Mod(forward(n))+= *TH);DF_OF_AR_FOR_MOD(-,Mod(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MOD(*,Mod(forward(n))*= *TH);DF_OF_AR_FOR_MOD(/,Mod(forward(n)).Invert()*= *TH);TE IN CE Mod Mod::OP<<(int n)CO NE{RE MO(Mod(*TH)<<= n);}TE IN CE Mod Mod::OP>>(int n)CO NE{RE MO(Mod(*TH)>>= n);}TE IN CE Mod Mod::OP-()CO NE{RE MO(Mod(*TH).SignInvert());}TE IN CE Mod& Mod::SignInvert()NE{RE Ref(m_n > 0?m_n = M - m_n:m_n);}TE IN CE Mod& Mod::Double()NE{RE Ref(Normalise(m_n <<= 1));}TE IN CE Mod& Mod::Halve()NE{RE Ref(((m_n & 1)== 0?m_n:m_n += M)>>= 1);}TE IN Mod& Mod::Invert(){assert(m_n > 0);uint m_n_neg;RE m_n < COantsForMod::g_memory_LE?Ref(m_n = Inverse(m_n).m_n):(m_n_neg = M - m_n < COantsForMod::g_memory_LE)?Ref(m_n = M - Inverse(m_n_neg).m_n):PositivePW(uint(COantsForMod::g_M_minus_2));}TE <> IN Mod<2>& Mod<2>::Invert(){assert(m_n > 0);RE *TH;}TE TE IN CE Mod& Mod::PositivePW(T&& EX)NE{Mod PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <> TE IN CE Mod<2>& Mod<2>::PositivePW(T&& EX)NE{RE *TH;}TE TE IN CE Mod& Mod::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(m_n = 1):Ref(PositivePW(forward(EX)));}TE TE IN CE Mod& Mod::PW(T&& EX){bool neg = EX < 0;assert(!(neg && m_n == 0));neg?EX *= COantsForMod::g_M_minus_2_neg:EX;RE m_n == 0?*TH:(EX %= COantsForMod::g_M_minus)== 0?Ref(m_n = 1):PositivePW(forward(EX));}TE IN CO Mod& Mod::Inverse(CO uint& n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr].m_n = M - MN::MU(memory[M % LE_curr].m_n,M / LE_curr);LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::Factorial(CO uint& n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::Factorial(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::FactorialInverse(CO uint& n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::FactorialInverse(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN CE VO Mod::swap(Mod& n)NE{std::swap(m_n,n.m_n);}TE IN CE CO uint& Mod::RP()CO NE{RE m_n;}TE IN CE Mod Mod::DeRP(CO uint& n)NE{Mod n_copy{};n_copy.m_n = n;RE n_copy;}TE IN CE uint& Mod::Normalise(uint& n)NE{RE n < M?n:n -= M;}TE IN CO Mod& Mod::zero()NE{ST CE CO Mod z{};RE z;}TE IN CO Mod& Mod::one()NE{ST CE CO Mod o{DeRP(1)};RE o;}TE TE IN CE Mod& Mod::Ref(T&& n)NE{RE *TH;}TE IN CE Mod Twice(CO Mod& n)NE{RE MO(Mod(n).Double());}TE IN CE Mod Half(CO Mod& n)NE{RE MO(Mod(n).Halve());}TE IN Mod Inverse(CO Mod& n){RE MO(Mod(n).Invert());}TE IN CE Mod Inverse_COrexpr(CO uint& n)NE{RE MO(Mod::DeRP(RS(n)).NonNegativePW(M - 2));}TE IN CE Mod PW(CO Mod& n,CO T& EX){RE MO(Mod(n).PW(T(EX)));}TE IN CE VO swap(Mod& n0,Mod& n1)NE{n0.swap(n1);}TE IN string to_string(CO Mod& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_ostream& OP<<(basic_ostream& os,CO Mod& n){RE os << n.RP();} int main() { UNTIE; LIBRARY_SEARCH; START_MAIN; // DEXPR( int , bound_T , 100000 , 100 ); // CIN_ASSERT( T , 1 , bound_T ); // REPEAT( T ){ // } CIN_ASSERT( N , 1 , bound_N ); CIN_ASSERT( M , 1 , bound_M ); CIN_ASSERT( K , 1 , bound_K ); int S[bound_N + 1]; FOREQ( i , 1 , N ){ CIN_ASSERT( Si, 1 , M ); S[i] = Si; } // CIN_ASSERT( H , 1 , bound_H ); // CIN_ASSERT( W , 1 , bound_W ); // TYPE_OF( bound_HW ) HW = TYPE_OF( bound_HW )( H ) * W; // assert( HW <= bound_HW ); int A[bound_M + 1]; FOREQ( i , 1 , M ){ CIN_ASSERT( Ai , 1 , K ); A[i] = Ai; } int B[bound_M + 1]; FOREQ( i , 1 , M ){ CIN_ASSERT( Bi , 1 , K ); B[i] = Bi; } BIT bit[bound_K+1] = {}; bit[0].Set( 0 , 1 ); int last[bound_K + 1] = {}; FOREQ( i , 1 , N ){ int& Si = S[i]; int& Ai = A[Si]; int& Bi = B[Si]; int& last_i = last[Si]; FOREQ( k , 1 , K ){ bit[k].Set( i , ( k >= Ai ? bit[k - Ai].IntervalSum( last_i , i - 1 ) : 0 ) + ( k >= Bi ? bit[k - Bi].IntervalSum( last_i , i - 1 ) : 0 ) ); } last_i = i; } // FOR( i , 0 , M ){ // CIN( ui , 1 , N ); // CIN( vi , 1 , N ); // ui--; // vi--; // e[ui].push_back( vi ); // e[vi].push_back( ui ); // } // DEXPR( int , bound_Q , 100000 , 100 ); // CIN_ASSERT( Q , 1 , bound_Q ); // REPEAT( Q ){ // COUT( N ); // } // ll guchoku = Guchoku(); ll answer = bit[K].IntervalSum( 1 , N ).RP(); // if( answer == guchoku ){ // CERR( answer << " == " << guchoku ); // } else { // CERR( answer << " != " << guchoku ); // QUIT; // } COUT( ( answer ) ); FINISH_MAIN; QUIT; }