import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {
  import scala.io.StdIn._
  loop()
  @tailrec def loop(): Unit = {
    val str = readLine.trim
    if (str == "#") return
    str match {
      case Expression(e) ⇒ println(e)
      case _ ⇒ ???
    }
    loop()
  }
}
object Expression {
  val mod = 1000000007L

  def unapply(arg: String): Option[Long] = {
    arg.filterNot(_.isWhitespace).toList match {
      case e Expr Nil ⇒ Some(e)
      case _ ⇒ None
    }
  }

  object Number {
    def unapply(arg: List[Char]): Option[(Long, List[Char])] = {
      arg.span(_.isDigit) match {
        case (Nil, _) ⇒ None
        case (n, rest) ⇒ Some(n.foldLeft(0L){case (acc, d) ⇒ acc * 10 + d.asDigit}, rest)
      }
    }
  }
  object Term {
    def unapply(arg: List[Char]): Option[(Long, List[Char])] = {
      arg match {
        case n Number rest ⇒ Some(n, rest)
        case 'S'::'<'::(e Expr '>'::rest) ⇒ Some((e * e) % mod, rest)
        case _ ⇒ None
      }
    }
  }
  object Expr {
    def unapply(arg: List[Char]): Option[(Long, List[Char])] = {
      arg match {
        case t Term rest ⇒ Some(takeAll(rest, t))
        case _ ⇒ None
      }
    }
    @tailrec private[this] def takeAll(arg: List[Char], left: Long): (Long, List[Char]) = {
      arg match {
        case '>'::'>'::(right Term rest) ⇒ takeAll(rest, left >> math.min(63, right))
        case _ ⇒ (left, arg)
      }
    }
  }
}
