Yuanfang is puzzled with the question below: 
There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations. 
Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<---a k+c, k = x,x+1,…,y. 
Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<---a k×c, k = x,x+1,…,y. 
Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<---c, k = x,x+1,…,y. 
Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y p. 
Yuanfang has no idea of how to do it. So he wants to ask you to help him. 

Input

There are no more than 10 test cases. 
For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000. 
Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3) 
The input ends with 0 0. 

Output

For each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.

Sample Input

5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0

Sample Output

307
7489

       三个lacy数组分别处理add,mul以及change的情况,三个sum数组分别表示区间每个数的1,2,3次方和是多少~。

#include<bits/stdc++.h>
using namespace std;
#define mod  10007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
const int maxn = 100010;
long long mul[maxn * 4], add[maxn * 4], sum1[maxn * 4], sum2[maxn * 4], sum3[maxn * 4], change[maxn * 4];
void pushup(int rt) {
	sum1[rt] = (sum1[rt << 1] + sum1[rt << 1 | 1]) % mod;
	sum2[rt] = (sum2[rt << 1] + sum2[rt << 1 | 1]) % mod;
	sum3[rt] = (sum3[rt << 1] + sum3[rt << 1 | 1]) % mod;
}
void pushdown(int l, int r, int rt) {
	ll len = r - l + 1; ll llen = (len + 1) / 2, rlen = (len) / 2;
	if (change[rt] != 0) {
		mul[rt << 1] = 1; mul[rt << 1 | 1] = 1;
		add[rt << 1] = 0; add[rt << 1 | 1] = 0;
		sum1[rt << 1] = (change[rt] * llen) % mod; sum1[rt << 1 | 1] = (change[rt] * rlen) % mod;
		sum2[rt << 1] = (change[rt] * change[rt] * llen) % mod; sum2[rt << 1 | 1] = (change[rt] * change[rt] * rlen) % mod;
		sum3[rt << 1] = (change[rt] * change[rt] * change[rt] * llen) % mod; sum3[rt << 1 | 1] = (change[rt] * change[rt] * change[rt] * rlen) % mod;
		change[rt << 1] = change[rt << 1 | 1] = change[rt];
		change[rt] = 0;
	}
	if (mul[rt] != 1) {
		mul[rt << 1] = (mul[rt << 1] * mul[rt]) % mod; mul[rt << 1 | 1] = (mul[rt << 1 | 1] * mul[rt]) % mod;
		add[rt << 1] = (add[rt << 1] * mul[rt]) % mod; add[rt << 1 | 1] = (add[rt << 1 | 1] * mul[rt]) % mod;
		sum1[rt << 1] = (sum1[rt << 1] * mul[rt]) % mod; sum1[rt << 1 | 1] = (sum1[rt << 1 | 1] * mul[rt]) % mod;
		sum2[rt << 1] = (sum2[rt << 1] * mul[rt] * mul[rt]) % mod; sum2[rt << 1 | 1] = (sum2[rt << 1 | 1] * mul[rt] * mul[rt]) % mod;
		sum3[rt << 1] = (sum3[rt << 1] * mul[rt] * mul[rt] * mul[rt]) % mod; sum3[rt << 1 | 1] = (sum3[rt << 1 | 1] * mul[rt] * mul[rt] * mul[rt]) % mod;
		mul[rt] = 1;
	}
	if (add[rt] != 0) {
		//	cout << l << " " << r << " " << add[rt] << " ";cout << sum2[rt << 1] << " " << sum2[rt << 1 | 1] << "\n";
		ll a = sum1[rt << 1], b = sum1[rt << 1 | 1], c = sum2[rt << 1], d = sum2[rt << 1 | 1];
		add[rt << 1] = (add[rt << 1] + add[rt]) % mod; add[rt << 1 | 1] = (add[rt << 1 | 1] + add[rt]) % mod;
		sum1[rt << 1] = (sum1[rt << 1] + add[rt] * llen) % mod; sum1[rt << 1 | 1] = (sum1[rt << 1 | 1] + add[rt] * rlen) % mod;
		sum2[rt << 1] = (sum2[rt << 1] + add[rt] * add[rt] * llen + 2 * a*add[rt]) % mod; sum2[rt << 1 | 1] = (sum2[rt << 1 | 1] + add[rt] * add[rt] * rlen + 2 * b*add[rt]) % mod;
		sum3[rt << 1] = (sum3[rt << 1] + 3 * c*add[rt] + 3 * a*add[rt] * add[rt] + llen*add[rt] * add[rt] * add[rt]) % mod; sum3[rt << 1 | 1] = (sum3[rt << 1 | 1] + 3 * d*add[rt] + 3 * b*add[rt] * add[rt] + rlen*add[rt] * add[rt] * add[rt]) % mod;

		add[rt] = 0;
	}
}
void build(int l, int r, int rt) {
	mul[rt] = 1; add[rt] = 0; change[rt] = 0;
	sum1[rt] = sum2[rt] = sum3[rt] = 0;
	if (l == r) {
		return;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}
void updatemul(int L, int R, int l, int r, int rt, ll c) {
	if (L <= l&&r <= R) {
		//	cout << l << " " << r << " "<<sum1[rt]<<" "<<mul[rt]<<" "<<add[rt]<<" "<<change[rt]<<endl;
		ll len = r - l + 1;
		mul[rt] = (mul[rt] * c) % mod;
		add[rt] = (add[rt] * c) % mod;
		sum1[rt] = (sum1[rt] * c) % mod;
		sum2[rt] = (sum2[rt] * c*c) % mod;
		sum3[rt] = (sum3[rt] * c*c*c) % mod;
		return;
	}
	int m = (l + r) >> 1;
	pushdown(l, r, rt);
	if (L <= m)
		updatemul(L, R, lson, c);
	if (R > m)
		updatemul(L, R, rson, c);
	pushup(rt);
}
//
void updateadd(int L, int R, int l, int r, int rt, ll c) {
	//cout << l << " ho:" << r <<" "<<L<<" "<<R<< endl;
	if (L <= l&&r <= R) {
		ll len = r - l + 1;
		add[rt] = (add[rt] + c) % mod;
		sum3[rt] = (sum3[rt] + 3 * sum2[rt] * c + 3 * sum1[rt] * c * c + c * c * c *len) % mod;
		sum2[rt] = (sum2[rt] + len * c * c + 2 * c * sum1[rt]) % mod;
		sum1[rt] = (sum1[rt] + len * c) % mod;
		return;
	}
	int m = (l + r) >> 1;
	pushdown(l, r, rt);
	if (L <= m)
		updateadd(L, R, lson, c);
	if (R > m)
		updateadd(L, R, rson, c);
	pushup(rt);
}
void updatechange(int L, int R, int l, int r, int rt, ll c) {
	if (L <= l&&r <= R) {
		ll len = r - l + 1;
		mul[rt] = 1; add[rt] = 0; change[rt] = c;
		sum1[rt] = (len * c) % mod; sum2[rt] = (len *c*c) % mod; sum3[rt] = (len*c*c*c) % mod;
		return;
	}
	int m = (l + r) >> 1;
	pushdown(l, r, rt);
	if (L <= m)
		updatechange(L, R, lson, c);
	if (R > m)
		updatechange(L, R, rson, c);
	pushup(rt);
}
ll query(int L, int R, int l, int r, int rt, int c) {
	long long sum = 0;
	if (L <= l&&r <= R) {
		//	if(l==r)cout << l << " " << r << " " << sum1[rt] << " " << mul[rt] << " " << add[rt] << " " << change[rt] << endl;
		if (c == 1)return sum1[rt];
		else if (c == 2)return sum2[rt];
		else return sum3[rt];
	}
	int m = (l + r) >> 1;
	pushdown(l, r, rt);
	if (L <= m) {
		sum += query(L, R, lson, c);
	}
	if (R > m) {
		sum += query(L, R, rson, c);
	}
	sum %= mod;
	return sum;
}
int n, m;
int main() {
	ios::sync_with_stdio(0);
	while (cin >> n >> m&&n + m) {
		build(1, n, 1);
		while (m--) {
			//cout << m << endl;
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			if (a == 1) {
				updateadd(b, c, 1, n, 1, 1LL * d);
			}
			else if (a == 2) {
				updatemul(b, c, 1, n, 1, 1LL * d);
			}
			else if (a == 3) {
				updatechange(b, c, 1, n, 1, 1LL * d);
			}
			else {
				cout << query(b, c, 1, n, 1, d) << "\n";
			}
		}
	}
	return 0;
}