#include using namespace std; using ll = long long; #define TYPE_OF( VAR ) remove_const::type >::type #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define CIN( LL , A ) LL A; cin >> A #define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) #define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define QUIT return 0 #define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT #define POWER( ANSWER , VAR , EXPONENT_REF , MODULO ) \ TYPE_OF( VAR ) ANSWER = 1; \ TYPE_OF( VAR ) VARIABLE_FOR_SQUARE_FOR_POWER = VAR; \ while( EXPONENT_REF != 0 ){ \ if( EXPONENT_REF % 2 == 1 ){ \ ANSWER = ( ANSWER * VARIABLE_FOR_SQUARE_FOR_POWER ) % MODULO; \ } \ VARIABLE_FOR_SQUARE_FOR_POWER = ( VARIABLE_FOR_SQUARE_FOR_POWER * VARIABLE_FOR_SQUARE_FOR_POWER ) % MODULO; \ EXPONENT_REF /= 2; \ } \ class Span { public: ll m_Li; ll m_Ri; inline Span( const ll& Li = 0 , const ll& Ri = 0 ) : m_Li( Li ) , m_Ri( Ri ) {} }; class Ord { public: inline Ord() = default; inline bool operator()( const Span& S0 , const Span& S1 ) { return S0.m_Li < S1.m_Li; }; }; // InitialSegmentSumで負の入力を扱うためにuintではなくintをテンプレート引数にする。 template class BIT { private: T m_fenwick[N + 1]; public: inline BIT(); inline BIT( const T ( & a )[N] ); 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 ); inline T IntervalSum( const int& i_start , const int& i_final ); }; template inline BIT::BIT() : m_fenwick() {} template inline BIT::BIT( const T ( & a )[N] ) : m_fenwick() { operator+=( a ); } 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 ) { T sum = 0; int j = i_final + 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 ) { return InitialSegmentSum( i_final ) - InitialSegmentSum( i_start - 1 ); } template class IntervalAddBIT { private: BIT m_bit_0; BIT m_bit_1; public: inline IntervalAddBIT(); inline IntervalAddBIT( const T ( & a )[N] ); inline void Set( const int& i , const T& n ); inline IntervalAddBIT& operator+=( const T ( & a )[N] ); inline void Add( const int& i , const T& n ); inline void IntervalAdd( const int& i_start , const int& i_final , const T& n ); inline T InitialSegmentSum( const int& i_final ); inline T IntervalSum( const int& i_start , const int& i_final ); }; template inline IntervalAddBIT::IntervalAddBIT() : m_bit_0() , m_bit_1() {} template inline IntervalAddBIT::IntervalAddBIT( const T ( & a )[N] ) : m_bit_0() , m_bit_1() { operator+=( a ); } template inline void IntervalAddBIT::Set( const int& i , const T& n ) { Add( i , n - IntervalSum( i , i ) ); } template inline IntervalAddBIT& IntervalAddBIT::operator+=( const T ( & a )[N] ) { for( int i = 0 ; i < N ; i++ ){ Add( i , a[i] ); } return *this; } template inline void IntervalAddBIT::Add( const int& i , const T& n ) { IntervalAdd( i , i , n ); } template inline void IntervalAddBIT::IntervalAdd( const int& i_start , const int& i_final , const T& n ) { m_bit_0.Add( i_start , - n * ( i_start - 1 ) ); m_bit_0.Add( i_final + 1 , n * i_final ); m_bit_1.Add( i_start , n ); m_bit_1.Add( i_final + 1 , - n ); } template inline T IntervalAddBIT::InitialSegmentSum( const int& i_final ) { return m_bit_0.InitialSegmentSum( i_final ) + i_final * m_bit_1.InitialSegmentSum( i_final ); } template inline T IntervalAddBIT::IntervalSum( const int& i_start , const int& i_final ) { return InitialSegmentSum( i_final ) - InitialSegmentSum( i_start - 1 ); } int main() { UNTIE; constexpr const int bound_N = 100000; CIN_ASSERT( N , 1 , bound_N ); constexpr const ll bound_K = 1000000000; CIN_ASSERT( K , 1 , bound_K ); constexpr const ll bound_Ri = 200000; Span S[bound_N] = {}; FOR( i , 0 , N ){ Span& Si = S[i]; cin >> Si.m_Li >> Si.m_Ri; assert( 1 <= Si.m_Li && Si.m_Li < Si.m_Ri && Si.m_Ri <= bound_Ri ); } constexpr const ll P = 998244353; int N_copy = N; POWER( total , K , N_copy , P ); sort( S , S + N , Ord() ); ll comp = 1; IntervalAddBIT mult{}; FOR( i , 0 , N ){ Span& Si = S[i]; comp = ( comp * ( K - mult.IntervalSum( Si.m_Li , Si.m_Li ) ) ) % P; mult.IntervalAdd( Si.m_Li , Si.m_Ri - 1 , 1 ); } RETURN( ( total + P - comp ) % P ); }