petya collect beautiful matrixa matrix size n time n beautiful all element matrix integer 1 n for row matrix element row different for pair vertically adjacent element element different Today Petya buy beautiful matrix size n time n want determine raritythe rarity matrix index list beautiful matrix size n time n sort lexicographical order Matrix comparison row row the index lexicographically smallest matrix zerosince number beautiful matrix huge Petya want calculate rarity matrix modulo 998244353