Problem: Expression Mining

Consider an arithmetic expression built by combining single-digit positive integers with addition symbols `+`, multiplication symbols `*`, and parentheses `(` `)`, defined by the following grammar rules with the start symbol `E`.


E ::= T | E '+' T
T ::= F | T '*' F
F ::= '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '(' E ')'


When such an arithmetic expression is viewed as a string, its substring, that is, a contiguous sequence of characters within the string, may again form an arithmetic expression. Given an integer n and a string s representing an arithmetic expression, let us count the number of its substrings that can be read as arithmetic expressions with values computed equal to n.

Input

The input consists of multiple datasets, each in the following format.

> n
>  s
>

A dataset consists of two lines. In the first line, the target value n is given. n is an integer satisfying 1 ≤ n ≤ 109. The string s given in the second line is an arithmetic expression conforming to the grammar defined above. The length of s does not exceed 2×106. The nesting depth of the parentheses in the string is at most 1000.

The end of the input is indicated by a line containing a single zero. The sum of the lengths of s in all the datasets does not exceed 5×106.

Output

For each dataset, output in one line the number of substrings of s that conform to the above grammar and have the value n. The same sequence of characters appearing at different positions should be counted separately.

Sample Input


3
(1+2)*3+3
2
1*1*1+1*1*1
587
1*(2*3*4)+5+((6+7*8))*(9)
0


Output for the Sample Input


4
9
2






Example

Input

3
(1+2)*3+3
2
1*1*1+1*1*1
587
1*(2*3*4)+5+((6+7*8))*(9)
0


Output

4
9
2

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
using Int = signed;
using ll = long long;

Int extgcd(Int a,Int b,Int &x, Int&y){
  Int d = a;
  if(b!=0){
    d = extgcd(b, a%b, y, x);
    y -= (a/b) * x;
  } else {
    x = 1; y = 0;
  }
  return d;
}

Int mod_inverse(Int a,Int mod){
  Int x, y;
  extgcd(a, mod, x, y);
  return (mod+x%mod)%mod;
}

using V = tuple<Int, Int, Int>;
using T = long long;
const Int MOD1 = 1e9 + 7;
const Int MOD2 = 1e9 + 9;
const Int MOD3 = 1e8 + 7;

inline T add1(T a, T b){
  a += b;
  if(a >= MOD1) a -= MOD1;
  return a;
}

inline T add2(T a, T b){
  a += b;
  if(a >= MOD2) a -= MOD2;
  return a;
}

inline T add3(T a,T b){
  a += b;
  if(a >= MOD3) a -=MOD3;
  return a;
}

inline T sub1(T a, T b){
  return add1(a, MOD1 - b);
}

inline T sub2(T a, T b){
  return add2(a, MOD2 - b);
}

inline T sub3(T a, T b){
  return add3(a, MOD3 - b);
}

inline T mul1(T a,T b){
  return (1LL * a * b) % MOD1;
}

inline T mul2(T a,T b){
  return (1LL * a * b) % MOD2;
}

inline T mul3(T a,T b){
  return (1LL * a * b) % MOD3;
}

T inv1(T a){
  return mod_inverse(a,MOD1);
}
T inv2(T a){
  return mod_inverse(a,MOD2);
}
T inv3(T a){
  return mod_inverse(a,MOD3);
}

V inv(const V &a){
  T a1, a2,a3;
  tie(a1,a2,a3) = a;
  T r1 = inv1(a1);
  T r2 = inv2(a2);
  T r3 = inv3(a3);
  return V(r1, r2, r3);
}

V add(const V &a, const V &b){
  T a1, a2,a3, b1,b2,b3;
  tie(a1,a2,a3) = a;
  tie(b1,b2,b3) = b;
  T r1 = add1(a1, b1);
  T r2 = add2(a2, b2);
  T r3 = add3(a3, b3);
  return V(r1, r2, r3);
}

V sub(const V &a, const V &b){
  T a1, a2,a3, b1,b2,b3;
  tie(a1,a2,a3) = a;
  tie(b1,b2,b3) = b;
  T r1 = sub1(a1, b1);
  T r2 = sub2(a2, b2);
  T r3 = sub3(a3, b3);
  return V(r1, r2, r3);
}

V mul(const V &a, const V &b){
  T a1, a2, a3, b1, b2, b3;
  tie(a1, a2, a3) = a;
  tie(b1, b2, b3) = b;
  T r1 = mul1(a1, b1);
  T r2 = mul2(a2, b2);
  T r3 = mul3(a3, b3);
  return V(r1, r2, r3);
}


vector<vector<V> > vs;
vector<vector<vector<V> > > vm;

V expr(string &s,Int &p,Int d);
V term(string &s,Int &p,Int d);
V factor(string &s,Int &p,Int d);
V number(string &s,Int &p);

V expr(string &s,Int &p,Int d){
  V res=term(s,p,d);
  vs[d].emplace_back(res);
  while(p<(Int)s.size()){
    if(s[p]=='+'){
      p++;
      V b=term(s,p,d);
      vs[d].emplace_back(b);
      res=add(res,b);
      continue;
    }
    break;
  }
  vs[d].emplace_back(V(0,0,0));
  vm[d].emplace_back();
  return res;
}
V term(string &s,Int &p,Int d){
  V res=factor(s,p,d);
  vm[d].emplace_back();
  vm[d].back().emplace_back(res);
  while(p<(Int)s.size()){
    if(s[p]=='*'){
      p++;
      V b=factor(s,p,d);
      vm[d].back().emplace_back(b);
      res=mul(res,b);
      continue;
    }
    break;
  }
  return res;
}
V factor(string &s,Int &p,Int d){
  if(s[p]=='('){
    p++;
    V res=expr(s,p,d+1);
    assert(s[p]==')');
    p++;
    return res;
  }
  return number(s,p);
}

V number(string &s,Int &p){
  Int k=s[p++]-'0';
  return V(k,k,k);
}


signed main(){
  Int n;
  while(cin>>n,n){
    string s;
    cin>>s;   
    vs.assign(1010,vector<V>());
    vm.assign(1010,vector<vector<V> >());
    
    V nv(n%MOD1,n%MOD2,n%MOD3);
    Int p=0;
    expr(s,p,0);
    ll ans=0;
    
    if(0){
      for(Int i=0;i<3;i++){
	for(Int j=0;j<(Int)vs[i].size();j++) cout<<get<0>(vs[i][j])<<" ";
	cout<<endl;
	for(Int j=0;j<(Int)vm[i].size();j++){
	  cout<<i<<" "<<j<<":"<<endl;
	  for(Int k=0;k<(Int)vm[i][j].size();k++)
	    cout<<get<0>(vm[i][j][k])<<" ";
	  cout<<endl;
	}
	cout<<endl;
      }
    }

    for(Int i=0;i<1010;i++){
      auto &v=vs[i];
      if(v.empty()) continue;
      map<V, ll> dp;
      V uku(0,0,0);
      dp[uku]++;
      for(Int j=0;j<(Int)v.size();j++){
	//cout<<i<<" "<<j<<":"<<get<0>(v[j])<<endl;
	if(v[j]==V(0,0,0)){
	  dp.clear();
	  uku=V(0,0,0);
	  dp[uku]++;
	  continue;
	}
	V malta(1,1,1);
	for(Int k=0;k<(Int)vm[i][j].size();k++){
	  malta=mul(malta,vm[i][j][k]);
	  ans+=dp[sub(add(uku,malta),nv)];
	}
	
	map<V, ll> dp2;
	malta=V(1,1,1);
	dp2[mul(malta,nv)]++;
	for(Int k=1;k<(Int)vm[i][j].size();k++){
	  malta=mul(malta,vm[i][j][k]);
	  ans+=dp2[malta];	  
	  dp2[mul(malta,nv)]++;
	}
	
	uku=add(uku,v[j]);
	malta=V(1,1,1);
	for(Int k=0;k+1<(Int)vm[i][j].size();k++){
	  malta=mul(malta,vm[i][j][k]);
	  dp[sub(uku,mul(v[j],inv(malta)))]++;
	}
	
	dp[uku]++;
      }
    }

    cout<<ans<<endl;
  }
  return 0;
}