loop do
    exp = gets.chomp
    break if exp =~ /^#/
    while exp =~ /S|>/
        exp.gsub!(/\d+\s*>>\s*\d+/){|shift| eval(shift).to_s}
        exp.gsub!(/S\s*<\s*\d+\s*>/){|square| square =~ /\d+/; ($&.to_i ** 2 % 1_000_000_007).to_s}
    end
    puts exp
end