Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
L = []
for _ in range(K):
  a, b = map(int, input().split())
  L.append((a,b))
L.sort()
DP = [0]*N
DP[0]=1
for i in range(1, N):
  for l in L:
    for j in range(l[0], l[1]+1):
      if i>=j: 
        DP[i] += DP[i-j]
        DP[i] %= 998244353
      else:
        break
    else:
      continue
    break
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
L = []
for _ in range(K):
  a, b = map(int, input().split())
  L.append((a,b))
L.sort()
DP_1 = [0]*N
DP_2 = [0]*(N+1)
DP_1[0], DP_2[1] = 1, 1
mod = 998244353
for i in range(1, N):
  for l in L:
    if l[0]>i:
      break
    elif l[1]>i:
      DP_1[i] += DP_2[i-l[0]+1]
      DP_1[i] %= mod
    elif l[1]<=i:
      DP_1[i] += DP_2[i-l[0]+1]-DP_2[i-l[1]]
      DP_1[i] %= mod
  DP_2[i+1] = DP_2[i] + DP_1[i]
  DP_2[i+1] %= mod
print(DP_1[-1]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
N, K  = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(K)]
L.sort()
S = set()
for l in L:
  S = S|set([i for i in range(l[0], l[1]+1)])
DP = [0]*N
DP[0]=1
for i in range(1, N):
  for j in S:
    if i>=j:
      DP[i] += DP[i-j]
      DP[i] %= 998244353
    else:
      break
  continue
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
L = []
for _ in range(K):
  a, b = map(int, input().split())
  L.append((a,b))
L.sort()
DP_1 = [0]*N
DP_2 = [0]*(N+1)
DP_1[0], DP_2[1] = 1, 1
mod = 998244353
for i in range(1, N):
  for l in L:
    if l[0]>i:
      break
    elif l[1]>i:
      DP_1[i] += DP_2[i-l[0]+1]
      DP_1[i] %= mod
    elif l[1]<=i:
      DP_1[i] += DP_2[i-l[0]+1]-DP_2[i-l[1]]
      DP_1[i] %= mod
  DP_2[i+1] = DP_2[i] + DP_1[i]
  DP_2[i+1] %= mod
print(DP_1[-1]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
N, K  = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(K)]
S = set()
for l in L:
  S = S|set([i for i in range(l[0], l[1]+1)])
DP = [0]*N
DP[0]=1
for i in range(1, N):
  for j in S:
    if i>=j:
      DP[i] += DP[i-j]
      DP[i] %= 998244353
    else:
      break
  continue
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
L = []
for _ in range(K):
  a, b = map(int, input().split())
  L.append((a,b))
L.sort()
DP_1 = [0]*N
DP_2 = [0]*(N+1)
DP_1[0], DP_2[1] = 1, 1
mod = 998244353
for i in range(1, N):
  for l in L:
    if l[0]>i:
      break
    elif l[1]>i:
      DP_1[i] += DP_2[i-l[0]+1]
      DP_1[i] %= mod
    elif l[1]<=i:
      DP_1[i] += DP_2[i-l[0]+1]-DP_2[i-l[1]]
      DP_1[i] %= mod
  DP_2[i+1] = DP_2[i] + DP_1[i]
  DP_2[i+1] %= mod
print(DP_1[-1]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
N, K  = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(K)]
S = set()
for l in L:
  S = S|set([i for i in range(l[0], l[1]+1)])
DP = [0]*N
DP[0]=1
for i in range(1, N):
  for j in S:
    if i>=j:
      DP[i] += DP[i-j]
      DP[i] %= 998244353
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
L = []
for _ in range(K):
  a, b = map(int, input().split())
  L.append((a,b))
L.sort()
DP_1 = [0]*N
DP_2 = [0]*(N+1)
DP_1[0], DP_2[1] = 1, 1
mod = 998244353
for i in range(1, N):
  for l in L:
    if l[0]>i:
      break
    elif l[1]>i:
      DP_1[i] += DP_2[i-l[0]+1]
      DP_1[i] %= mod
    elif l[1]<=i:
      DP_1[i] += DP_2[i-l[0]+1]-DP_2[i-l[1]]
      DP_1[i] %= mod
  DP_2[i+1] = DP_2[i] + DP_1[i]
  DP_2[i+1] %= mod
print(DP_1[-1]%mod)