import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } enum MO = 10L^^9 + 7; class SegmentTree { int n; long[] prod; this(long[] ini) { for (n = 1; n < ini.length; n <<= 1) {} prod = new long[n << 1]; foreach (i, val; ini) { prod[n + i] = val; } foreach_reverse (a; 1 .. n) { prod[a] = (prod[a << 1] * prod[a << 1 | 1]) % MO; } } void update(int a, long val) { prod[a += n] = val; for (a >>= 1; a; a >>= 1) { prod[a] = (prod[a << 1] * prod[a << 1 | 1]) % MO; } } long rangeProd(int a, int b) { long ret = 1; for (a += n, b += n; a <= b; a >>= 1, b >>= 1) { if ( a & 1) (ret *= prod[a++]) %= MO; if (~b & 1) (ret *= prod[b--]) %= MO; } return ret; } } int N; long[] C; string[] O; int Q; string[] T; int[] X, Y; void main() { try { for (; ; ) { N = readInt() / 2 + 1; C = new long[N]; O = new string[N + 1]; C[0] = readLong(); foreach (i; 1 .. N) { O[i] = readToken(); C[i] = readLong(); } O[0] = O[N] = "+"; Q = readInt(); T = new string[Q]; X = new int[Q]; Y = new int[Q]; foreach (q; 0 .. Q) { T[q] = readToken(); X[q] = readInt(); Y[q] = readInt(); } auto seg = new SegmentTree(C); auto pluses = new RedBlackTree!int(); foreach (i; 0 .. N + 1) { if (O[i] == "+") { pluses.insert(i); } } void updateOp(int i) { debug { writeln("updateOp ", i, " ", O[i]); } switch (O[i]) { case "+": { // "*" -> "+" pluses.insert(i); } break; case "*": { // "+" -> "*" pluses.removeKey(i); } break; default: assert(false); } } foreach (q; 0 .. Q) { switch (T[q]) { case "!": { if (X[q] % 2 == 0) { if (O[X[q] / 2] != O[Y[q] / 2]) { swap(O[X[q] / 2], O[Y[q] / 2]); updateOp(X[q] / 2); updateOp(Y[q] / 2); } } else { swap(C[X[q] / 2], C[Y[q] / 2]); seg.update(X[q] / 2, C[X[q] / 2]); seg.update(Y[q] / 2, C[Y[q] / 2]); } } break; case "?": { const l = pluses.upperBound(X[q] / 2).front; const r = pluses.lowerBound(Y[q] / 2 + 1).back; debug { writeln("? ", X[q] / 2, " ", Y[q] / 2); writeln(" l = ", l, ", r = ", r); } long ans; if (l <= r) { ans += seg.rangeProd(X[q] / 2, l - 1); if (l < r) { ans += seg.rangeProd(l, r - 1); } ans += seg.rangeProd(r, Y[q] / 2); } else { ans += seg.rangeProd(X[q] / 2, Y[q] / 2); } ans %= MO; writeln(ans); } break; default: assert(false); } debug { writeln("C = ", C); writeln("pluses = ", pluses); } } } } catch (EOFException e) { } }