#include <bits/stdc++.h>
using namespace std;

constexpr int64_t mod = 1000000007;
char *code;

bool consume(char c){
    if(*code != c) return false;
    ++code;
    return true;
}

int64_t expr();
int64_t term();
int64_t num();

int64_t expr(){
    int64_t value = term();
    while(true){
        if(
            strncmp(code, ">>", 2) == 0
        &&  ( *(code+2) == 'S' || isdigit(*(code+2)) )
        ){
            code += 2;
            int64_t rhs = term();
            if(rhs >= 64) value = 0;
            else value >>= rhs;
        }else break;
    }
    return value;
}

int64_t term(){
    if(consume('S')){
        assert(consume('<'));
        int64_t value = expr();
        assert(consume('>'));
        return value * value % mod;
    }else return num();
}

int64_t num(){
    int64_t value = 0;
    while(isdigit(*code)){
        value = value * 10 + (*code - '0');
        ++code;
    }
    return value;
}

int main(){
    string s;
    while(true){
        getline(cin, s);
        if(s[0] == '#') break;
        code = new char[s.size()+1];
        size_t j = 0;
        for(size_t i=0;i<s.size();++i){
            if(s[i] != ' ') code[j++] = s[i];
        }
        code[j] = '\0';
        cout << expr() << endl;
    }
    
    return 0;
}
