Chayas

시간 제한8초메모리 제한1024 MB

요약
b가 a와 c 사이에 있다는 m개의 조건을 모두 만족하는 chaya 순열의 개수를 998244353으로 나눈 나머지로 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 비트 연산, 조합론
정답자
아직 제출이 없습니다

문제

Once upon a time, there were a number of chayas (teahouses) along one side of an east-west road in Yokohama. Although the total number of chayas is known, the information about their locations was considered to be lost totally.

Recently, a document describing the old townscapes of Yokohama has been found. The document contains a number of records on the order of the locations of chayas. Each record has information below on the order of the locations of three chayas, say aa, bb, and cc.

Chaya bb was located between chayas aa and cc. Note that there may have been other chayas between aa and bb, or between bb and cc. Also, note that chaya aa may have been located east of cc or west of cc.

We want to know how many different orders of chayas along the road are consistent with all of these records in the recently found document. Note that, as the records may have some errors, there might exist no orders consistent with the records.

입력

The input consists of a single test case given in the following format.

nn mm

a_1a\_1 b_1b\_1 c_1c\_1

⋮\vdots

a_ma\_m b_mb\_m c_mc\_m

Here, nn represents the number of chayas and mm represents the number of records in the recently found document. 3≤n≤243 ≤ n ≤ 24 and 1≤m≤n×(n−1)×(n−2)/21 ≤ m ≤ n \times (n - 1) \times (n - 2)/2 hold. The chayas are numbered from 11 to nn.

Each of the following mm lines represents a record. The ii-th of them contains three distinct integers a_ia\_i, b_ib\_i, and c_ic\_i, each between 11 and nn, inclusive. This says that chaya b_ib\_i was located between chayas a_ia\_i and c_ic\_i. No two records have the same information, that is, for any two different integers ii and jj, the triple (a_i,b_i,c_i)(a\_i , b\_i , c\_i) is not equal to (a_j,b_j,c_j)(a\_j , b\_j , c\_j ) nor (c_j,b_j,a_j)(c\_j , b\_j , a\_j ).

출력

Output the number of different orders of the chayas, from east to west, consistent with all of the records modulo 998,244,353998\\, 244\\, 353 in a line. Note that 998,244,353=223×7×17+1998\\, 244\\, 353 = 2^{23} \times 7 \times 17 + 1 is a prime number.

힌트

For Sample Input 1, four orders, (1,5,3,2,4)(1, 5, 3, 2, 4), (4,2,3,1,5)(4, 2, 3, 1, 5), (4,2,3,5,1)(4, 2, 3, 5, 1), and (5,1,3,2,4)(5, 1, 3, 2, 4), are consistent with the records.

For Sample Input 2, there are no consistent orders.

예제2

  1. 예제 1

    입력
    5 4
    1 2 4
    2 3 5
    3 2 4
    1 3 2
    
    예상 출력
    4
    
  2. 예제 2

    입력
    4 2
    3 1 4
    1 4 3
    
    예상 출력
    0