import bisect MOD = 998244353 def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 Q = int(data[idx]) idx += 1 A = list(map(int, data[idx:idx+N])) idx += N A.sort() # Compute prefix products prefix_product = [1] * (N + 1) for i in range(N): prefix_product[i+1] = prefix_product[i] * A[i] % MOD # Precompute DP table for elementary symmetric sums max_n = N dp = [[0] * (max_n + 2) for _ in range(max_n + 2)] # +2 to avoid index issues # Initialize dp for k = N (empty suffix) dp[N][0] = 1 for d in range(1, max_n + 1): dp[N][d] = 0 for k in range(N-1, -1, -1): current_val = (A[k] - 1) % MOD m = N - k # number of elements in the suffix starting at k dp[k][0] = 1 for d in range(1, m + 1): dp[k][d] = dp[k+1][d] if d - 1 >= 0: dp[k][d] = (dp[k][d] + dp[k+1][d-1] * current_val) % MOD # Degrees beyond m are zero, but initialized as 0 # Process queries output = [] for _ in range(Q): l = int(data[idx]) idx += 1 r = int(data[idx]) idx += 1 p = int(data[idx]) idx += 1 res = 0 for c in range(l, r + 1): k = bisect.bisect_left(A, c) product = prefix_product[k] m = N - k if p > m: cnt = 0 else: required_degree = m - p if required_degree < 0 or required_degree > m: cnt = 0 else: cnt = dp[k][required_degree] * product % MOD res ^= cnt res %= MOD output.append(res) print('\n'.join(map(str, output))) if __name__ == '__main__': main()