def exp(s, i)
	return nil if i >= s.size
	a = term(s, i) or return nil
	x, i = a
	ts = [x]
	while a = term(s, i+2)
		x, i = a
		ts << x
	end
	return [ts.inject(:>>), i]
end

def term(s, i)
	return nil if i >= s.size
	if s[i] == ?S
		return nil if i+1 >= s.size || s[i+1] != ?<
		a = exp(s, i+2) or return nil
		x, i = a
		return nil if i >= s.size || s[i] != ?>
		return [x ** 2 % 1_000_000_007, i+1]
	elsif s[i] =~ /\d/
		j = i + 1
		j += 1 while j < s.size && s[j] =~ /\d/
		x = s[i..j-1].to_i
		return [x, j]
	else 
		return nil
	end
end

loop do
	e = gets.chomp
	break if e =~ /#/
	e.gsub!(/\s/, "")
	p exp(e, 0)[0]
end