using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Diagnostics; using System.Numerics; using Enu = System.Linq.Enumerable; public class Program { Dictionary dic = new Dictionary(); static readonly long Mod0 = (long)1e9 + 7, Mod1 = (long)1e9 + 9; int N; RangeCoefAddSumSegTree seg1, seg2; public void Solve() { N = Reader.Int(); int NQ = Reader.Int(); var Q = Reader.StringTable(NQ); Init(); dic[0] = 0; var ans = new List(); for (int qi = 0; qi < NQ; qi++) if (Q[qi][0] == "!") Add(int.Parse(Q[qi][1]), int.Parse(Q[qi][2]), int.Parse(Q[qi][3]), qi + 1); else ans.Add(dic[Key()]); Console.WriteLine(string.Join("\n", ans)); } void Init() { var random = new Random(); var C = new int[N]; long mult = random.Next((int)1e7 + 1, (int)Mod0); for (long i = 0, m = 1; i < N; i++, m = m * mult % Mod0) C[i] = (int)m; seg1 = new RangeCoefAddSumSegTree(N, C, (int)Mod0); mult = random.Next((int)1e7 + 1, (int)Mod1); for (long i = 0, m = 1; i < N; i++, m = m * mult % Mod1) C[i] = (int)m; seg2 = new RangeCoefAddSumSegTree(N, C, (int)Mod1); } long Key() { return seg1.Sum() << 32 | seg2.Sum(); } void Add(int L, int R, int val, int time) { seg1.Add(L, R, val); seg2.Add(L, R, val); long key = Key(); if (!dic.ContainsKey(key)) dic[key] = time; } } class RangeCoefAddSumSegTree { public readonly int N, Mod; private readonly long[] V, S, Mult; public RangeCoefAddSumSegTree(int n, int[] mult, int mod) { for (N = 2; N < n; ) N *= 2; Mod = mod; V = new long[N * 2]; S = new long[N * 2]; Mult = new long[N + 1]; for (int i = 0; i < mult.Length; i++) Mult[i + 1] = (Mult[i] + mult[i]) % Mod; } public long Sum(int L = 0, int R = int.MaxValue) { return Sum(0, 0, N, L, R); } private long Sum(int i, int currL, int currR, int L, int R) { if (currL >= R || currR <= L) return 0; if (currL >= L && currR <= R) return ((S[i] + V[i] * (Mod + Mult[currR] - Mult[currL])) % Mod + Mod) % Mod; long res = V[i] * (Mod + Mult[Math.Min(currR, R)] - Mult[Math.Max(currL, L)]) % Mod; int mid = (currL + currR) >> 1; res = (res + Sum(i * 2 + 1, currL, mid, L, R)) % Mod; res = (res + Sum(i * 2 + 2, mid, currR, L, R)) % Mod; return res; } public void Add(int L, int R, long val) { Add(0, val, 0, N, L, R); } private void Add(int i, long val, int currL, int currR, int L, int R) { if (currL >= R || currR <= L) return; if (currL >= L && currR <= R) { V[i] = (V[i] + val) % Mod; return; } S[i] = (S[i] + val * (Mod + Mult[Math.Min(currR, R)] - Mult[Math.Max(currL, L)])) % Mod; int mid = currL + currR >> 1; Add(i * 2 + 1, val, currL, mid, L, R); Add(i * 2 + 2, val, mid, currR, L, R); } } class Entry { static void Main() { new Program().Solve(); } } class Reader { static TextReader reader = Console.In; static readonly char[] separator = { ' ' }; static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; static string[] A = new string[0]; static int i; static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Range(N, Int); } public static int[][] IntTable(int H) { return Range(H, IntLine); } public static string[] StringArray(int N) { return Range(N, Next); } public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); } public static string Line() { return reader.ReadLine().Trim(); } public static T[] Range(int N, Func f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; } static string[] Split(string s) { return s.Split(separator, op); } static string Next() { CheckNext(); return A[i++]; } static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }