def eval_shifts(s)
    s.split(">>").map(&:to_i).inject(:>>)
end

loop.with_index do |_, i|
    e = gets.chomp
    break if e =~ /#/ 
    e.gsub!(/\s/, "") 
    while i = e.rindex(?S)
        e[i] = ?T
        e.sub!(/T<\d+(>>\d+)*>/){|s| (eval_shifts(s[2..-2]) ** 2 % 1_000_000_007).to_s}
    end 
    p eval_shifts(e)
end