import sys input = sys.stdin.readline def nec(X,k): ko=k Y=[] for i in range(len(X)): a,b=X[i] if i+1=x: return a+(x-1) else: x-=b def kai(X,k,m): if k%2==1: X=nec(X,m) k-=1 Y=nec(X,m) necfirst=Y[-1][0]+Y[-1][1] first=X[0][0] Y=[] for a,b in X: Y.append([a+(necfirst-first),b]) X=X+Y return X[k//2:k//2+len(X)//2] mod=998244353 N,Q=list(map(int,input().split())) A=list(map(int,input().split())) A=sorted(set(A)) X=[] for a in A: if X==[] or X[-1][0]+X[-1][1]!=a: X.append([a,1]) else: X[-1][1]+=1 #print(X) plus=0 for tests in range(Q): k,x,m=list(map(int,input().split())) if k<=5: for tt in range(k): X=nec(X,m) print((what(X,x)+plus)%mod) else: #print("!",X) Y=nec(X,m) Z=nec(Y,m) k-=1 while len(Y)!=len(Z): Y=Z Z=nec(Y,m) k-=1 #print(Y) #print(Z) LEN=len(Y)+len(Z) a,b=Z[-1] necfirst=a+b first=Y[0][0] shuuki=necfirst-first rep=k//(LEN) k%=(LEN) plus=(plus+shuuki*rep)%mod Y=kai(Y,k,m) #print("!",Y1) #for tt in range(k): # Y=nec(Y,m) #print("?",Y) #assert Y==Y1 X=Y if X[0][0]>=mod: k=X[0][0]//mod for i in range(len(X)): X[i][0]-=k*mod print((what(X,x)+plus)%mod)