"""
https://www.lanqiao.cn/problems/3382/learning/?page=1&first_category_id=1&name=%E5%8C%BA%E9%97%B4%E6%AC%A1%E6%96%B9%E5%92%8C
"""
import os
import sys
from itertools import accumulate
# 请在此输入您的代码
n, m = map(int, input().split())
a = list(map(int, input().split()))
mod = 1000000007
# 预处理数组的前缀和
def get_presum(a):
the_sum = list(accumulate(a))
the_sum = [x % mod for x in the_sum]
return the_sum
# 区间[l, r]中数的和
def get_sum(the_sum, l, r):
if l == 0:
return the_sum[r]
else:
return (the_sum[r] - the_sum[l - 1]) % mod
sum_list = []
for k in range(1, 6):
sum_list.append(get_presum([x ** k for x in a]))
for i in range(m):
l, r, k = map(int, input().split())
print(get_sum(sum_list[k - 1], l - 1, r - 1))