N = gets.to_i Q = gets.to_i Query = Q.times.map{ q,*args = gets.split [q.intern] + args.map(&:to_i) } $x = Hash.new(0) $y = Hash.new(0) $a = {} $b = {} def a(i) return 1 if i == 0 # $a[i] ||= ($x[i-1] * b(i-1)**2 + a(i-1)) % (10**9+7) $a[i] ||= if $x[i-1] == 0 a(i-1) else $x[i-1] * b(i-1)**2 + a(i-1) end % (10**9+7) end def b(i) return 1 if i == 0 $b[i] ||= if $y[i-1] == 0 1 else b(i-1) + 1 end end Query.each{|q, *args| case q when :a puts a(args[0]) % (10**9+7) when :x $x[args[0]] = args[1] $a.clear when :y $y[args[0]] = args[1] $a.clear; $b.clear end }