(loop with N = (read) for i from 2 to N 
      with lst = (loop repeat (read) collect (cons (read) (read)))
      and ht = (make-hash-table)
      and divisor = 998244353 and diff
      initially (setf (gethash 1 ht) 1)
      do (setf diff (loop for x in lst
                          sum (- (or (gethash (- i (car x)) ht) 0)
                                 (or (gethash (- i (cdr x) 1) ht) 0)))
               (gethash i ht) (mod (+ (gethash (1- i) ht) diff) divisor))
      finally (princ diff))