#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
typedef long long int lli;
const lli mod = 1e9+7;

lli read_term(int &p, string &s);
lli read_expr(int &p, string &s);

lli read_term(int &p, string &s){
    if(s[p] == 'S'){
        p += 2;
        lli ret = read_expr(p, s);
        p++;
        return ret*ret %mod;
    }
    lli res = 0;
    while(p<(int)s.length() and '0'<=s[p] and s[p]<='9'){
        res *= 10;
        res += s[p]-'0';
        res %= mod;
        p++;
    }
    return res;
}

lli read_expr(int &p, string &s){
    lli res = read_term(p, s);
    while(p<(int)s.length() and s[p]!='>'){
        assert(s[p] == '#');
        p++;
        lli ret = read_term(p, s);
        res >>= min(ret, 32LL);
    }
    return res;
}

lli parse(string s){
    string tmp = "";
    for(char c: s){
        if(c != ' '){
            tmp += c;
        }
    }
    s = "";
    bool is_followed_by_num = false;
    for(int i=tmp.length()-1; i>=0; i--){
        if(tmp[i] == '>' and is_followed_by_num){
            s += '#';
            i--;
            is_followed_by_num = false;
        }else{
            s += tmp[i];
            if(('0'<=tmp[i] and tmp[i]<='9') or tmp[i]=='S'){
                is_followed_by_num = true;
            }
        }
    }
    reverse(s.begin(), s.end());
    //cerr << s << endl;
    int p = 0;
    return read_expr(p, s);
}

int main(){
    while(1){
        string s;
        getline(cin, s);
        if(s == "#") break;
        cout << parse(s) << endl;
    }
    return 0;
}
