#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<memory>
#include<stack>



using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000000;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
#define ptr shared_ptr
//class some;
/*class hai{
public:
	int siz;
	ptr<some> num;
	ptr<hai> hi,mg;
	//sizはその要素のサイズを表す
	//numはsizが1の時に入っている要素を表す
};*/

vector<string>codelines={
	"let next (@a (+ a 1))",
	"let prev (@a (- a 1))",
	
	"let ha (@x (/ x 2 ))",
	//"let ddde (ha 57)",
	//"woutn ddde",
	"let inclbit (@x @y "
		"if y ("
			"if ( > ( % x 2) ( % y 2 ) ) 0"
			"( inclbit ( ha x )( ha y ) )"
		") 1"
	")",
	
	"let stand (@x @y (% (/ x y) 2))",
	"winn L",
	"winn R",

	
	"let count (@x @y @fxb @fyb "
		"(% "
		"(if ( max fxb fyb )( "
			"if ( < fxb fyb ) ("
				"* ( - 2 ( stand x fyb ) )"
				"( count x y fxb (ha fyb) )"
			")("
				"if ( > fxb fyb ) "
				"( "
				 "* ( + 1 ( stand y fxb ) ) "
				 "( count x y (ha fxb) fyb  )"
				 ")"
				"(* 3 ( count x y (ha fxb) (ha fyb) ))"
			")"
		")( 1 ))"
		" 1000000007)"
	")",
	
	
	
	"let countin (@x @y @fxb @fyb "
		"if ("
			"(@aa (inclbit (/ x aa) (/ y aa)))"
			"(max (max (* fxb 2) (* fyb 2))  1)"
		") (count x y fxb fyb) 0"
		
	")",
	
	"let ikouone (@x @fb "
		"if fb ("
			"if (stand x fb)"
			"(ikouone x (ha fb))"
			"0"
		") 1"
	")",
	
	"let countR (@x @y @fxb @fyb "
		"if (ikouone y fyb) (countin x y fxb fyb)"
		"("
			"+"
			"(if (stand y fyb) (countin x (- y fyb) fxb (ha fyb)) 0)"
			"(countR x y fxb (ha fyb))"
		")"
	")",
	
	"let ikouzro (@x @fb "
		"if fb ("
			"if (stand x fb)"
			"0"
			"(ikouzro x (ha fb))"
		") 1"
	")",
	"let countLR (@x @y @fxb @fyb "
		"if (ikouzro x fxb) (countR x y fxb fyb)"
		"("
			"+"
			"(if (stand x fxb) 0 (countR (+ x fxb) y (ha fxb) fyb))"
			"(countLR x y (ha fxb) fyb)"
		")"
	")",
	
	"let solve (@x @y @now "
		"if (< y now) 0 ("
			"+("
				"if (< x (* now 2))"
				"(countLR (max x now) (min y (prev(* now 2))) (ha now) (ha now))"
				"(0)"
			")(solve x y (* 2 now))"
		")"
	")",
	
	"let ans (% (solve L R 1) 1000000007)",
	"woutn ans"
};


class some{
public:
	char enz;//どの演算を行いますか?
	ptr<some> hi,mg;llint ban;
	bool syu;
	bool ramukan;//ラムダの完結
	some(void){ramukan=false;syu=false;enz='Q';ban=0;}
	//syukuがtrueなら縮約済み!やった!
};
ptr<some> hairead(ptr<some> in,int soe){
	ptr<some> gen=in->hi;
	while(gen->enz=='H'){
		if(soe<gen->ban){gen=gen->hi;}
		else{soe-=gen->ban;gen=gen->mg;}
	}
	return gen;
}
ptr<some> haiwrite(ptr<some> in,int soe,ptr<some> suu){

	ptr<some> ata(new some());
	ptr<some> gen(new some());
	(*ata)=(*in);
	(*gen)=(*(in->hi));
	ata->hi=gen;
	if(gen->enz!='H'){ata->hi=suu;return ata;}
	while(-1){
		ptr<some>iti(new some());
		if(soe<gen->ban){
			if(gen->hi->enz!='H'){gen->hi=suu;return ata;}
			(*iti)=(*(gen->hi));
			gen->hi=iti;
			gen=iti;
		}else{
			if(gen->mg->enz!='H'){gen->mg=suu;return ata;}
			soe-=gen->ban;
			(*iti)=(*(gen->mg));
			gen->mg=iti;
			gen=iti;
		}
	}
};
ptr<some> sai_hai(int soe,ptr<some>suu){
	
	if(soe==1){return suu;}
	ptr<some>ans(new some());
	ans->enz='H';ans->ban=soe/2;
	ans->hi=sai_hai(soe/2,suu);
	ans->mg=sai_hai((soe+1)/2,suu);
	return ans;
}
ptr<some> make_hai(int soe,ptr<some> suu){
	ptr<some>ans(new some());
	ans->enz='h';ans->ban=soe;
	if(soe>0){ans->hi=sai_hai(soe,suu);}
	return ans;
}

ptr<some> unite_hai(ptr<some> v,ptr<some>w){
	if(v->ban==0){return w;}
	if(w->ban==0){return v;}
	ptr<some>ans(new some());
	ptr<some>tgi(new some());
	ans->enz='h';ans->ban=v->ban+w->ban;ans->hi=tgi;
	tgi->enz='H';tgi->ban=v->ban;tgi->hi=v->hi;tgi->mg=w->hi;
	return ans;
}
//let束縛

string str;
int hibas=0;
map<llint,ptr<some>>letnames;
vector<string>atnames;
const llint itmo=2.19e16+13;
llint roll(string in){
	//cerr<<in;
	llint ans=0;
	for(auto it:in){ans*=100;ans+=it;ans%=itmo;}
	//cerr<<" "<<ans<<endl;
	return ans;
}
bool vach(char c){return ('a'<=c&&c<='z')||('A'<=c&&c<='Z')||(c=='_');}
ptr<some> pasa(void){
	//cerr<<"hibas="<<hibas<<endl;
	int scopename=0;
	//ラムダのスコープを探している
	do{hibas++;}while(str[hibas]==' '||str[hibas]=='\t'||str[hibas]=='\n');
	ptr<some>kan(new some());
	while(str[hibas]=='@'){
		//ラムダ
		//kan->enz='@';
		do{hibas++;}while(str[hibas]==' ');
		string itna;
		while(vach(str[hibas])){
			itna.pub(str[hibas]);
			hibas++;
		}
		while(str[hibas]==' '){hibas++;}
		atnames.pub(itna);
		//cerr<<itna<<endl;
		scopename++;
		//kan->hi=ptr<some>(new some());
		//kan=kan->hi;
		//cerr<<"de";
	}
	//cerr<<"de";
	while(str[hibas]!=')'){
		ptr<some> gen;
		if(str[hibas]=='('){gen=pasa();}
		else{
			gen=ptr<some>(new some());
			//ここで大量の場合分け?
			string itna;
			while(str[hibas]!=' '&&str[hibas]!='('&&str[hibas]!=')'){
				itna.pub(str[hibas]);
				hibas++;
			}
			while(str[hibas]==' '){hibas++;}
			//ここで、各条件は絶対に被らない
			if('0'<=itna[0]&&itna[0]<='9'){
				gen->enz='n';gen->ban=0;
				for(auto it:itna){gen->ban*=10;gen->ban+=it-'0';}
			}else if(itna=="+"){gen->enz='+';}
			else if(itna=="-"){gen->enz='-';}
			else if(itna=="*"){gen->enz='*';}
			else if(itna=="/"){gen->enz='/';}
			else if(itna=="%"){gen->enz='%';}
			else if(itna=="max"){gen->enz='o';}
			else if(itna=="min"){gen->enz='c';}
			else if(itna=="<"){gen->enz='<';}
			else if(itna==">"){gen->enz='>';}
			else if(itna=="vec"||itna=="vector"){gen->enz='v';}
			else if(itna=="uni"||itna=="union"){gen->enz='u';}
			else if(itna=="if"){gen->enz='i';}
			else if(itna=="jf"){gen->enz='j';}
			else if(itna=="for"||itna=="while"){gen->enz='f';}
			else if(itna=="[r]"){gen->enz='r';}
			else if(itna=="[w]"){gen->enz='w';}
			else if(itna=="{}"){gen->enz='h';gen->ban=0;}
			else if(itna=="siz"){gen->enz='z';}
			else if(itna=="lef"){gen->enz='l';}
			else if(itna=="rig"){gen->enz='m';}
			else {
				bool atta=0;
				for(int i=0;i<atnames.size();i++){
					if(atnames[i]==itna){
						atta=1;
						gen->enz='&';
						gen->ban=atnames.size()-1-i;
					}
				}
				if(!atta){gen->enz='$';gen->ban=roll(itna);}//どこにもないので未使用のletが確定する
			}
			//cerr<<"itna="<<itna;
		}
		
		if(kan->enz=='Q'){(*kan)=(*gen);}
		else{
			//cerr<<"de";
			ptr<some>ata(new some());
			ata->hi=kan;
			ata->mg=gen;
			ata->enz='#';
			kan=ata;
		}
	}
	ptr<some>ans=kan;
	while(scopename--){
		ptr<some>ata(new some());
		ata->enz='@';
		ata->hi=ans;
		ans=ata;
		atnames.pob();
	}
	do{hibas++;}while(str[hibas]==' ');
	return ans;
}
void letkai(ptr<some>in,bool aaa=1){
	if((!aaa)&&in->ramukan){return;}
	if(in->enz=='#'||in->enz=='@'){
		if(in->hi->enz=='$'){
			if(letnames.count(in->hi->ban)==0){cerr<<"missed name find"<<in->hi->ban<<endl;}
			else{in->hi=letnames[in->hi->ban];}
		}else{letkai(in->hi,0);}
	}
	if(in->enz=='#'){
		if(in->mg->enz=='$'){
			if(letnames.count(in->mg->ban)==0){cerr<<"missed name find"<<in->mg->ban<<endl;}
			else{in->mg=letnames[in->mg->ban];}
		}else{letkai(in->mg,0);}
	}
}
int tabu=0;
void TS(void){for(int i=0;i<tabu;i++){cout<<"    ";}}
void syutu(const ptr<some> in){
	cout<<"{\n";				//}
	tabu++;
	if(in->enz=='@'){//らむだ
		TS();cout<<"enz:@\n";
		//TS();cout<<"arg :"<<in->ban<<endl;
		TS();cout<<"arg:";syutu(in->hi);
	}
	else if(in->enz=='#'){//えんざん
		TS();cout<<"enz:#\n";
		TS();cout<<"arg :";syutu(in->hi);
		TS();cout<<"body:";syutu(in->mg);
	}
	else if(in->enz=='&'){//即値
		TS();cout<<"enz:&\n";
		TS();cout<<"arg :"<<in->ban<<endl;
	}
	else if(in->enz=='n'){TS();cout<<"nut="<<in->ban<<endl;}
	else{TS();cout<<"enz="<<in->enz<<endl;}
	tabu--;
	TS();cout<<"}\n";				//{
}
bool isoy(ptr<some>tai){return (tai!=nullptr&&(!tai->syu)&&(tai->enz=='#'||tai->enz=='@'));}
bool daishi(ptr<some>&tai,int num,ptr<some>&xca){
	//cerr<<"dai";
	//syutu(tai);
	if(tai->syu){return 0;}
	if(tai->ramukan){return 0;}
	bool hen=0;
	ptr<some>moto=tai;
	ptr<some> iti(new some());(*iti)=(*tai);tai=iti;tai->syu=0;
	if(tai->enz=='&'&&tai->ban==num){tai=xca;hen=1;}
	else if(tai->enz=='&'&&tai->ban>num){tai->ban--;hen=1;}
	else if(tai->enz=='@'){hen|=daishi(tai->hi,num+1,xca);}
	else if(tai->enz=='#'){hen|=daishi(tai->hi,num,xca);hen|=daishi(tai->mg,num,xca);}
	if(hen){tai->syu=0;}else{tai=moto;}
	return hen;
}

//dainyu(0,0,...)
void WA_RE(int in=-1333){cerr<<"RE"<<in;exit(0);}
void keisan(ptr<some>in){
	//cerr<<"sai";
	if(in==nullptr||in->syu||in->enz!='#'){return;}
		if(in->hi!=nullptr&&in->hi->enz=='$'){exit(0);}
	//cerr<<"de "<<in->enz;
	//syutu(in);
	if(isoy(in->hi)){keisan(in->hi);}
	
	if(in->hi->enz=='@'){
		//cerr<<"ramu";
		keisan(in->mg);
		ptr<some> xca=in->mg;
		//syutu(xca);
		ptr<some> tai=in->hi->hi;
		daishi(tai,0,xca);
		(*in)=(*tai);
		keisan(in);
		return;
	}
	if(in->hi->enz=='1'){
		if(isoy(in->mg)){keisan(in->mg);}
		in->enz='3';return;
	}
	else if(in->hi->enz=='2'){in->syu=1;in->enz='4';return;}
	else if(in->hi->enz=='3'){(*in)=(*(in->hi->mg));return;}
	else if(in->hi->enz=='4'){
		if(isoy(in->mg)){keisan(in->mg);}
		(*in)=(*(in->mg));return;
	}else if(isoy(in->mg)){keisan(in->mg);}
	char hiz=in->hi->enz,mgz=in->mg->enz;
	//cerr<<"himg="<<hiz<<mgz<<endl;
	
	if(hiz=='#'||hiz=='@'||hiz=='&'){in->syu=1;return;}
	if(mgz=='n'){
		bool yatta=1;
		//if(in->hi->enz=='*'&&in->mg->ban==2){cerr<<"hyo";}
		switch(hiz){
			case '+':in->enz='P';in->ban =in->mg->ban;break;
			case 'P':in->enz='n';in->ban =in->hi->ban+in->mg->ban;break;
			case '-':in->enz='M';in->ban =in->mg->ban;break;
			case 'M':in->enz='n';in->ban =in->hi->ban-in->mg->ban;break;
			case '*':in->enz='T';in->ban =in->mg->ban;break;
			case 'T':in->enz='n';in->ban =in->hi->ban*in->mg->ban;break;
			case '/':in->enz='D';in->ban =in->mg->ban;break;
			case 'D':in->enz='n';in->ban =in->hi->ban/in->mg->ban;break;
			case '%':in->enz='A';in->ban =in->mg->ban;break;
			case 'A':in->enz='n';in->ban =in->hi->ban%in->mg->ban;break;
			case 'o':in->enz='O';in->ban =in->mg->ban;break;
			case 'O':in->enz='n';in->ban =max(in->hi->ban,in->mg->ban);break;
			case 'c':in->enz='C';in->ban =in->mg->ban;break;
			case 'C':in->enz='n';in->ban =min(in->hi->ban,in->mg->ban);break;
			case '<':in->enz='S';in->ban =in->mg->ban;break;
			case 'S':in->enz='n';in->ban=((in->hi->ban < in->mg->ban)?1:0);break;
			case '>':in->enz='B';in->ban =in->mg->ban;break;
			case 'B':in->enz='n';in->ban=((in->hi->ban > in->mg->ban)?1:0);break;
			default:yatta=0;
		}
		if(yatta){
			//cerr<<"ya";
			in->hi=nullptr;in->mg=nullptr;in->syu=1;return;
		}
	}
	if(in->enz!='#'){in->hi=nullptr;in->mg=nullptr;}//計算を行ったので
	if(hiz=='v'&&mgz=='n'){in->enz='V';in->ban=in->mg->ban;}
	if(hiz=='V'){(*in)=(*make_hai(in->hi->ban,in->mg));}
	if(hiz=='u'&&mgz=='h'){in->enz='U';in->hi=in->mg;}
	if(hiz=='z'){in->enz='n';in->ban =in->mg->ban;}
	if(hiz=='l'){in->enz='h';in->ban =in->mg->hi->ban;in->hi=in->mg->hi->hi;}
	if(hiz=='m'){in->enz='h';in->ban =in->mg->ban-in->mg->hi->ban;in->hi=in->mg->hi->mg;}
	if(hiz=='U'&&mgz=='h'){(*in)=(*unite_hai(in->hi->hi,in->mg));}
	if(hiz=='h'){
		
		ptr<some>ata(new some());
		ata->ban=1;ata->enz='h';ata->hi=in->mg;
		(*in)=*unite_hai(in->hi,ata);
		
	}
	if(hiz=='r'&&mgz=='h'){in->enz='R';in->hi=in->mg;}
	if(hiz=='R'&&mgz=='n'){(*in)=(*hairead(in->hi->hi,in->mg->ban));}
	if(hiz=='w'&&mgz=='h'){in->enz='W';in->hi=in->mg;}
	if(hiz=='W'&&mgz=='n'){in->enz='=';in->hi=in->hi->hi;in->ban=in->mg->ban;}
	if(hiz=='='){(*in)=(*haiwrite(in->hi->hi,in->hi->ban,in->mg));}
	if(hiz=='i'&&mgz=='n'){in->enz=((in->mg->ban)?'1':'2');}
	if(hiz=='j'&&mgz=='n'){in->enz=((in->mg->ban<0)?'1':'2');}
	in->syu=1;return;
}
#include <sstream>
int main(void){
	
	for(auto code:codelines){
		std::istringstream stm(code);
		string mei;stm>>mei;
		if(mei=="winn"){
			string na;stm>>na;
			llint kaz;cin>>kaz;
			llint rollname=roll(na);
			ptr<some> soku(new some());
			soku->enz='n';
			soku->ban=kaz;
			letnames[rollname]=soku;
		}
		if(mei=="winh"){
			string na;stm>>na;//名前
			string enu;stm>>enu;
			for(auto it:letnames){letkai(it.sec);}
			ptr<some>terget=letnames[roll(enu)];
			keisan(terget);
			llint size=0;
			if(terget->enz=='n'){size=terget->ban;}
			else{cout<<"era-"<<endl;}
			//ここで入力量を決める
			ptr<some> zro(new some());zro->enz='n';zro->ban=0;
			ptr<some> haso(make_hai(size,zro));
			for(int i=0;i<size;i++){
				ptr<some> soku(new some());
				soku->enz='n';cin>>soku->ban;
				haso=haiwrite(haso,i,soku);
			}
			letnames[roll(na)]=haso;
		}
		if(mei=="wins"){
			string na;stm>>na;//名前
			string nyu;cin>>nyu;
			int size=nyu.size();
			ptr<some> zro(new some());zro->enz='n';zro->ban=0;
			ptr<some> haso(make_hai(size,zro));
			for(int i=0;i<size;i++){
				ptr<some> soku(new some());
				soku->enz='n';soku->ban=nyu[i];
				haso=haiwrite(haso,i,soku);
			}
			letnames[roll(na)]=haso;
		}
		
		if(mei=="let"){
			string na;stm>>na;
			getline(stm,str);
			hibas=0;while(str[hibas]==' '){hibas++;}
			if(letnames.count(roll(na))){cout<<"namaenosyoutotu"<<endl;}
			letnames[roll(na)]=pasa();
			letnames[roll(na)]->ramukan=1;
			
			
		}
		if(mei=="woutn"){
			string na;stm>>na;
			//let名前解決する
			for(auto it:letnames){letkai(it.sec);}
			//syutu(letnames[roll(na)]);
			ptr<some>terget=letnames[roll(na)];
			keisan(terget);
			
			if(terget->enz=='n'){cout<<terget->ban<<endl;}
			else{syutu(terget);cerr<<"erra-"<<terget->enz<<endl;}
		}
		if(mei=="wouth"){
			string na;stm>>na;
			for(auto it:letnames){letkai(it.sec);}
			ptr<some>terget=letnames[roll(na)];
			keisan(terget);
			if(terget->enz=='h'){
				for(int i=0;i<terget->ban;i++){cout<<hairead(terget,i)->ban<<endl;}
			}else{cerr<<"ella-"<<terget->enz<<endl;}
		}
		if(mei=="wouts"){
			string na;stm>>na;
			for(auto it:letnames){letkai(it.sec);}
			ptr<some>terget=letnames[roll(na)];
			keisan(terget);
			if(terget->enz=='h'){
				for(int i=0;i<terget->ban;i++){cout<<(char)(hairead(terget,i)->ban);}
				cout<<endl;
			}else{cerr<<"ela-"<<terget->enz<<endl;}
		}
		if(mei=="end"){return 0;}
	}
	return 0;
}


/*
2019
やりたいこと
高速化
わかりやすさ強化

関数一覧
		(itna=="+"){gen->enz='+';}
else if(itna=="-"){gen->enz='-';}
else if(itna=="*"){gen->enz='*';}
else if(itna=="/"){gen->enz='/';}
else if(itna=="%"){gen->enz='%';}
else if(itna=="max"){gen->enz='o';}
else if(itna=="min"){gen->enz='c';}
else if(itna=="<"){gen->enz='<';}
else if(itna==">"){gen->enz='>';}
else if(itna=="vec"||itna=="vector"){gen->enz='v';}
else if(itna=="uni"||itna=="union"){gen->enz='u';}
else if(itna=="if"){gen->enz='i';}
else if(itna=="jf"){gen->enz='j';}
else if(itna=="for"||itna=="while"){gen->enz='f';}
else if(itna=="[r]"){gen->enz='r';}
else if(itna=="[w]"){gen->enz='w';}
else if(itna=="{}"){gen->enz='h';gen->ban=0;}



実装したいもの

double型の実装


nut @a @b 		(a(a(a(b))))//チャーチ数として処理する

やっぱり数字をプログラム中に入出力したい
3
	2
		3 4
	1
		2
	4
		3 4 5
このままだとこれみたいな入力形式に対応できない

ptinput @a (aはnut)入力を受け取る番号
doinput @a 		内部のptinputが含まれる関数をすべて展開して、 実際に入力する
内部にptinputが含まれる関数は、関数を深く複製しなければいけない。
doinputによる入力は、一回しかされない。
doinputをしたら、ptinput関数が全て入力された値に置き換わる




let tukeru
let myin pic0  


let watle ((ptoutput 1)(* 2 (ptinput 1))) 
let in1 out1 (10 watle)

??よくわからん


vectorのnlognソートってできたっけ?
とりあえずクイックソート

winn n 
winh hai

let next (@a (+ a 1))
let prev (@a (- a 1))
let aswap (@ary @a @b ([w] ([w] ary a ([r] ary b)) b ([r] ary a)))
let part (@ary @st @en 
	if (- st en)
	(
		if (< ([r] ary st) ([r] ary (pone st)))
		(part (aswap ary (next st) en ) st (prev en))
		(part (aswap ary st (next st) ) (next st) en)
	)
	({} ary st)
)
//パーティションして、tuple(ソート済みアレイ、最後の場所) を返します

let quick (@ary @st @en 
	if (-st en)
	(
		(@in (quick (quick ([w] in 0) st (prev([w] in 1)) ) (next ([w] in 1)) en ))
		(part ary st en)
	)
	(ary)
)

let ans (quick hai 0 (prev n))

wouth ans
*/


Measured memory_bytes: 768