結果

問題 No.2604 Initial Motion
ユーザー 👑 p-adicp-adic
提出日時 2024-02-10 22:45:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,733 ms / 3,000 ms
コード長 57,509 bytes
コンパイル時間 5,085 ms
コンパイル使用メモリ 311,384 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-28 17:21:40
合計ジャッジ時間 39,119 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 60 ms
6,816 KB
testcase_04 AC 60 ms
6,816 KB
testcase_05 AC 60 ms
6,820 KB
testcase_06 AC 62 ms
6,820 KB
testcase_07 AC 61 ms
6,820 KB
testcase_08 AC 59 ms
6,820 KB
testcase_09 AC 55 ms
6,820 KB
testcase_10 AC 59 ms
6,816 KB
testcase_11 AC 59 ms
6,816 KB
testcase_12 AC 56 ms
6,816 KB
testcase_13 AC 1,465 ms
6,820 KB
testcase_14 AC 970 ms
6,816 KB
testcase_15 AC 653 ms
6,820 KB
testcase_16 AC 1,356 ms
6,820 KB
testcase_17 AC 1,733 ms
6,816 KB
testcase_18 AC 1,635 ms
6,820 KB
testcase_19 AC 1,559 ms
6,816 KB
testcase_20 AC 1,256 ms
6,816 KB
testcase_21 AC 1,017 ms
6,816 KB
testcase_22 AC 1,556 ms
6,820 KB
testcase_23 AC 1,074 ms
6,820 KB
testcase_24 AC 1,424 ms
6,816 KB
testcase_25 AC 1,625 ms
6,820 KB
testcase_26 AC 1,264 ms
6,816 KB
testcase_27 AC 794 ms
6,816 KB
testcase_28 AC 1,104 ms
6,816 KB
testcase_29 AC 1,434 ms
6,816 KB
testcase_30 AC 908 ms
6,820 KB
testcase_31 AC 1,210 ms
6,816 KB
testcase_32 AC 1,105 ms
6,816 KB
testcase_33 AC 5 ms
6,816 KB
testcase_34 AC 1,396 ms
6,820 KB
testcase_35 AC 1,515 ms
6,816 KB
testcase_36 AC 1,513 ms
6,820 KB
testcase_37 AC 5 ms
6,816 KB
testcase_38 AC 3 ms
6,816 KB
testcase_39 AC 3 ms
6,816 KB
testcase_40 AC 1,415 ms
6,816 KB
testcase_41 AC 1,427 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef INCLUDE_MODE
  #define INCLUDE_MODE
  // #define REACTIVE
  // #define USE_GETLINE
#endif

#ifdef INCLUDE_MAIN

inline void Solve()
{
  CIN( int , K , N , M );
    CIN_A( int , A , K );
    CIN_A( int , B , N );
    Map<int,int> A_hind{};
    FOR( k , 0 , K ){
      A_hind[A[k]]++;
    }
    using path_type = tuple<int,ll,ll>;
    gE<path_type>.resize( N + 2 );
    FOR_ITR( A_hind ){
      gE<path_type>[0].push_back( { itr->first , 0 , itr->second } );
    }
    FOREQ( i , 1 , N ){
      gE<path_type>[i].push_back( { N + 1 , 0 , B[i-1] } );
    }
    FOR( j , 0 , M ){
      CIN( ll , uj , vj , wj );
      gE<path_type>[uj].push_back( { vj , wj , K } );
      gE<path_type>[vj].push_back( { uj , wj , K } );
    }
    Graph graph{ N + 2 , Get( gE<path_type> ) };
    // MinimumCostFlow mcf{ graph , 1LL , 1LL<<62 };
    AbstractMinimumCostFlow mcf{ graph , Ring( 1LL ) , 1LL<<62 };
    auto [answer,flow] = mcf.GetFlow( 0 , N + 1 , K , false );
    RETURN( answer );
}
REPEAT_MAIN(1);

#else // INCLUDE_MAIN

#ifdef INCLUDE_SUB

// グラフ用
TE <TY T> Map<T,T> gF;
TE <TY T> VE<T> gA;
TE <TY PATH> VE<LI<PATH>> gE;
TE <TY T , TE <TY...> TY V> IN auto Get( CO V<T>& a ) { return [&]( CRI i = 0 ){ RE a[i]; }; }

// COMPAREに使用。圧縮時は削除する。
ll Naive( int N , int M , int K )
{
  ll answer = N + M + K;
  return answer;
}

// COMPAREに使用。圧縮時は削除する。
ll Answer( ll N , ll M , ll K )
{
  // START_WATCH;
  ll answer = N + M + K;

  // // TLに準じる乱択や全探索。デフォルトの猶予は100.0[ms]。
  // CEXPR( double , TL , 2000.0 );
  // while( CHECK_WATCH( TL ) ){

  // }
  return answer;
}

// 圧縮時は中身だけ削除する。
inline void Experiment()
{
  // CEXPR( int , bound , 10 );
  // FOREQ( N , 0 , bound ){
  //   FOREQ( M , 0 , bound ){
  //     FOREQ( K , 0 , bound ){
  //   	COUT( N , M , K , ":" , Naive( N , M , K ) );
  //     }
  //   }
  //   // cout << Naive( N ) << ",\n"[N==bound];
  // }
}

// 圧縮時は中身だけ削除する。
inline void SmallTest()
{
  // CEXPR( int , bound , 10 );
  // FOREQ( N , 0 , bound ){
  //   FOREQ( M , 0 , bound ){
  //     FOREQ( K , 0 , bound ){
  //   	COMPARE( N , M , K );
  //     }
  //   }
  //   // COMPARE( N );
  // }
}

#define INCLUDE_MAIN
#include __FILE__

#else // INCLUDE_SUB

#ifdef INCLUDE_LIBRARY

/*

C-x 3 C-x o C-x C-fによるファイル操作用

BFS:
c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/BreadthFirstSearch/compress.txt

CoordinateCompress:
c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/CoordinateCompress/compress.txt

DFSOnTree
c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/DepthFirstSearch/Tree/a.hpp

Divisor:
c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Prime/Divisor/compress.txt

Polynomial
c:/Users/user/Documents/Programming/Mathematics/Polynomial/compress.txt

UnionFind
c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/UnionFindForest/compress.txt

*/

// VVV 常設でないライブラリは以下に挿入する。

TE <TY U,TY MONOID,TY SEMIGROUP>CL VirtualSemirng{PU:VI U Sum(CO U& u0,CO U& u1)= 0;VI CO U& Zero()CO NE = 0;VI U Product(CO U& u0,CO U& u1)= 0;VI MONOID& AdditiveMonoid()NE = 0;VI SEMIGROUP& MultiplicativeSemigroup()NE = 0;US type = U;};TE <TY U,TY MONOID,TY SEMIGROUP>CL AbstractSemirng:VI PU VirtualSemirng<U,MONOID,SEMIGROUP>{PU:MONOID m_R0;SEMIGROUP m_R1;IN AbstractSemirng(MONOID R0,SEMIGROUP R1);IN U Sum(CO U& u0,CO U& u1);IN CO U& Zero()CO NE;IN U Product(CO U& u0,CO U& u1);IN MONOID& AdditiveMonoid()NE;IN SEMIGROUP& MultiplicativeSemigroup()NE;};TE <TY U>CL Semirng:PU AbstractSemirng<U,AdditiveMonoid<U>,MultiplicativeMagma<U>>{PU:IN Semirng();};
TE <TY U,TY MONOID,TY SEMIGROUP> IN AbstractSemirng<U,MONOID,SEMIGROUP>::AbstractSemirng(MONOID R0,SEMIGROUP R1):m_R0(MO(R0)),m_R1(MO(R1)){}TE <TY U> IN Semirng<U>::Semirng():AbstractSemirng<U,AdditiveMonoid<U>,MultiplicativeMagma<U>>(AdditiveMonoid<U>(),MultiplicativeMagma()){}TE <TY U,TY MONOID,TY SEMIGROUP> IN U AbstractSemirng<U,MONOID,SEMIGROUP>::Sum(CO U& u0,CO U& u1){RE m_R0.Sum(u0,u1);}TE <TY U,TY MONOID,TY SEMIGROUP> IN CO U& AbstractSemirng<U,MONOID,SEMIGROUP>::Zero()CO NE{RE m_R0.Zero();}TE <TY U,TY MONOID,TY SEMIGROUP> IN U AbstractSemirng<U,MONOID,SEMIGROUP>::Product(CO U& u0,CO U& u1){RE m_R1.Product(u0,u1);}TE <TY U,TY MONOID,TY SEMIGROUP> IN MONOID& AbstractSemirng<U,MONOID,SEMIGROUP>::AdditiveMonoid()NE{RE m_R0;}TE <TY U,TY MONOID,TY SEMIGROUP> IN SEMIGROUP& AbstractSemirng<U,MONOID,SEMIGROUP>::MultiplicativeSemigroup()NE{RE m_R1;}

TE <TY U,TY GROUP,TY MONOID>CL VirtualRing:VI PU VirtualSemirng<U,GROUP,MONOID>{PU:VI U Inverse(CO U& u)= 0;VI CO U& One()CO NE = 0;IN GROUP& AdditiveGroup()NE;IN MONOID& MultiplicativeMonoid()NE;};TE <TY U,TY GROUP,TY MONOID>CL AbstractRing:VI PU VirtualRing<U,GROUP,MONOID>,PU AbstractSemirng<U,GROUP,MONOID>{PU:IN AbstractRing(GROUP R0,MONOID R1);IN U Inverse(CO U& u);IN CO U& One()CO NE;};TE <TY U = ll>CL Ring:VI PU AbstractRing<U,AdditiveGroup<U>,MultiplicativeMonoid<U>>{PU:IN Ring(CO U& one_U);};
TE <TY U,TY GROUP,TY MONOID> IN AbstractRing<U,GROUP,MONOID>::AbstractRing(GROUP R0,MONOID R1):AbstractSemirng<U,GROUP,MONOID>(MO(R0),MO(R1)){}TE <TY U> IN Ring<U>::Ring(CO U& one_U):AbstractRing<U,AdditiveGroup<U>,MultiplicativeMonoid<U>>(AdditiveGroup<U>(),MultiplicativeMonoid<U>(one_U)){}TE <TY U,TY GROUP,TY MONOID> IN U AbstractRing<U,GROUP,MONOID>::Inverse(CO U& u){RE TH->m_R0.Inverse(u);}TE <TY U,TY GROUP,TY MONOID> IN CO U& AbstractRing<U,GROUP,MONOID>::One()CO NE{RE TH->m_R1.One();}TE <TY U,TY GROUP,TY MONOID> IN GROUP& VirtualRing<U,GROUP,MONOID>::AdditiveGroup()NE{RE TH->AdditiveMonoid();}TE <TY U,TY GROUP,TY MONOID> IN MONOID& VirtualRing<U,GROUP,MONOID>::MultiplicativeMonoid()NE{RE TH->MultiplicativeSemigroup();}

#define BELLMAN_FORD_BODY(INITIALISE_PREV,SET_PREV)CO U& zero = m_M.Zero();CO U& infty = TH->Infty();AS(zero < infty);CRI SZ = m_G.SZ();auto&& i_start = m_G.Enumeration_inv(t_start);AS(0 <= i_start && i_start < SZ);VE<bool> found(SZ);VE<U> weight(SZ,infty);found[i_start]= true;weight[i_start]= 0;INITIALISE_PREV;for(int LE = 0;LE < SZ;LE++){for(int i = 0;i < SZ;i++){if(found[i]){CO U& weight_i = weight[i];AS(weight_i != infty);auto&& edge_i = m_G.Edge(m_G.Enumeration(i));for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);CO U& edge_ij = IT->second;U temp = m_M.Sum(weight_i,edge_ij);U& weight_j = weight[j];if(weight_j > temp){found[j]= true;weight_j = MO(temp);SET_PREV;}}}}}bool valid = true;for(int i = 0;i < SZ && valid;i++){if(found[i]){CO U& weight_i = weight[i];auto&& edge_i = m_G.Edge(m_G.Enumeration(i));for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);CO U& edge_ij = IT->second;U& weight_j = weight[j];CO U temp = m_M.Sum(weight_i,edge_ij);if(weight_j > temp){valid = false;break;}}}}
TE <TY GRAPH,TY MONOID,TY U>CL AbstractBellmanFord:PU PointedSet<U>{PU:GRAPH& m_G;MONOID m_M;IN AbstractBellmanFord(GRAPH& G,MONOID M,CO U& infty);tuple<bool,VE<U>> GetDistance(CO inner_t<GRAPH>& t_start,CO bool& dummy = true);TE <TE <TY...> TY V> tuple<bool,VE<U>,VE<LI<inner_t<GRAPH>>>> GetPath(CO inner_t<GRAPH>& t_start,CO V<inner_t<GRAPH>>& t_finals,CO bool& dummy = true);tuple<bool,VE<U>,VE<LI<inner_t<GRAPH>>>> GetPath(CO inner_t<GRAPH>& t_start,CO bool& dummy);};TE <TY GRAPH>CL BellmanFord:PU AbstractBellmanFord<GRAPH,AdditiveMonoid<>,ll>{PU:IN BellmanFord(GRAPH& G);};
TE <TY GRAPH,TY MONOID,TY U> IN AbstractBellmanFord<GRAPH,MONOID,U>::AbstractBellmanFord(GRAPH& G,MONOID M,CO U& infty):PointedSet<U>(infty),m_G(G),m_M(MO(M)){ST_AS(! is_same_v<U,int>);}TE <TY GRAPH> IN BellmanFord<GRAPH>::BellmanFord(GRAPH& G):AbstractBellmanFord<GRAPH,AdditiveMonoid<>,ll>(G,AdditiveMonoid<>(),4611686018427387904){}TE <TY GRAPH,TY MONOID,TY U>tuple<bool,VE<U>> AbstractBellmanFord<GRAPH,MONOID,U>::GetDistance(CO inner_t<GRAPH>& t_start,CO bool& dummy){BELLMAN_FORD_BODY(,);m_G.Reset();RE{valid,MO(weight)};}TE <TY GRAPH,TY MONOID,TY U> TE <TE <TY...> TY V>tuple<bool,VE<U>,VE<LI<inner_t<GRAPH>>>> AbstractBellmanFord<GRAPH,MONOID,U>::GetPath(CO inner_t<GRAPH>& t_start,CO V<inner_t<GRAPH>>& t_finals,CO bool& dummy){BELLMAN_FORD_BODY(VE<int> prev(SZ),prev[j]= i);VE<LI<inner_t<GRAPH>>> path{};if(valid){CO int path_SZ = t_finals.SZ();path.reserve(path_SZ);for(auto IT = t_finals.begin(),EN = t_finals.EN();IT != EN;IT++){LI<inner_t<GRAPH>> path_j{};CO inner_t<GRAPH>& t_final = *IT;path_j.push_back(t_final);int i = m_G.Enumeration_inv(t_final);if(found[i]){WH(i != i_start){i = prev[i];path_j.push_front(m_G.Enumeration(i));}}path.push_back(path_j);}}m_G.Reset();RE{valid,MO(weight),MO(path)};}TE <TY GRAPH,TY MONOID,TY U>tuple<bool,VE<U>,VE<LI<inner_t<GRAPH>>>> AbstractBellmanFord<GRAPH,MONOID,U>::GetPath(CO inner_t<GRAPH>& t_start,CO bool& dummy){CRI SZ = m_G.SZ();VE<inner_t<GRAPH>> t_finals(SZ);for(int i = 0;i < SZ;i++){t_finals[i]= i;}RE GetPath(t_start,t_finals);}

#define DIJKSTRA_PREP(INITIALISE_PREV)CO U& zero = m_M.Zero();CO U& infty = TH->Infty();AS(zero < infty);CRI SZ = m_G.SZ();auto&& i_start = m_G.Enumeration_inv(t_start);AS(0 <= i_start && i_start < SZ);VE<bool> found(SZ);VE<U> weight(SZ,infty);INITIALISE_PREV;
#define DIJKSTRA_BODY_1(SET_PREV)weight[i_start]= zero;int i = i_start;for(int num = 0;num < SZ;num++){CO U& weight_i = weight[i];found[i]= true;auto&& edge_i = m_G.Edge(m_G.Enumeration(i));for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);if(!found[j]){CO U& edge_ij = get<1>(*IT);U temp = m_M.Sum(weight_i,edge_ij);AS(temp < infty);U& weight_j = weight[j];if(temp < weight_j){SET_PREV;weight_j = MO(temp);}}}U temp = infty;for(int j = 0;j < SZ;j++){if(!found[j]){U& weight_j = weight[j];if(weight_j < temp){temp = weight_j;i = j;}}}}
#define DIJKSTRA_BODY_2(CHECK_FINAL,SET_PREV)set<pair<U,int>> vertex{};vertex.insert(pair<U,int>(weight[i_start]= zero,i_start));WH(! vertex.empty()){auto BE = vertex.BE();auto[weight_i,i]= *BE;CHECK_FINAL;found[i]= true;vertex.erase(BE);auto&& edge_i = m_G.Edge(m_G.Enumeration(i));LI<pair<U,int>> changed_vertex{};for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);if(!found[j]){CO U& edge_ij = get<1>(*IT);U temp = m_M.Sum(weight_i,edge_ij);AS(temp < infty);U& weight_j = weight[j];if(temp < weight_j){if(weight_j != infty){vertex.erase(pair<U,int>(weight_j,j));}SET_PREV;changed_vertex.push_back(pair<U,int>(weight_j = MO(temp),j));}}}for(auto IT_changed = changed_vertex.BE(),EN_changed = changed_vertex.EN();IT_changed != EN_changed;IT_changed++){vertex.insert(*IT_changed);}}
#define DIJKSTRA_BODY(INITIALISE_PREV,CHECK_FINAL,SET_PREV)DIJKSTRA_PREP(INITIALISE_PREV);if(many_edges){DIJKSTRA_BODY_1(SET_PREV);}else{DIJKSTRA_BODY_2(CHECK_FINAL,SET_PREV);}
TE <TY GRAPH,TY MONOID,TY U>CL AbstractDijkstra:PU PointedSet<U>{PU:GRAPH& m_G;MONOID m_M;IN AbstractDijkstra(GRAPH& G,MONOID M,CO U& infty);U GetDistance(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,CO bool& many_edges = true);VE<U> GetDistance(CO inner_t<GRAPH>& t_start,CO bool& many_edges = true);pair<U,LI<inner_t<GRAPH>>> GetPath(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,CO bool& many_edges = true);TE <TE <TY...> TY V> pair<VE<U>,VE<LI<inner_t<GRAPH>>>> GetPath(CO inner_t<GRAPH>& t_start,CO V<inner_t<GRAPH>>& t_finals,CO bool& many_edges = true);pair<VE<U>,VE<LI<inner_t<GRAPH>>>> GetPath(CO inner_t<GRAPH>& t_start);};
TE <TY GRAPH>CL Dijkstra:PU AbstractDijkstra<GRAPH,AdditiveMonoid<>,ll>{PU:IN Dijkstra(GRAPH& G);};TE <TY GRAPH,TY MONOID,TY U> IN AbstractDijkstra<GRAPH,MONOID,U>::AbstractDijkstra(GRAPH& G,MONOID M,CO U& infty):PointedSet<U>(infty),m_G(G),m_M(MO(M)){ST_AS(! is_same_v<U,int>);}TE <TY GRAPH> IN Dijkstra<GRAPH>::Dijkstra(GRAPH& G):AbstractDijkstra<GRAPH,AdditiveMonoid<>,ll>(G,AdditiveMonoid<>(),4611686018427387904){}TE <TY GRAPH,TY MONOID,TY U>U AbstractDijkstra<GRAPH,MONOID,U>::GetDistance(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,CO bool& many_edges){auto&& i_final = m_G.Enumeration_inv(t_final);DIJKSTRA_BODY(,if(i == i_final){break;},);U AN{MO(weight[i_final])};m_G.Reset();RE AN;}TE <TY GRAPH,TY MONOID,TY U>VE<U> AbstractDijkstra<GRAPH,MONOID,U>::GetDistance(CO inner_t<GRAPH>& t_start,CO bool& many_edges){DIJKSTRA_BODY(,,);m_G.Reset();RE weight;}TE <TY GRAPH,TY MONOID,TY U>pair<U,LI<inner_t<GRAPH>>> AbstractDijkstra<GRAPH,MONOID,U>::GetPath(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,CO bool& many_edges){auto&& i_final = m_G.Enumeration_inv(t_final);DIJKSTRA_BODY(VE<int> prev(SZ),if(i == i_final){break;},prev[j]= i);int i = i_final;LI<inner_t<GRAPH>> path{};path.push_back(t_final);if(found[i]){WH(i != i_start){i = prev[i];path.push_front(m_G.Enumeration(i));}}U AN{MO(weight[i_final])};m_G.Reset();RE{MO(AN),MO(path)};}TE <TY GRAPH,TY MONOID,TY U> TE <TE <TY...> TY V>pair<VE<U>,VE<LI<inner_t<GRAPH>>>> AbstractDijkstra<GRAPH,MONOID,U>::GetPath(CO inner_t<GRAPH>& t_start,CO V<inner_t<GRAPH>>& t_finals,CO bool& many_edges){DIJKSTRA_BODY(VE<int> prev(SZ),,prev[j]= i);CO int path_SZ = t_finals.SZ();VE<LI<inner_t<GRAPH>>> path;path.reserve(path_SZ);for(auto IT = t_finals.BE(),EN = t_finals.EN();IT != EN;IT++){LI<inner_t<GRAPH>> path_j{};CO inner_t<GRAPH>& t_final = *IT;path_j.push_back(t_final);int i = m_G.Enumeration_inv(t_final);if(found[i]){WH(i != i_start){i = prev[i];path_j.push_front(m_G.Enumeration(i));}}path.push_back(path_j);}m_G.Reset();RE{MO(weight),MO(path)};}TE <TY GRAPH,TY MONOID,TY U>pair<VE<U>,VE<LI<inner_t<GRAPH>>>> AbstractDijkstra<GRAPH,MONOID,U>::GetPath(CO inner_t<GRAPH>& t_start){CRI SZ = m_G.SZ();VE<inner_t<GRAPH>> t_finals(SZ);for(int i = 0;i < SZ;i++){t_finals[i]= i;}RE GetPath(t_start,t_finals);}

#define POTENTIALISED_DIJKSTRA_BODY(GET,WEIGHT,...)CO U& infty = TH->Infty();if(m_valid){CO U& zero = m_M.Zero();auto edge =[&](CO T& t){CO U& potential_i = m_potential[m_G.Enumeration_inv(t)];AS(potential_i < infty);auto edge_i = m_G.Edge(t);LI<pair<T,U>> AN{};for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){auto& e = *IT;if(m_on(e)){CO auto& v_j = get<0>(e);U& w_j = get<1>(e);CO U& potential_j = m_potential[m_G.Enumeration_inv(v_j)];AS(w_j < infty && potential_j < infty);CO U potential_j_inv = m_M.Inverse(potential_j);w_j = m_M.Sum(m_M.Sum(w_j,potential_i),potential_j_inv);AS(!(w_j < zero)&& w_j < infty);AN.push_back({v_j,MO(w_j)});}}RE AN;};auto G = m_G.GetGraph(MO(edge));AbstractDijkstra d{G,m_M,infty};auto value = d.GET;CRI SZ = m_G.SZ();for(int i = 0;i < SZ;i++){auto& weight_i = WEIGHT[i];if(weight_i != infty){weight_i = m_M.Sum(weight_i,m_potential[i]);}}RE{m_valid,__VA_ARGS__};}auto edge =[&](CO T& t){auto&& edge_i = m_G.Edge(t);LI<pair<T,U>> AN{};for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){if(m_on(*IT)){AN.push_back({get<0>(*IT),get<1>(*IT)});}}RE AN;};auto G = m_G.GetGraph(MO(edge));AbstractBellmanFord d{G,m_M,infty};RE d.GET;
TE <TY T,TY GRAPH,TY GROUP,TY U,TY On>CL AbstractPotentialisedDijkstra:PU PointedSet<U>{PU:GRAPH& m_G;GROUP m_M;T m_t_start;On m_on;bool m_valid;VE<U> m_potential;IN AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& negative = true);IN AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& valid,VE<U> potential);IN CO bool& Valid()CO NE;IN CO VE<U>& Potential()CO NE;IN VO SetPotential(CO bool& valid,VE<U> potential);tuple<bool,VE<U>> GetDistance(CO bool& many_edges = true);TE <TY...Args> tuple<bool,VE<U>,VE<LI<T>>> GetPath(CO Args&... args);};TE <TY T,TY GRAPH,TY On>CL PotentialisedDijkstra:PU AbstractPotentialisedDijkstra<T,GRAPH,AdditiveGroup<>,ll,On>{PU:TE <TY...Args> IN PotentialisedDijkstra(GRAPH& G,CO T& t_start,On on,Args&&... args);};
TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> IN AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& negative):AbstractPotentialisedDijkstra(G,MO(M),t_start,infty,MO(on),true,VE<U>()){if(negative){auto edge =[&](CRI t){auto&& edge_i = m_G.Edge(t);LI<pair<T,U>> AN{};for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){CO auto& e = *IT;AN.push_back({get<0>(e),get<1>(e)});}RE AN;};auto G_full = m_G.GetGraph(MO(edge));AbstractBellmanFord bf{G_full,m_M,infty};auto[valid,potential]= bf.GetDistance(m_t_start);m_valid = valid;m_potential = MO(potential);}else{m_potential = VE<U>(m_G.SZ(),m_M.Zero());}}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> IN AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& valid,VE<U> potential):PointedSet<U>(infty),m_G(G),m_M(MO(M)),m_t_start(t_start),m_on(MO(on)),m_valid(valid),m_potential(potential){ST_AS(is_invocable_r_v<bool,On,decltype(declval<GRAPH>().Edge(declval<T>()).back())>);}TE <TY T,TY GRAPH,TY On> TE <TY...Args> IN PotentialisedDijkstra<T,GRAPH,On>::PotentialisedDijkstra(GRAPH& G,CO T& t_start,On on,Args&&... args):AbstractPotentialisedDijkstra<T,GRAPH,AdditiveGroup<>,ll,On>(G,AdditiveGroup<>(),t_start,4611686018427387904,MO(on),forward<decay_t<Args>>(args)...){}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> IN CO bool& AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::Valid()CO NE{RE m_valid;}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> IN CO VE<U>& AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::Potential()CO NE{RE m_potential;}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> IN VO AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::SetPotential(CO bool& valid,VE<U> potential){AS(int(potential.SZ())== m_G.SZ());m_valid = valid;m_potential = MO(potential);}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> tuple<bool,VE<U>> AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::GetDistance(CO bool& many_edges){POTENTIALISED_DIJKSTRA_BODY(GetDistance(m_t_start,many_edges),value,MO(value));}TE <TY T,TY GRAPH,TY GROUP,TY U,TY On> TE <TY...Args> tuple<bool,VE<U>,VE<LI<T>>> AbstractPotentialisedDijkstra<T,GRAPH,GROUP,U,On>::GetPath(CO Args&... args){POTENTIALISED_DIJKSTRA_BODY(GetPath(m_t_start,args...),get<0>(value),MO(get<0>(value)),MO(get<1>(value)));}

TE <TY GRAPH,TY RING,TY U>CL AbstractMinimumCostFlow:PU PointedSet<U>{PU:GRAPH& m_G;RING m_R;IN AbstractMinimumCostFlow(GRAPH& G,RING R,CO U& infty);pair<U,VE<VE<tuple<inner_t<GRAPH>,U>>>> GetFlow(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,U f,CO bool& many_edges = true);};TE <TY GRAPH,TY U>CL MinimumCostFlow:PU AbstractMinimumCostFlow<GRAPH,Ring<U>,U>{PU:IN MinimumCostFlow(GRAPH& G,CO U& one_U,CO U& infty);};
TE <TY GRAPH,TY RING,TY U> IN AbstractMinimumCostFlow<GRAPH,RING,U>::AbstractMinimumCostFlow(GRAPH& G,RING R,CO U& infty):PointedSet<U>(infty),m_G(G),m_R(MO(R)){}TE <TY GRAPH,TY U> IN MinimumCostFlow<GRAPH,U>::MinimumCostFlow(GRAPH& G,CO U& one_U,CO U& infty):AbstractMinimumCostFlow<GRAPH,Ring<U>,U>(G,Ring<U>(one_U),infty){}TE <TY GRAPH,TY RING,TY U>pair<U,VE<VE<tuple<inner_t<GRAPH>,U>>>> AbstractMinimumCostFlow<GRAPH,RING,U>::GetFlow(CO inner_t<GRAPH>& t_start,CO inner_t<GRAPH>& t_final,U f,CO bool& many_edges){US T = inner_t<GRAPH>;CO U& zero = m_R.Zero();CO U& infty = TH->Infty();CRI SZ = m_G.SZ();VE<VE<tuple<int,U,U,bool,int>>> rest(SZ);VE<VE<tuple<T,U>>> flow(SZ);int edge_num = 0;for(int i = 0;i < SZ;i++){auto&& ui = m_G.Enumeration(i);auto&& edge_i = m_G.Edge(ui);for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){CO auto&[vj,wj,fj]= *IT;AS(ui != vj && !(wj < zero)&& wj < infty && !(fj < zero)&& fj < infty);auto&& j = m_G.Enumeration_inv(vj);rest[i].push_back({j,wj,fj,false,edge_num});rest[j].push_back({i,m_R.Inverse(wj),zero,true,edge_num});flow[i].push_back({vj,0});edge_num++;}}for(int i = 0;i < SZ;i++){auto& rest_i = rest[i];sort(rest_i.begin(),rest_i.EN());}VE<tuple<int,int,int,int>> edge_pair(edge_num,{-1,-1,-1,-1});for(int i = 0;i < SZ;i++){CO auto& rest_i = rest[i];CO int SZ_i = rest_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto& rest_ij = rest_i[j];auto&[i_0,j_0,i_1,j_1]= edge_pair[get<4>(rest_ij)];if(i_0 == -1){i_0 = i;j_0 = j;}else{i_1 = i;j_1 = j;}}}auto edge =[&](CO T& t)-> CO VE<tuple<int,U,U,bool,int>>&{RE rest[m_G.Enumeration_inv(t)];};auto on =[&](CO tuple<T,U,U,bool,int>& e){RE zero < get<2>(e);};auto G = m_G.GetGraph(MO(edge));AbstractPotentialisedDijkstra pd{G,m_R.AdditiveGroup(),t_start,infty,MO(on),false};auto&& i_start = m_G.Enumeration_inv(t_start);LI<T> t_finals ={t_final};U w = zero;WH(zero < f){auto[valid,weight,paths]= pd.GetPath(t_finals,many_edges);AS(valid);pd.SetPotential(valid,MO(weight));auto& path = paths.front();auto IT_path = path.begin(),IT_path_prev = IT_path,EN_path = path.EN();AS(IT_path != EN_path);int i = i_start;LI<tuple<int,int,int,int>> flow_num{};U f_min = f;WH(++IT_path != EN_path){T t = *IT_path;flow_num.push_back({i,m_G.Enumeration_inv(t),-1,-1});auto&[i_curr,i_next,j_1,j_2]= flow_num.back();CO auto& rest_i = rest[i_curr];int SZ_i = rest_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto&[vj,wj,fj,rj,numj]= rest_i[j];if(zero < fj && vj == t){j_1 = j;fj < f_min?f_min = fj:f_min;if(rj){i_curr = i_next;t = *IT_path_prev;}break;}}AS(j_1 != -1);auto& flow_i = flow[i_curr];SZ_i = flow_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto&[vj,fj]= flow_i[j];if(vj == t){j_2 = j;break;}}AS(j_2 != -1);i_curr = i;i = i_next;IT_path_prev = IT_path;}CO U f_min_minus = m_R.Inverse(f_min);U w_diff = zero;for(auto IT = flow_num.begin(),EN = flow_num.EN();IT != EN;IT++){CO auto&[i_curr,i_next,j_1,j_2]= *IT;auto&[vj,wj,fj,rj,numj]= rest[i_curr][j_1];CO auto& edge_pair_i = edge_pair[numj];CRI j_3 = get<0>(edge_pair_i)== i_curr?get<3>(edge_pair_i):get<1>(edge_pair_i);auto& fj_inv = get<2>(rest[i_next][j_3]);auto& f_curr = get<1>(flow[rj?i_next:i_curr][j_2]);w_diff = m_R.Sum(w_diff,wj);fj = m_R.Sum(fj,f_min_minus);fj_inv = m_R.Sum(fj_inv,f_min);f_curr = m_R.Sum(f_curr,f_min);}f = m_R.Sum(f,f_min_minus);w = m_R.Sum(w,m_R.Product(f_min,w_diff));}RE{MO(w),MO(flow)};}

// AAA 常設でないライブラリは以上に挿入する。

#define INCLUDE_SUB
#include __FILE__

#else // INCLUDE_LIBRARY

#ifdef DEBUG
  #define _GLIBCXX_DEBUG
  #define REPEAT_MAIN( BOUND ) START_MAIN; signal( SIGABRT , &AlertAbort ); AutoCheck( exec_mode , use_getline ); if( exec_mode == sample_debug_mode || exec_mode == submission_debug_mode || exec_mode == library_search_mode ){ RE 0; } else if( exec_mode == experiment_mode ){ Experiment(); RE 0; } else if( exec_mode == small_test_mode ){ SmallTest(); RE 0; }; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if( exec_mode == solve_mode ){ if CE( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } } else if( exec_mode == random_test_mode ){ CERR( "ランダムテストを行う回数を指定してください。" ); SET_LL( test_case_num ); } FINISH_MAIN
  #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE )
  #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); AS( ( MIN ) <= A && A <= ( MAX ) )
  #define SET_ASSERT( A , MIN , MAX ) if( exec_mode == solve_mode ){ SET_LL( A ); ASSERT( A , MIN , MAX ); } else if( exec_mode == random_test_mode ){ CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ); } else { AS( false ); }
  #define SOLVE_ONLY ST_AS( __FUNCTION__[0] == 'S' )
  #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl
  #define COUT( ... ) VariadicCout( cout << "出力: " , __VA_ARGS__ ) << endl
  #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl
  #define COUT_A( A , N ) cout << "出力: "; OUTPUT_ARRAY( cout , A , N ) << endl
  #define CERR_ITR( A ) OUTPUT_ITR( cerr , A ) << endl
  #define COUT_ITR( A ) cout << "出力: "; OUTPUT_ITR( cout , A ) << endl
#else
  #pragma GCC optimize ( "O3" )
  #pragma GCC optimize ( "unroll-loops" )
  #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
  #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if CE( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } FINISH_MAIN
  #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE )
  #define ASSERT( A , MIN , MAX ) AS( ( MIN ) <= A && A <= ( MAX ) )
  #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX )
  #define SOLVE_ONLY 
  #define CERR( ... ) 
  #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL
  #define CERR_A( A , N ) 
  #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL
  #define CERR_ITR( A ) 
  #define COUT_ITR( A ) OUTPUT_ITR( cout , A ) << ENDL
#endif
#ifdef REACTIVE
  #define ENDL endl
#else
  #define ENDL "\n"
#endif
#ifdef USE_GETLINE
  #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A ## _str ); }
  #define GETLINE_SEPARATE( SEPARATOR , ... ) SOLVE_ONLY; string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ )
  #define GETLINE( ... ) SOLVE_ONLY; GETLINE_SEPARATE( '\n' , __VA_ARGS__ )
#else
  #define SET_LL( A ) cin >> A
  #define CIN( LL , ... ) SOLVE_ONLY; LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ )
  #define SET_A( A , N ) SOLVE_ONLY; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; }
  #define CIN_A( LL , A , N ) VE<LL> A( N ); SET_A( A , N );
#endif
#include <bits/stdc++.h>
using namespace std;
#define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) )
#define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr )
#define FINISH_MAIN REPEAT( test_case_num ){ if CE( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } }
#define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now()
#define CURRENT_TIME static_cast<double>( chrono::duration_cast<chrono::microseconds>( chrono::system_clock::now() - watch ).count() / 1000.0 )
#define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 )
#define CEXPR( LL , BOUND , VALUE ) CE LL BOUND = VALUE
#define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX )
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( decldecay_t( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ )
#define FOREQ( VAR , INITIAL , FINAL ) for( decldecay_t( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ )
#define FOREQINV( VAR , INITIAL , FINAL ) for( decldecay_t( INITIAL ) VAR = INITIAL ; VAR + 1 > FINAL ; VAR -- )
#define AUTO_ITR( ARRAY ) auto itr_ ## ARRAY = ARRAY .BE() , end_ ## ARRAY = ARRAY .EN()
#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 SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS )
#define OUTPUT_ARRAY( OS , A , N ) FOR( VARIABLE_FOR_OUTPUT_ARRAY , 0 , N ){ OS << A[VARIABLE_FOR_OUTPUT_ARRAY] << (VARIABLE_FOR_OUTPUT_ARRAY==N-1?"":" "); } OS
#define OUTPUT_ITR( OS , A ) { auto ITERATOR_FOR_OUTPUT_ITR = A.BE() , EN_FOR_OUTPUT_ITR = A.EN(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; WH( VARIABLE_FOR_OUTPUT_ITR ){ OS << *ITERATOR_FOR_COUT_ITR; ( VARIABLE_FOR_OUTPUT_ITR = ++ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR ) ? OS : OS << " "; } } OS
#define RETURN( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); RE
#define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ ); auto answer = Answer( __VA_ARGS__ ); bool match = naive == answer; COUT( "(" , #__VA_ARGS__ , ") == (" , __VA_ARGS__ , ") : Naive == " , naive , match ? "==" : "!=" , answer , "== Answer" ); if( !match ){ RE; }

// 圧縮用
#define TE template
#define TY typename
#define US using
#define ST static
#define AS assert
#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 LE length
#define PW Power
#define MO move
#define TH this
#define CRI CO int&
#define CRUI CO uint&
#define CRL CO ll&
#define VI virtual 
#define ST_AS static_assert
#define reMO_CO remove_const
#define is_COructible_v is_constructible_v
#define rBE rbegin
#define reSZ resize

// 型のエイリアス
#define decldecay_t( VAR ) decay_t<decltype( VAR )>
TE <TY F , TY...Args> US ret_t = decltype( declval<F>()( declval<Args>()... ) );
TE <TY T> US inner_t = TY T::type;
US uint = unsigned int;
US ll = long long;
US ull = unsigned long long;
US ld = long double;
US lld = __float128;
TE <TY INT> US T2 = pair<INT,INT>;
TE <TY INT> US T3 = tuple<INT,INT,INT>;
TE <TY INT> US T4 = tuple<INT,INT,INT,INT>;
US path = pair<int,ll>;

// 入出力用
TE <CL Traits> IN basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is ) { RE is; }
TE <CL Traits , TY Arg , TY... ARGS> IN basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is , Arg& arg , ARGS&... args ) { RE VariadicCin( is >> arg , args... ); }
TE <CL Traits> IN basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , CO char& separator ) { RE is; }
TE <CL Traits , TY Arg , TY... ARGS> IN basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , CO char& separator , Arg& arg , ARGS&... args ) { RE VariadicGetline( getline( is , arg , separator ) , separator , args... ); }
TE <CL Traits , TY Arg> IN basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , CO VE<Arg>& arg ) { auto BE = arg.BE() , EN = arg.EN(); auto itr = BE; WH( itr != EN ){ ( itr == BE ? os : os << " " ) << *itr; itr++; } RE os; }
TE <CL Traits , TY Arg> IN basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , CO Arg& arg ) { RE os << arg; }
TE <CL Traits , TY Arg1 , TY Arg2 , TY... ARGS> IN basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , CO Arg1& arg1 , CO Arg2& arg2 , CO ARGS&... args ) { RE VariadicCout( os << arg1 << " " , arg2 , args... ); }

// デバッグ用
#ifdef DEBUG
  IN VO AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); }
  VO AutoCheck( int& exec_mode , CO bool& use_getline );
  IN VO Solve();
  IN VO Experiment();
  IN VO SmallTest();
  IN VO RandomTest();
  ll GetRand( CRL Rand_min , CRL Rand_max );
  IN VO BreakPoint( CRI LINE ) {}
  int exec_mode;
  CEXPR( int , solve_mode , 0 );
  CEXPR( int , sample_debug_mode , 1 );
  CEXPR( int , submission_debug_mode , 2 );
  CEXPR( int , library_search_mode , 3 );
  CEXPR( int , experiment_mode , 4 );
  CEXPR( int , small_test_mode , 5 );
  CEXPR( int , random_test_mode , 6 );
  #ifdef USE_GETLINE
    CEXPR( bool , use_getline , true );
  #else
    CEXPR( bool , use_getline , false );
  #endif
#else
  ll GetRand( CRL Rand_min , CRL Rand_max ) { ll answer = time( NULL ); RE answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; }
#endif

// VVV 常設ライブラリは以下に挿入する。
// Map
// c:/Users/user/Documents/Programming/Mathematics/Function/Map
CL is_ordered{PU:is_ordered()= delete;TE <TY T> ST CE auto Check(CO T& t)-> decltype(t < t,true_type());ST CE false_type Check(...);TE <TY T> ST CE CO bool value = is_same_v< decltype(Check(declval<T>())),true_type >;};
TE <TY T , TY U>US Map = conditional_t<is_constructible_v<unordered_map<T,int>>,unordered_map<T,U>,conditional_t<is_ordered::value<T>,map<T,U>,void>>;

// Algebra
// c:/Users/user/Documents/Programming/Mathematics/Algebra/compress.txt
#define DC_OF_CPOINT(POINT)IN CO U& POINT()CO NE
#define DC_OF_POINT(POINT)IN U& POINT() NE
#define DF_OF_CPOINT(POINT)TE <TY U> IN CO U& VirtualPointedSet<U>::POINT()CO NE{RE Point();}
#define DF_OF_POINT(POINT)TE <TY U> IN U& VirtualPointedSet<U>::POINT()NE{RE Point();}
TE <TY U>CL UnderlyingSet{PU:US type = U;};TE <TY U>CL VirtualPointedSet:virtual PU UnderlyingSet<U>{PU:virtual CO U& Point()CO NE = 0;virtual U& Point() NE = 0;DC_OF_CPOINT(Unit);DC_OF_CPOINT(Zero);DC_OF_CPOINT(One);DC_OF_CPOINT(Infty);DC_OF_CPOINT(size);DC_OF_POINT(init);DC_OF_POINT(root);};TE <TY U>CL PointedSet:virtual PU VirtualPointedSet<U>{PU:U m_b_U;IN PointedSet(CO U& b_u = U());IN CO U& Point()CO NE;IN U& Point() NE;};TE <TY U>CL VirtualNSet:virtual PU UnderlyingSet<U>{PU:virtual U Transfer(CO U& u)= 0;IN U Inverse(CO U& u);};TE <TY U,TY F_U>CL AbstractNSet:virtual PU VirtualNSet<U>{PU:F_U& m_f_U;IN AbstractNSet(F_U& f_U);IN U Transfer(CO U& u);};TE <TY U>CL VirtualMagma:virtual PU UnderlyingSet<U>{PU:virtual U Product(CO U& u0,CO U& u1)= 0;IN U Sum(CO U& u0,CO U& u1);};TE <TY U = ll>CL AdditiveMagma:virtual PU VirtualMagma<U>{PU:IN U Product(CO U& u0,CO U& u1);};TE <TY U = ll>CL MultiplicativeMagma:virtual PU VirtualMagma<U>{PU:IN U Product(CO U& u0,CO U& u1);};TE <TY U,TY M_U>CL AbstractMagma:virtual PU VirtualMagma<U>{PU:M_U& m_m_U;IN AbstractMagma(M_U& m_U);IN U Product(CO U& u0,CO U& u1);};
TE <TY U> IN PointedSet<U>::PointedSet(CO U& b_U):m_b_U(b_U){}TE <TY U> IN CO U& PointedSet<U>::Point()CO NE{RE m_b_U;}TE <TY U> IN U& PointedSet<U>::Point()NE{RE m_b_U;}DF_OF_CPOINT(Unit);DF_OF_CPOINT(Zero);DF_OF_CPOINT(One);DF_OF_CPOINT(Infty);DF_OF_CPOINT(size);DF_OF_POINT(init);DF_OF_POINT(root);TE <TY U,TY F_U> IN AbstractNSet<U,F_U>::AbstractNSet(F_U& f_U):m_f_U(f_U){ST_AS(is_invocable_r_v<U,F_U,U>);}TE <TY U,TY F_U> IN U AbstractNSet<U,F_U>::Transfer(CO U& u){RE m_f_U(u);}TE <TY U> IN U VirtualNSet<U>::Inverse(CO U& u){RE Transfer(u);}TE <TY U,TY M_U> IN AbstractMagma<U,M_U>::AbstractMagma(M_U& m_U):m_m_U(m_U){ST_AS(is_invocable_r_v<U,M_U,U,U>);}TE <TY U> IN U AdditiveMagma<U>::Product(CO U& u0,CO U& u1){RE u0 + u1;}TE <TY U> IN U MultiplicativeMagma<U>::Product(CO U& u0,CO U& u1){RE u0 * u1;}TE <TY U,TY M_U> IN U AbstractMagma<U,M_U>::Product(CO U& u0,CO U& u1){RE m_m_U(u0,u1);}TE <TY U> IN U VirtualMagma<U>::Sum(CO U& u0,CO U& u1){RE Product(u0,u1);}

TE <TY U>CL VirtualMonoid:virtual PU VirtualMagma<U>,virtual PU VirtualPointedSet<U>{};TE <TY U = ll>CL AdditiveMonoid:virtual PU VirtualMonoid<U>,PU AdditiveMagma<U>,PU PointedSet<U>{};TE <TY U = ll>CL MultiplicativeMonoid:virtual PU VirtualMonoid<U>,PU MultiplicativeMagma<U>,PU PointedSet<U>{PU:IN MultiplicativeMonoid(CO U& e_U);};TE <TY U,TY M_U>CL AbstractMonoid:virtual PU VirtualMonoid<U>,PU AbstractMagma<U,M_U>,PU PointedSet<U>{PU:IN AbstractMonoid(M_U& m_U,CO U& e_U);};
TE <TY U> IN MultiplicativeMonoid<U>::MultiplicativeMonoid(CO U& e_U):PointedSet<U>(e_U){}TE <TY U,TY M_U> IN AbstractMonoid<U,M_U>::AbstractMonoid(M_U& m_U,CO U& e_U):AbstractMagma<U,M_U>(m_U),PointedSet<U>(e_U){}

TE <TY U>CL VirtualGroup:virtual PU VirtualMonoid<U>,virtual PU VirtualPointedSet<U>,virtual PU VirtualNSet<U>{};TE <TY U = ll>CL AdditiveGroup:virtual PU VirtualGroup<U>,PU AdditiveMonoid<U>{PU:IN U Transfer(CO U& u);};TE <TY U,TY M_U,TY I_U>CL AbstractGroup:virtual PU VirtualGroup<U>,PU AbstractMonoid<U,M_U>,PU AbstractNSet<U,I_U>{PU:IN AbstractGroup(M_U& m_U,CO U& e_U,I_U& i_U);IN U Transfer(CO U& u);};TE <TY U,TY M_U,TY I_U> IN AbstractGroup<U,M_U,I_U>::AbstractGroup(M_U& m_U,CO U& e_U,I_U& i_U):AbstractMonoid<U,M_U>(m_U,e_U),AbstractNSet<U,I_U>(i_U){}TE <TY U,TY M_U,TY I_U> IN U AbstractGroup<U,M_U,I_U>::Transfer(CO U& u){RE m_i_U(u);}TE <TY U> IN U AdditiveGroup<U>::Transfer(CO U& u){RE -u;}

// Graph
// c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/compress.txt
#define SFINAE_FOR_GRAPH TY T,TY E,enable_if_t<is_invocable_v<E,T>,void*> PTR
TE <TY T,TY R1,TY R2,TY E>CL VirtualGraph:PU UnderlyingSet<T>{PU:int m_SZ;E m_edge;IN VirtualGraph(CRI SZ,E edge);virtual R1 Enumeration(CRI i)= 0;virtual R2 Enumeration_inv(CO T& t)= 0;IN VO Reset();IN CRI SZ()CO NE;IN E& edge()NE;IN ret_t<E,T> Edge(CO T& t);US type = T;};TE <TY E>CL Graph:virtual PU VirtualGraph<int,CRI,CRI,E>{PU:IN Graph(CRI SZ,E edge);IN CRI Enumeration(CRI i);IN CRI Enumeration_inv(CRI t);TE <TY F> IN Graph<F> GetGraph(F edge)CO;};TE <TY T,TY Enum_T,TY Enum_T_inv,TY E>CL EnumerationGraph:virtual PU VirtualGraph<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>{PU:Enum_T& m_enum_T;Enum_T_inv& m_enum_T_inv;IN EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge);IN ret_t<Enum_T,int> Enumeration(CRI i);IN ret_t<Enum_T_inv,T> Enumeration_inv(CO T& t);TE <TY F> IN EnumerationGraph<T,Enum_T,Enum_T_inv,F> GetGraph(F edge)CO;};TE <TY Enum_T,TY Enum_T_inv,TY E> EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge)-> EnumerationGraph<decldecay_t(declval<Enum_T>()(0)),Enum_T,Enum_T_inv,E>;TE <SFINAE_FOR_GRAPH = nullptr>CL MemorisationGraph:virtual PU VirtualGraph<T,T,CRI,E>{PU:int m_LE;VE<T> m_memory;Map<T,int> m_memory_inv;IN MemorisationGraph(CRI SZ,E edge);IN T Enumeration(CRI i);IN CRI Enumeration_inv(CO T& t);IN VO Reset();TE <TY F> IN MemorisationGraph<T,F> GetGraph(F edge)CO;};TE <TY E> MemorisationGraph(CRI SZ,E edge)-> MemorisationGraph<decldecay_t(declval<E>()().back()),E>;TE <TY E> MemorisationGraph(CRI SZ,E edge)-> MemorisationGraph<decldecay_t(get<0>(declval<E>()().back())),E>;
TE <TY T,TY R1,TY R2,TY E> IN VirtualGraph<T,R1,R2,E>::VirtualGraph(CRI SZ,E edge):m_SZ(SZ),m_edge(MO(edge)){ST_AS(is_COructible_v<T,R1> && is_COructible_v<int,R2> && is_invocable_v<E,T>);}TE <TY E> IN Graph<E>::Graph(CRI SZ,E edge):VirtualGraph<int,CRI,CRI,E>(SZ,MO(edge)){}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN EnumerationGraph<T,Enum_T,Enum_T_inv,E>::EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge):VirtualGraph<T,ret_t<Enum_T,int>,ret_t<Enum_T_inv,T>,E>(SZ,MO(edge)),m_enum_T(enum_T),m_enum_T_inv(enum_T_inv){}TE <SFINAE_FOR_GRAPH> IN MemorisationGraph<T,E,PTR>::MemorisationGraph(CRI SZ,E edge):VirtualGraph<T,T,CRI,E>(SZ,MO(edge)),m_LE(),m_memory(),m_memory_inv(){}TE <TY E> IN CRI Graph<E>::Enumeration(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN ret_t<Enum_T,int> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration(CRI i){RE m_enum_T(i);}TE <SFINAE_FOR_GRAPH> IN T MemorisationGraph<T,E,PTR>::Enumeration(CRI i){AS(0 <= i && i < m_LE);RE m_memory[i];}TE <TY E> IN CRI Graph<E>::Enumeration_inv(CRI i){RE i;}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> IN ret_t<Enum_T_inv,T> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::Enumeration_inv(CO T& t){RE m_enum_T_inv(t);}TE <SFINAE_FOR_GRAPH> IN CRI MemorisationGraph<T,E,PTR>::Enumeration_inv(CO T& t){if(m_memory_inv.count(t)== 0){AS(m_LE < TH->SZ());m_memory.push_back(t);RE m_memory_inv[t]= m_LE++;}RE m_memory_inv[t];}TE <TY T,TY R1,TY R2,TY E> VO VirtualGraph<T,R1,R2,E>::Reset(){}TE <SFINAE_FOR_GRAPH> IN VO MemorisationGraph<T,E,PTR>::Reset(){m_LE = 0;m_memory.clear();m_memory_inv.clear();}TE <TY T,TY R1,TY R2,TY E> IN CRI VirtualGraph<T,R1,R2,E>::SZ()CO NE{RE m_SZ;}TE <TY T,TY R1,TY R2,TY E> IN E& VirtualGraph<T,R1,R2,E>::edge()NE{RE m_edge;}TE <TY T,TY R1,TY R2,TY E> IN ret_t<E,T> VirtualGraph<T,R1,R2,E>::Edge(CO T& t){RE m_edge(t);}TE <TY E> TE <TY F> IN Graph<F> Graph<E>::GetGraph(F edge)CO{RE Graph<F>(TH->SZ(),MO(edge));}TE <TY T,TY Enum_T,TY Enum_T_inv,TY E> TE <TY F> IN EnumerationGraph<T,Enum_T,Enum_T_inv,F> EnumerationGraph<T,Enum_T,Enum_T_inv,E>::GetGraph(F edge)CO{RE EnumerationGraph(TH->SZ(),m_enum_T,m_enum_T_inv,MO(edge));}TE <SFINAE_FOR_GRAPH> TE <TY F> IN MemorisationGraph<T,F> MemorisationGraph<T,E,PTR>::GetGraph(F edge)CO{RE MemorisationGraph(TH->SZ(),MO(edge));}

// ConstexprModulo
// c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Mod/ConstexprModulo/a.hpp
CEXPR(uint,P,998244353);TE <uint M,TY INT> CE INT& RS(INT& n)NE{RE n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n %= M;}TE <uint M> CE uint& RS(uint& n)NE{RE n %= M;}TE <uint M> CE ull& RS(ull& n)NE{RE n %= M;}TE <TY INT> 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 <> CE ull& RS<P,ull>(ull& n)NE{CE CO ull Pull = P;CE CO ull Pull2 =(Pull - 1)*(Pull - 1);RE RSP(n > Pull2?n -= Pull2:n);}TE <uint M,TY INT> CE INT RS(INT&& n)NE{RE MO(RS<M>(n));}TE <uint M,TY INT> CE INT RS(CO INT& n)NE{RE RS<M>(INT(n));}

#define SFINAE_FOR_MOD(DEFAULT)TY T,enable_if_t<is_constructible<uint,decay_t<T> >::value>* DEFAULT
#define DC_OF_CM_FOR_MOD(FUNC)CE bool OP FUNC(CO Mod<M>& n)CO NE
#define DC_OF_AR_FOR_MOD(FUNC)CE Mod<M> OP FUNC(CO Mod<M>& n)CO NE;TE <SFINAE_FOR_MOD(= nullptr)> CE Mod<M> OP FUNC(T&& n)CO NE;
#define DF_OF_CM_FOR_MOD(FUNC)TE <uint M> CE bool Mod<M>::OP FUNC(CO Mod<M>& n)CO NE{RE m_n FUNC n.m_n;}
#define DF_OF_AR_FOR_MOD(FUNC,FORMULA)TE <uint M> CE Mod<M> Mod<M>::OP FUNC(CO Mod<M>& n)CO NE{RE MO(Mod<M>(*TH)FUNC ## = n);}TE <uint M> TE <SFINAE_FOR_MOD()> CE Mod<M> Mod<M>::OP FUNC(T&& n)CO NE{RE FORMULA;}TE <uint M,SFINAE_FOR_MOD(= nullptr)> CE Mod<M> OP FUNC(T&& n0,CO Mod<M>& n1)NE{RE MO(Mod<M>(forward<T>(n0))FUNC ## = n1);}

TE <uint M>CL Mod{PU:uint m_n;CE Mod()NE;CE Mod(CO Mod<M>& n)NE;CE Mod(Mod<M>& n)NE;CE Mod(Mod<M>&& n)NE;TE <SFINAE_FOR_MOD(= nullptr)> CE Mod(CO T& n)NE;TE <SFINAE_FOR_MOD(= nullptr)> CE Mod(T& n)NE;TE <SFINAE_FOR_MOD(= nullptr)> CE Mod(T&& n)NE;CE Mod<M>& OP=(CO Mod<M>& n)NE;CE Mod<M>& OP=(Mod<M>&& n)NE;CE Mod<M>& OP+=(CO Mod<M>& n)NE;CE Mod<M>& OP-=(CO Mod<M>& n)NE;CE Mod<M>& OP*=(CO Mod<M>& n)NE;IN Mod<M>& OP/=(CO Mod<M>& n);CE Mod<M>& OP<<=(int n)NE;CE Mod<M>& OP>>=(int n)NE;CE Mod<M>& OP++()NE;CE Mod<M> OP++(int)NE;CE Mod<M>& OP--()NE;CE Mod<M> 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(/);CE Mod<M> OP<<(int n)CO NE;CE Mod<M> OP>>(int n)CO NE;CE Mod<M> OP-()CO NE;CE Mod<M>& SignInvert()NE;CE Mod<M>& Double()NE;CE Mod<M>& Halve()NE;IN Mod<M>& Invert();TE <TY T> CE Mod<M>& PositivePW(T&& EX)NE;TE <TY T> CE Mod<M>& NonNegativePW(T&& EX)NE;TE <TY T> CE Mod<M>& PW(T&& EX);CE VO swap(Mod<M>& n)NE;CE CRUI RP()CO NE;ST CE Mod<M> DeRP(CRUI n)NE;ST CE uint& Normalise(uint& n)NE;ST IN CO Mod<M>& Inverse(CRUI n)NE;ST IN CO Mod<M>& Factorial(CRUI n)NE;ST IN CO Mod<M>& FactorialInverse(CRUI n)NE;ST IN Mod<M> Combination(CRUI n,CRUI i)NE;ST IN CO Mod<M>& zero()NE;ST IN CO Mod<M>& one()NE;TE <TY T> CE Mod<M>& Ref(T&& n)NE;};

#define SFINAE_FOR_MN(DEFAULT)TY T,enable_if_t<is_constructible<Mod<M>,decay_t<T> >::value>* DEFAULT
#define DC_OF_AR_FOR_MN(FUNC)IN MN<M> OP FUNC(CO MN<M>& n)CO NE;TE <SFINAE_FOR_MOD(= nullptr)> IN MN<M> OP FUNC(T&& n)CO NE;
#define DF_OF_CM_FOR_MN(FUNC)TE <uint M> IN bool MN<M>::OP FUNC(CO MN<M>& n)CO NE{RE m_n FUNC n.m_n;}
#define DF_OF_AR_FOR_MN(FUNC,FORMULA)TE <uint M> IN MN<M> MN<M>::OP FUNC(CO MN<M>& n)CO NE{RE MO(MN<M>(*TH)FUNC ## = n);}TE <uint M> TE <SFINAE_FOR_MOD()> IN MN<M> MN<M>::OP FUNC(T&& n)CO NE{RE FORMULA;}TE <uint M,SFINAE_FOR_MOD(= nullptr)> IN MN<M> OP FUNC(T&& n0,CO MN<M>& n1)NE{RE MO(MN<M>(forward<T>(n0))FUNC ## = n1);}

TE <uint M>CL MN:PU Mod<M>{PU:CE MN()NE;CE MN(CO MN<M>& n)NE;CE MN(MN<M>& n)NE;CE MN(MN<M>&& n)NE;TE <SFINAE_FOR_MN(= nullptr)> CE MN(CO T& n)NE;TE <SFINAE_FOR_MN(= nullptr)> CE MN(T&& n)NE;CE MN<M>& OP=(CO MN<M>& n)NE;CE MN<M>& OP=(MN<M>&& n)NE;CE MN<M>& OP+=(CO MN<M>& n)NE;CE MN<M>& OP-=(CO MN<M>& n)NE;CE MN<M>& OP*=(CO MN<M>& n)NE;IN MN<M>& OP/=(CO MN<M>& n);CE MN<M>& OP<<=(int n)NE;CE MN<M>& OP>>=(int n)NE;CE MN<M>& OP++()NE;CE MN<M> OP++(int)NE;CE MN<M>& OP--()NE;CE MN<M> OP--(int)NE;DC_OF_AR_FOR_MN(+);DC_OF_AR_FOR_MN(-);DC_OF_AR_FOR_MN(*);DC_OF_AR_FOR_MN(/);CE MN<M> OP<<(int n)CO NE;CE MN<M> OP>>(int n)CO NE;CE MN<M> OP-()CO NE;CE MN<M>& SignInvert()NE;CE MN<M>& Double()NE;CE MN<M>& Halve()NE;CE MN<M>& Invert();TE <TY T> CE MN<M>& PositivePW(T&& EX)NE;TE <TY T> CE MN<M>& NonNegativePW(T&& EX)NE;TE <TY T> CE MN<M>& PW(T&& EX);CE uint RP()CO NE;CE Mod<M> Reduce()CO NE;ST CE MN<M> DeRP(CRUI n)NE;ST IN CO MN<M>& Formise(CRUI n)NE;ST IN CO MN<M>& Inverse(CRUI n)NE;ST IN CO MN<M>& Factorial(CRUI n)NE;ST IN CO MN<M>& FactorialInverse(CRUI n)NE;ST IN MN<M> Combination(CRUI n,CRUI i)NE;ST IN CO MN<M>& zero()NE;ST IN CO MN<M>& one()NE;ST CE uint Form(CRUI n)NE;ST CE ull& Reduction(ull& n)NE;ST CE ull& ReducedMU(ull& n,CRUI m)NE;ST CE uint MU(CRUI n0,CRUI n1)NE;ST CE uint BaseSquareTruncation(uint& n)NE;TE <TY T> CE MN<M>& Ref(T&& n)NE;};TE <uint M> CE MN<M> Twice(CO MN<M>& n)NE;TE <uint M> CE MN<M> Half(CO MN<M>& n)NE;TE <uint M> CE MN<M> Inverse(CO MN<M>& n);TE <uint M,TY T> CE MN<M> PW(MN<M> n,T EX);TE <TY T> CE MN<2> PW(CO MN<2>& n,CO T& p);TE <TY T> CE T Square(CO T& t);TE <> CE MN<2> Square<MN<2> >(CO MN<2>& t);TE <uint M> CE VO swap(MN<M>& n0,MN<M>& n1)NE;TE <uint M> IN string to_string(CO MN<M>& n)NE;TE<uint M,CL Traits> IN basic_istream<char,Traits>& OP>>(basic_istream<char,Traits>& is,MN<M>& n);TE<uint M,CL Traits> IN basic_ostream<char,Traits>& OP<<(basic_ostream<char,Traits>& os,CO MN<M>& n);

TE <uint M>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 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 <uint M> CE ull COantsForMod<M>::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;}

US MP = Mod<P>;US MNP = MN<P>;TE <uint M> CE uint MN<M>::Form(CRUI n)NE{ull n_copy = n;RE uint(MO(Reduction(n_copy *= COantsForMod<M>::g_MN_base_square_mod)));}TE <uint M> CE ull& MN<M>::Reduction(ull& n)NE{ull n_sub = n & COantsForMod<M>::g_MN_base_minus;RE((n +=((n_sub *= COantsForMod<M>::g_MN_M_neg_inverse)&= COantsForMod<M>::g_MN_base_minus)*= M)>>= COantsForMod<M>::g_MN_digit)< M?n:n -= M;}TE <uint M> CE ull& MN<M>::ReducedMU(ull& n,CRUI m)NE{RE Reduction(n *= m);}TE <uint M> CE uint MN<M>::MU(CRUI n0,CRUI n1)NE{ull n0_copy = n0;RE uint(MO(ReducedMU(ReducedMU(n0_copy,n1),COantsForMod<M>::g_MN_base_square_mod)));}TE <uint M> CE uint MN<M>::BaseSquareTruncation(uint& n)NE{CO uint n_u = n >> COantsForMod<M>::g_MN_digit_half;n &= COantsForMod<M>::g_MN_base_sqrt_minus;RE n_u;}TE <uint M> CE MN<M>::MN()NE:Mod<M>(){static_assert(! COantsForMod<M>::g_even);}TE <uint M> CE MN<M>::MN(CO MN<M>& n)NE:Mod<M>(n){}TE <uint M> CE MN<M>::MN(MN<M>& n)NE:Mod<M>(n){}TE <uint M> CE MN<M>::MN(MN<M>&& n)NE:Mod<M>(MO(n)){}TE <uint M> TE <SFINAE_FOR_MN()> CE MN<M>::MN(CO T& n)NE:Mod<M>(n){static_assert(! COantsForMod<M>::g_even);Mod<M>::m_n = Form(Mod<M>::m_n);}TE <uint M> TE <SFINAE_FOR_MN()> CE MN<M>::MN(T&& n)NE:Mod<M>(forward<T>(n)){static_assert(! COantsForMod<M>::g_even);Mod<M>::m_n = Form(Mod<M>::m_n);}TE <uint M> CE MN<M>& MN<M>::OP=(CO MN<M>& n)NE{RE Ref(Mod<M>::OP=(n));}TE <uint M> CE MN<M>& MN<M>::OP=(MN<M>&& n)NE{RE Ref(Mod<M>::OP=(MO(n)));}TE <uint M> CE MN<M>& MN<M>::OP+=(CO MN<M>& n)NE{RE Ref(Mod<M>::OP+=(n));}TE <uint M> CE MN<M>& MN<M>::OP-=(CO MN<M>& n)NE{RE Ref(Mod<M>::OP-=(n));}TE <uint M> CE MN<M>& MN<M>::OP*=(CO MN<M>& n)NE{ull m_n_copy = Mod<M>::m_n;RE Ref(Mod<M>::m_n = MO(ReducedMU(m_n_copy,n.m_n)));}TE <uint M> IN MN<M>& MN<M>::OP/=(CO MN<M>& n){RE OP*=(MN<M>(n).Invert());}TE <uint M> CE MN<M>& MN<M>::OP<<=(int n)NE{RE Ref(Mod<M>::OP<<=(n));}TE <uint M> CE MN<M>& MN<M>::OP>>=(int n)NE{RE Ref(Mod<M>::OP>>=(n));}TE <uint M> CE MN<M>& MN<M>::OP++()NE{RE Ref(Mod<M>::Normalise(Mod<M>::m_n += COantsForMod<M>::g_MN_base_mod));}TE <uint M> CE MN<M> MN<M>::OP++(int)NE{MN<M> n{*TH};OP++();RE n;}TE <uint M> CE MN<M>& MN<M>::OP--()NE{RE Ref(Mod<M>::m_n < COantsForMod<M>::g_MN_base_mod?((Mod<M>::m_n += M)-= COantsForMod<M>::g_MN_base_mod):Mod<M>::m_n -= COantsForMod<M>::g_MN_base_mod);}TE <uint M> CE MN<M> MN<M>::OP--(int)NE{MN<M> n{*TH};OP--();RE n;}DF_OF_AR_FOR_MN(+,MN<M>(forward<T>(n))+= *TH);DF_OF_AR_FOR_MN(-,MN<M>(forward<T>(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MN(*,MN<M>(forward<T>(n))*= *TH);DF_OF_AR_FOR_MN(/,MN<M>(forward<T>(n)).Invert()*= *TH);TE <uint M> CE MN<M> MN<M>::OP<<(int n)CO NE{RE MO(MN<M>(*TH)<<= n);}TE <uint M> CE MN<M> MN<M>::OP>>(int n)CO NE{RE MO(MN<M>(*TH)>>= n);}TE <uint M> CE MN<M> MN<M>::OP-()CO NE{RE MO(MN<M>(*TH).SignInvert());}TE <uint M> CE MN<M>& MN<M>::SignInvert()NE{RE Ref(Mod<M>::m_n > 0?Mod<M>::m_n = M - Mod<M>::m_n:Mod<M>::m_n);}TE <uint M> CE MN<M>& MN<M>::Double()NE{RE Ref(Mod<M>::Double());}TE <uint M> CE MN<M>& MN<M>::Halve()NE{RE Ref(Mod<M>::Halve());}TE <uint M> CE MN<M>& MN<M>::Invert(){assert(Mod<M>::m_n > 0);RE PositivePW(uint(COantsForMod<M>::g_M_minus_2));}TE <uint M> TE <TY T> CE MN<M>& MN<M>::PositivePW(T&& EX)NE{MN<M> PW{*TH};(--EX)%= COantsForMod<M>::g_M_minus_2;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <uint M> TE <TY T> CE MN<M>& MN<M>::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(Mod<M>::m_n = COantsForMod<M>::g_MN_base_mod):PositivePW(forward<T>(EX));}TE <uint M> TE <TY T> CE MN<M>& MN<M>::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod<M>::m_n == 0));RE neg?PositivePW(forward<T>(EX *= COantsForMod<M>::g_M_minus_2_neg)):NonNegativePW(forward<T>(EX));}TE <uint M> CE uint MN<M>::RP()CO NE{ull m_n_copy = Mod<M>::m_n;RE MO(Reduction(m_n_copy));}TE <uint M> CE Mod<M> MN<M>::Reduce()CO NE{ull m_n_copy = Mod<M>::m_n;RE Mod<M>::DeRP(MO(Reduction(m_n_copy)));}TE <uint M> CE MN<M> MN<M>::DeRP(CRUI n)NE{RE MN<M>(Mod<M>::DeRP(n));}TE <uint M> IN CO MN<M>& MN<M>::Formise(CRUI n)NE{ST MN<M> memory[COantsForMod<M>::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 <uint M> IN CO MN<M>& MN<M>::Inverse(CRUI n)NE{ST MN<M> memory[COantsForMod<M>::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN<M>(Mod<M>::Inverse(LE_curr));LE_curr++;}RE memory[n];}TE <uint M> IN CO MN<M>& MN<M>::Factorial(CRUI n)NE{ST MN<M> memory[COantsForMod<M>::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN<M> val_curr{one()};ST MN<M> val_last{one()};WH(LE_curr <= n){memory[LE_curr++] = val_curr *= ++val_last;}RE memory[n];}TE <uint M> IN CO MN<M>& MN<M>::FactorialInverse(CRUI n)NE{ST MN<M> memory[COantsForMod<M>::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN<M> val_curr{one()};ST MN<M> val_last{one()};WH(LE_curr <= n){memory[LE_curr] = val_curr *= Inverse(LE_curr);LE_curr++;}RE memory[n];}TE <uint M> IN MN<M> MN<M>::Combination(CRUI n,CRUI i)NE{RE i <= n?Factorial(n)*FactorialInverse(i)*FactorialInverse(n - i):zero();}TE <uint M> IN CO MN<M>& MN<M>::zero()NE{ST CE CO MN<M> z{};RE z;}TE <uint M> IN CO MN<M>& MN<M>::one()NE{ST CE CO MN<M> o{DeRP(1)};RE o;}TE <uint M> TE <TY T> CE MN<M>& MN<M>::Ref(T&& n)NE{RE *TH;}TE <uint M> CE MN<M> Twice(CO MN<M>& n)NE{RE MO(MN<M>(n).Double());}TE <uint M> CE MN<M> Half(CO MN<M>& n)NE{RE MO(MN<M>(n).Halve());}TE <uint M> CE MN<M> Inverse(CO MN<M>& n){RE MO(MN<M>(n).Invert());}TE <uint M,TY T> CE MN<M> PW(MN<M> n,T EX){RE MO(n.PW(EX));}TE <uint M> CE VO swap(MN<M>& n0,MN<M>& n1)NE{n0.swap(n1);}TE <uint M> IN string to_string(CO MN<M>& n)NE{RE to_string(n.RP())+ " + MZ";}TE<uint M,CL Traits> IN basic_istream<char,Traits>& OP>>(basic_istream<char,Traits>& is,MN<M>& n){ll m;is >> m;n = m;RE is;}TE<uint M,CL Traits> IN basic_ostream<char,Traits>& OP<<(basic_ostream<char,Traits>& os,CO MN<M>& n){RE os << n.RP();}

TE <uint M> CE Mod<M>::Mod()NE:m_n(){}TE <uint M> CE Mod<M>::Mod(CO Mod<M>& n)NE:m_n(n.m_n){}TE <uint M> CE Mod<M>::Mod(Mod<M>& n)NE:m_n(n.m_n){}TE <uint M> CE Mod<M>::Mod(Mod<M>&& n)NE:m_n(MO(n.m_n)){}TE <uint M> TE <SFINAE_FOR_MOD()> CE Mod<M>::Mod(CO T& n)NE:m_n(RS<M>(n)){}TE <uint M> TE <SFINAE_FOR_MOD()> CE Mod<M>::Mod(T& n)NE:m_n(RS<M>(decay_t<T>(n))){}TE <uint M> TE <SFINAE_FOR_MOD()> CE Mod<M>::Mod(T&& n)NE:m_n(RS<M>(forward<T>(n))){}TE <uint M> CE Mod<M>& Mod<M>::OP=(CO Mod<M>& n)NE{RE Ref(m_n = n.m_n);}TE <uint M> CE Mod<M>& Mod<M>::OP=(Mod<M>&& n)NE{RE Ref(m_n = MO(n.m_n));}TE <uint M> CE Mod<M>& Mod<M>::OP+=(CO Mod<M>& n)NE{RE Ref(Normalise(m_n += n.m_n));}TE <uint M> CE Mod<M>& Mod<M>::OP-=(CO Mod<M>& n)NE{RE Ref(m_n < n.m_n?(m_n += M)-= n.m_n:m_n -= n.m_n);}TE <uint M> CE Mod<M>& Mod<M>::OP*=(CO Mod<M>& n)NE{RE Ref(m_n = COantsForMod<M>::g_even?RS<M>(ull(m_n)* n.m_n):MN<M>::MU(m_n,n.m_n));}TE <> 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 <uint M> IN Mod<M>& Mod<M>::OP/=(CO Mod<M>& n){RE OP*=(Mod<M>(n).Invert());}TE <uint M> CE Mod<M>& Mod<M>::OP<<=(int n)NE{WH(n-- > 0){Normalise(m_n <<= 1);}RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP>>=(int n)NE{WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += M)>>= 1;}RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP++()NE{RE Ref(m_n < COantsForMod<M>::g_M_minus?++m_n:m_n = 0);}TE <uint M> CE Mod<M> Mod<M>::OP++(int)NE{Mod<M> n{*TH};OP++();RE n;}TE <uint M> CE Mod<M>& Mod<M>::OP--()NE{RE Ref(m_n == 0?m_n = COantsForMod<M>::g_M_minus:--m_n);}TE <uint M> CE Mod<M> Mod<M>::OP--(int)NE{Mod<M> 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<M>(forward<T>(n))+= *TH);DF_OF_AR_FOR_MOD(-,Mod<M>(forward<T>(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MOD(*,Mod<M>(forward<T>(n))*= *TH);DF_OF_AR_FOR_MOD(/,Mod<M>(forward<T>(n)).Invert()*= *TH);TE <uint M> CE Mod<M> Mod<M>::OP<<(int n)CO NE{RE MO(Mod<M>(*TH)<<= n);}TE <uint M> CE Mod<M> Mod<M>::OP>>(int n)CO NE{RE MO(Mod<M>(*TH)>>= n);}TE <uint M> CE Mod<M> Mod<M>::OP-()CO NE{RE MO(Mod<M>(*TH).SignInvert());}TE <uint M> CE Mod<M>& Mod<M>::SignInvert()NE{RE Ref(m_n > 0?m_n = M - m_n:m_n);}TE <uint M> CE Mod<M>& Mod<M>::Double()NE{RE Ref(Normalise(m_n <<= 1));}TE <uint M> CE Mod<M>& Mod<M>::Halve()NE{RE Ref(((m_n & 1)== 0?m_n:m_n += M)>>= 1);}TE <uint M> IN Mod<M>& Mod<M>::Invert(){assert(m_n > 0);uint m_n_neg;RE m_n < COantsForMod<M>::g_memory_LE?Ref(m_n = Inverse(m_n).m_n):((m_n_neg = M - m_n)< COantsForMod<M>::g_memory_LE)?Ref(m_n = M - Inverse(m_n_neg).m_n):PositivePW(uint(COantsForMod<M>::g_M_minus_2));}TE <> IN Mod<2>& Mod<2>::Invert(){assert(m_n > 0);RE *TH;}TE <uint M> TE <TY T> CE Mod<M>& Mod<M>::PositivePW(T&& EX)NE{Mod<M> PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <> TE <TY T> CE Mod<2>& Mod<2>::PositivePW(T&& EX)NE{RE *TH;}TE <uint M> TE <TY T> CE Mod<M>& Mod<M>::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(m_n = 1):Ref(PositivePW(forward<T>(EX)));}TE <uint M> TE <TY T> CE Mod<M>& Mod<M>::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod<M>::m_n == 0));RE neg?PositivePW(forward<T>(EX *= COantsForMod<M>::g_M_minus_2_neg)):NonNegativePW(forward<T>(EX));}TE <uint M> IN CO Mod<M>& Mod<M>::Inverse(CRUI n)NE{ST Mod<M> memory[COantsForMod<M>::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr].m_n = M - MN<M>::MU(memory[M % LE_curr].m_n,M / LE_curr);LE_curr++;}RE memory[n];}TE <uint M> IN CO Mod<M>& Mod<M>::Factorial(CRUI n)NE{ST Mod<M> memory[COantsForMod<M>::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN<M>::Factorial(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE <uint M> IN CO Mod<M>& Mod<M>::FactorialInverse(CRUI n)NE{ST Mod<M> memory[COantsForMod<M>::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN<M>::FactorialInverse(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE <uint M> IN Mod<M> Mod<M>::Combination(CRUI n,CRUI i)NE{RE MN<M>::Combination(n,i).Reduce();}TE <uint M> CE VO Mod<M>::swap(Mod<M>& n)NE{std::swap(m_n,n.m_n);}TE <uint M> CE CRUI Mod<M>::RP()CO NE{RE m_n;}TE <uint M> CE Mod<M> Mod<M>::DeRP(CRUI n)NE{Mod<M> n_copy{};n_copy.m_n = n;RE n_copy;}TE <uint M> CE uint& Mod<M>::Normalise(uint& n)NE{RE n < M?n:n -= M;}TE <uint M> IN CO Mod<M>& Mod<M>::zero()NE{ST CE CO Mod<M> z{};RE z;}TE <uint M> IN CO Mod<M>& Mod<M>::one()NE{ST CE CO Mod<M> o{DeRP(1)};RE o;}TE <uint M> TE <TY T> CE Mod<M>& Mod<M>::Ref(T&& n)NE{RE *TH;}TE <uint M> CE Mod<M> Twice(CO Mod<M>& n)NE{RE MO(Mod<M>(n).Double());}TE <uint M> CE Mod<M> Half(CO Mod<M>& n)NE{RE MO(Mod<M>(n).Halve());}TE <uint M> IN Mod<M> Inverse(CO Mod<M>& n){RE MO(Mod<M>(n).Invert());}TE <uint M> CE Mod<M> Inverse_COrexpr(CRUI n)NE{RE MO(Mod<M>::DeRP(RS<M>(n)).NonNegativePW(M - 2));}TE <uint M,TY T> CE Mod<M> PW(Mod<M> n,T EX){RE MO(n.PW(EX));}TE <TY T>CE Mod<2> PW(Mod<2> n,const T& p){RE p == 0?Mod<2>::one():move(n);}TE <uint M> CE VO swap(Mod<M>& n0,Mod<M>& n1)NE{n0.swap(n1);}TE <uint M> IN string to_string(CO Mod<M>& n)NE{RE to_string(n.RP())+ " + MZ";}TE<uint M,CL Traits> IN basic_istream<char,Traits>& OP>>(basic_istream<char,Traits>& is,Mod<M>& n){ll m;is >> m;n = m;RE is;}TE<uint M,CL Traits> IN basic_ostream<char,Traits>& OP<<(basic_ostream<char,Traits>& os,CO Mod<M>& n){RE os << n.RP();}

// AAA 常設ライブラリは以上に挿入する。

#define INCLUDE_LIBRARY
#include __FILE__

#endif // INCLUDE_LIBRARY

#endif // INCLUDE_SUB

#endif // INCLUDE_MAIN
0