rt
code:
#include<bits/stdc++.h>
#define int long long
#define PII pair<int,int>
using namespace std;
const int mod=10007;
string x,y;
bool flag;
int n;
stack<PII >sta1;
stack<char>sta2;
PII solve(PII x,PII y,char z)
{
PII res;
if(z=='+')
{
int tmp=(x.first+x.second)*(y.first+y.second);
res.first=(x.first*y.first)%mod;
res.second=(tmp-res.first)%mod;
}
if(z=='*')
{
int tmp=(x.first+x.second)*(y.first+y.second);
res.second=(x.second*y.second)%mod;
res.first=(tmp-res.second)%mod;
}
return res;
}
signed main()
{
cin>>n>>x,x=' '+x+' ';
for(int i=1;i<=n;i++)
{
if(x[i]=='+'||x[i]=='*')
{
if(!flag)flag=true,y+='_';
y+=x[i];
if(x[i+1]!='(')y+='_';
else flag=false;
}
else y+=x[i];
}
n=y.size(),y=' '+y;
for(int i=1;i<=n;i++)
{
if(y[i]=='_')sta1.push(make_pair(1,1));
if(y[i]=='+')
{
if(sta2.empty()||sta2.top()=='+'||sta2.top()=='(')sta2.push('+');
else
{
PII t1=sta1.top(),t2;
sta1.pop(),t2=sta1.top();
sta1.pop(),sta1.push(solve(t1,t2,'*'));
sta2.pop(),sta2.push('+');
}
}
if(y[i]=='*'||y[i]=='(')sta2.push(y[i]);
if(y[i]==')')
{
while(sta2.top()!='(')
{
PII t1=sta1.top(),t2;
sta1.pop(),t2=sta1.top();
sta1.pop(),sta1.push(solve(t1,t2,sta2.top()));
sta2.pop();
}
sta2.pop();
}
}
while(!sta2.empty())
{
PII t1=sta1.top(),t2;
sta1.pop(),t2=sta1.top();
sta1.pop(),sta1.push(solve(t1,t2,sta2.top()));
sta2.pop();
}
cout<<sta1.top().first;
return 0;
}