#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <experimental/random>

class EmptyList { };
class EmptyDict { };

template <class T, class S>
struct CommonType { };

struct String;

template <class T>
struct List {
    std::shared_ptr<std::vector<T>> inner;

    List() : inner(std::shared_ptr<std::vector<T>>(new std::vector<T>())) {}
    List(std::vector<T> a) : inner(std::shared_ptr<std::vector<T>>(new std::vector<T>(a))) {}
    template <class S>
    List(List<S> a) : inner(std::shared_ptr<std::vector<T>>(new std::vector<T>(a.inner->begin(), a.inner->end()))) {}
    List(EmptyList) : inner(std::shared_ptr<std::vector<T>>(new std::vector<T>())) {}
    T &operator[](size_t index) { return (*inner)[index]; }
    const T &operator[](size_t index) const { return (*inner)[index]; }
    void push(T value) { inner->push_back(value); }
    void pop() { inner->pop_back(); }
    int64_t length() const { return inner->size(); }

    template <class SortFn>
    void sort(SortFn sortFn) { std::sort(inner->begin(), inner->end(), sortFn); }

    template <class S>
    List<typename CommonType<T, S>::inner> concat(List<S> other) { List<typename CommonType<T, S>::inner> ret = EmptyList(); ret.inner->insert(ret.inner->end(), inner->begin(), inner->end()); ret.inner->insert(ret.inner->end(), other.inner->begin(), other.inner->end()); return ret; }
    String join(String delim);
};

struct String {
    std::shared_ptr<std::string> inner;

    String() : inner(std::shared_ptr<std::string>(new std::string())) {}
    String(const char* a) : inner(std::shared_ptr<std::string>(new std::string(a))) {}
    String(std::string a) : inner(std::shared_ptr<std::string>(new std::string(a))) {}
    String operator +(String other) { return String(*inner + *other.inner); }
    operator std::string() { return *inner; }
    String operator[](size_t index) const { return String(std::string({(*inner)[index]})); }
    int64_t charCodeAt(size_t index) const { return (*inner)[index]; }
    String operator+(String &other) const { return String(*inner + *other.inner); }
    String& operator+=(const String &other) { *inner += *other.inner; return *this; }
    char operator <(const String &other) const { return *inner < *other.inner; }
    char operator >(const String &other) const { return *inner > *other.inner; }
    char operator <=(const String &other) const { return *inner <= *other.inner; }
    char operator >=(const String &other) const { return *inner >= *other.inner; }
    char operator ==(const String &other) const { return *inner == *other.inner; }
    char operator !=(const String &other) const { return *inner != *other.inner; }
    int64_t length() const { return inner->size(); }

    String trim() {
        std::string s = *inner;
        const char* ws = " \t\n\r\f\v";
        s.erase(s.find_last_not_of(ws) + 1);
        s.erase(0, s.find_first_not_of(ws));
        return String(s);
    }
    List<String> split(String delim) { std::vector<String> ret; size_t last = 0; size_t next = 0; while ((next = inner->find(*delim.inner, last)) != std::string::npos) {   ret.push_back(String(inner->substr(last, next-last)));   last = next + delim.length(); } ret.push_back(String(inner->substr(last))); return ret; }
};

template<> String List<String>::join(String delim) {
    std::string ret; bool first = true; for (const auto& it: *inner) { if (!first) ret += *delim.inner; first = false; ret += *it.inner; } return ret;
}

template <class K, class V>
struct Dict {
    using Inner = __gnu_pbds::tree<K, V, std::less<K>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
    std::shared_ptr<Inner> inner;

    Dict() : inner(std::shared_ptr<Inner>(new Inner())) {}
    Dict(std::vector<std::pair<K, V>> a) : inner(std::shared_ptr<Inner>(new Inner(a.begin(), a.end()))) {}
    template <class K2, class V2>
    Dict(Dict<K2, V2> a) : inner(std::shared_ptr<Inner>(new Inner(a.inner->begin(), a.inner->end()))) {}
    Dict(EmptyDict) : inner(std::shared_ptr<Inner>(new Inner())) {}
    V &operator[](K key) { return (*inner)[key]; }
    const V &operator[](K key) const { return (*inner)[key]; }
    int64_t length() const { return inner->size(); }
    bool contains(const K& k) const { return inner->find(k) != inner->end(); }
    void remove(const K& k) const { inner->erase(k); }
    K keyAt(int64_t pos) const { return inner->find_by_order(pos)->first; }
    int64_t bisect(const K& value) const { return inner->order_of_key(value); }

    List<K> keys() const { std::vector<K> ret; for (auto const& it: *inner) { ret.push_back(it.first); }; return List<K>(ret); }
};

template <class T> struct CommonType<List<T>, EmptyList> { using inner = List<T>; };
template <class T> struct CommonType<EmptyList, List<T>> { using inner = List<T>; };
template <class T, class S> struct CommonType<List<T>, List<S>> { using inner = List<typename CommonType<T, S>::inner>; };

template <class T> struct CommonType<std::nullptr_t, std::shared_ptr<T>> { using inner = std::shared_ptr<T>; };
template <class T> struct CommonType<std::shared_ptr<T>, std::nullptr_t> { using inner = std::shared_ptr<T>; };

template <class K, class V> struct CommonType<Dict<K, V>, EmptyDict> { using inner = Dict<K, V>; };
template <class K, class V> struct CommonType<EmptyDict, Dict<K, V>> { using inner = Dict<K, V>; };
template <class K, class V, class K2, class V2> struct CommonType<Dict<K, V>, Dict<K2, V2>> { using inner = Dict<typename CommonType<K, K2>::inner, typename CommonType<V, V2>::inner>; };

template <> struct CommonType<int64_t, int64_t> { using inner = int64_t; };
template <> struct CommonType<EmptyList, EmptyList> { using inner = EmptyList; };
template <> struct CommonType<EmptyDict, EmptyDict> { using inner = EmptyDict; };
template <> struct CommonType<std::nullptr_t, std::nullptr_t> { using inner = std::nullptr_t; };
template <> struct CommonType<char, char> { using inner = char; };
template <> struct CommonType<String, String> { using inner = String; };

int64_t cast_int(long int x) { return x; }
int64_t cast_int(long long int x) { return x; }
int64_t cast_int(double x) { return x; }
int64_t cast_int(String x) { return std::strtoll(x.inner->c_str(), NULL, 10); }

double cast_float(long int x) { return x; }
double cast_float(long long int x) { return x; }
double cast_float(double x) { return x; }
double cast_float(String x) { return std::strtod(x.inner->c_str(), NULL); }

String cast_str(long int x) { return String(std::to_string(x)); }
String cast_str(long long int x) { return String(std::to_string(x)); }
String cast_str(double x) { return String(std::to_string(x)); }
String cast_str(bool x) { return String(std::to_string(x)); }
String cast_str(String x) { return x; }

bool rand_bool() { return rand() % 100 < 50; }
int64_t rand_int(int64_t upper) { return std::experimental::randint(int64_t(0), upper); }
String chr(int64_t x) { return String(std::string({(char)x})); }

template <class T>
struct TypeHolder { };

template <class T, class S, class Filter, class Mapper>
List<T> linq(const List<S> &input, Filter filter, Mapper mapper, TypeHolder<T> type_holder) {
    List<T> ret;
    for (const auto &it: *input.inner) {
        if (filter(it)) {
            ret.push(mapper(it));
        }
    }
    return ret;
}

String solve(String input);
String solve(String input) {
List<String> lines = (input).split(String("\n"));
List<String> line1 = ((lines)[0ll]).split(String(" "));
List<String> line2 = ((lines)[1ll]).split(String(" "));
int64_t n = cast_int((line1)[0ll]);
int64_t m = cast_int((line1)[1ll]);
int64_t k = cast_int((line1)[2ll]);
int64_t r = cast_int((line1)[3ll]);
int64_t c = cast_int((line1)[4ll]);
int64_t ax = cast_int((line2)[0ll]);
int64_t ay = cast_int((line2)[1ll]);
int64_t bx = cast_int((line2)[2ll]);
int64_t by = cast_int((line2)[3ll]);
int64_t till = 0ll;
if (((ax==bx)&&(ay==by))) {
till = (n*m);
} else {till = ((n*m)-(r*c));
}
int64_t now = 1ll;
for (int64_t i = 0ll; (i<till); i+=1ll) {
now*=k;
now%=1000000007ll;

}
return cast_str(now);
}
int main() {  std::ios::sync_with_stdio(false) ; std::cin.tie(0) ; std::cout.tie(0) ; std::string line, input; while (std::getline(std::cin, line)) { input += line + "\n"; } std::cout << *solve(String(input)).inner; return 0; }/*1652205446.7610285*/


#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll MOD=1e9+7;
using namespace std;
ll N,M,K,R,C;
ll a,b,c,d;
vector <pair<ll,ll> > vec;
ll pangkat(ll x,ll y){
	if(y==0){
		return 1;
	}
	if(y==1){
		return x;
	}
	ll z=pangkat(x,y/2);
	if(y%2==0){
		return (z*z)%MOD;
	}
	else{
		return ((z*z)%MOD*x)%MOD;
	}
}
int main(){
	cin>>N>>M>>K>>R>>C;
	cin>>a>>b>>c>>d;
	ll x=2*R-abs(max(a+R-1,c+R-1)-min(a,c)+1);
	ll y=2*C-abs(max(b+C-1,d+C-1)-min(b,d)+1);
	if(x<=0||y<=0){
		cout<<pangkat(K,N*M-R*C)<<endl;
	}
	else{
		ll tempx=x;
		ll tempy=y;
		vec.push_back({R,C});
		ll offx=abs(a-c);
		ll offy=abs(b-d);
		while(tempx>0&&tempy>0){
			vec.push_back({tempx,tempy});
			tempx-=offx;
			tempy-=offy;
		}
		ll exp=N*M-2*vec[0].fi*vec[0].se+vec[1].fi*vec[1].se;
		for(int i=0;i<vec.size()-2;i++){
			exp+=vec[i].fi*vec[i].se-2*vec[i+1].fi*vec[i+1].se+vec[i+2].fi*vec[i+2].se;
		}
		ll s=vec.size();
		exp+=vec[s-2].fi*vec[s-2].se-2*vec[s-1].fi*vec[s-1].se;
		exp+=vec[s-1].fi*vec[s-1].se;
		cout<<pangkat(K,exp)<<endl;
	}
}


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

pair <int,int> NoOfWays(string s) // gives the min and max position we can reach
{
      // Base Case
      if (s.size()==0)
      {
            pair <int,int> worstAns(INT_MAX,INT_MIN);
            return worstAns;
      }

      if (s.size()==1)
      {
            if (s[0]=='L')
            {
                  pair <int,int> verySmallAns(-1,0);
                  return verySmallAns;
            }

            else
            {
                  pair <int,int> verySmallAns(0,1);
                  return verySmallAns;
            }
      }

      // Our Calculation
      int move=0;
      if (s[0]=='L')
      {
            move=-1;
      }
      else
      {
            move=1;
      }

      pair <int,int> onePossibleAns=NoOfWays(s.substr(1));
      pair <int,int> secondPossibleAns=NoOfWays(s.substr(1));

      int overAllMin=min(onePossibleAns.first+move,secondPossibleAns.first);
      int overAllMax=max(onePossibleAns.second+move,secondPossibleAns.second);

      pair <int,int> finalAns(overAllMin,overAllMax);
      return finalAns;
}

int main()
{
      int t;
	cin>>t;

       char * s=new char [t];
       for (int i=0;i<t;i++)
       {
            cin>>s[i];
       }

       pair <int,int> minMax=NoOfWays(s);
       cout<<minMax.second-minMax.first+1<<endl;
       //cout<<minMax.second<<" "<<minMax.first<<endl;

 return 0;
}


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

#define ll long long

ll county=0;

std::vector<ll> v;

void countPossiblePos(string s,ll st, ll end,ll pos)
{
	if(st>end)
		{
			v.push_back(pos)	;
					return;
		}
	ll inc;
	if(s[st]=='L')
	 inc = -1;
	else inc = 1;
	 countPossiblePos(s,st+1,end,pos+inc);

	countPossiblePos(s,st+1,end,pos);
	//cout<<"excld = "<<exclude<<endl;

}
int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif

  ll n; cin>>n;

  string s; cin>>s;
  ll x=0;

  countPossiblePos(s,0,n-1,x);

  //cout<<stt.size()<<endl;
  // cout<<county<<endl;
  //
  sort(v.begin(),v.end());
  ll county=0;
  ll i;
  for(i=1;i<v.size();i++)
  {
  	if(v[i]!=v[i-1])
  		county++;
  }
  i--;
  if(v[i]!=v[i-1])
  	county++;
  cout<<county<<endl;

  return 0;

}


#include<bits/stdc++.h>

// #include</Users/sai-4365/sai/oldlaptopflow/oldc/cpp/stdc++.h>
using namespace std;

set<int> getDistinctCount(string s,int n,map<int,set<int> > m)
{
    if(m.find(n)!=m.end())
    {
        return m[n];
    }

    set<int> res;
    if(n==0)
    {
        res.insert(0);
        m[n]=res;
        return res;
    }

    set<int> e = getDistinctCount(s,n-1,m);
    res.insert(e.begin(),e.end());
    int cur = s[n-1]=='L'?-1:+1;
    set<int>::iterator it;
    for(it=e.begin();it!=e.end();it++)
    {
        res.insert((*it)+cur);
    }

    m[n]=res;
    return res;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int n;
    cin>>n;

    string s;
    cin>>s;
    map<int,set<int> > m;
    set<int> r = getDistinctCount(s,n,m);
    cout<<r.size()<<"\n";

    return 0;
}


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

#define ll long long
set<ll>stt;

void countPossiblePos(string s,ll st, ll end,ll pos)
{
	if(st>end)
		{
			stt.insert(pos);
			return;
		}
	ll inc;
	if(s[st]=='L')
	 inc = -1;
	else inc = 1;
	 countPossiblePos(s,st+1,end,pos+inc);

	countPossiblePos(s,st+1,end,pos);
	//cout<<"excld = "<<exclude<<endl;

}
int main()
{

  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif

  ll n; cin>>n;

  string s; cin>>s;
  ll x=0;

  countPossiblePos(s,0,n-1,x);

  cout<<stt.size()<<endl;

  return 0;

}


#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <bits/stdc++.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
int main()
{
    string a, b = "1";
    int n;
    int track;
    cin >> n;
    cin >> a;
    if (a=="0"){
    	cout << "0";
	}
	else{
	for (int i = 0; i<n; i++){
		if (a[i]=='0') track++;
	}
    for (int i = 0; i<track; i++){
    	b+='0';
	}
	cout << b;
	}
    return 0;
}


#include <iostream>

using namespace std;
int main(){
	int n,zeros;
	cin>>n;
	char msg[n];
	string ans {};
	for(int i=0; i<n;i++){
		cin>>msg[i];
		if(msg[i]=='0') zeros++;
	}
	if(msg[0]=='1') ans+='1';
	for(int i=0;i<zeros;i++)
		ans+='0';
	cout<<ans;
	return 0;
}


#include <iostream>
#include <string>

using namespace std;
int main(){
	int n,zeros;
	cin>>n;
	char msg[n];
	string ans {};
	for(int i=0; i<n;i++){
		cin>>msg[i];
		if(msg[i]=='0') zeros++;
	}
	if(msg[0]=='1') ans+='1';
	for(int i=0;i<zeros;i++)
		ans+='0';
	cout<<ans;
	return 0;
}


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

long long a, b, c, i;
string s;
deque < string > v(100);

void prov()
{
    if ( abs((a+b)-c) == 0 ) cout << s;
    else if ( abs((a+b)-c) == 2 )
    {
        v.push_front("|");
        for ( i = 0; i < s.length(); i++ )
        {
            cout << v[i];
        }
    }
    else cout << "Impossible";
}

int main()
{
    cin >> s;
    while ( s[i] != 43 )
    {
        a++;
        i++;
    }
    i++;
    while ( s[i] != 61 )
    {
        b++;
        i++;
    }
    i++;
    while ( i < s.length() )
    {
        c++;
        i++;
    }
    for ( i = 0; i <= s.length(); i++ )
    {
        v[i] = s[i];
    }
    cerr << a << " " << b << " " << c;
    prov();
}


//In the Name of God
#include<iostream>
#include<math.h>
#include<string.h>
#include<conio.h>
using namespace std;
main()
{
      string s;
      cin>>s;
      int l=s.length();
      int A=0,B=0,C=0,p=0,m=0,i,k;
      for(i=0;i<l;i++)
      {
                      if((int)s[i]==43)
                      p++;
                      if((int)s[i]==61)
                      m++;
                      if((int)s[i]==124 && p==0 && m==0)
                      A++;
                      if((int)s[i]==124 && p==1 && m==0)
                      B++;
                      if((int)s[i]==124 && p==1 && m==1)
                      C++;
      }
      k=A+B+C;
      if(k%2!=0 || fabs(C-A-B)>2)
      cout<<"Impossible"<<endl;
      else
      {
            k=k/2;
            A=A+C-k;
            C=k;
      while(A<1)
      {
                A++;
                B--;
      }
      for(i=0;i<A;i++)
      cout<<"|";
      cout<<"+";
      for(i=0;i<B;i++)
      cout<<"|";
      cout<<"=";
      for(i=0;i<C;i++)
      cout<<"|";
      cout<<endl;
      }
      getch();
      return 0;
}


//In the Name of God
#include<iostream>
#include<math.h>
#include<string.h>
#include<conio.h>
using namespace std;
main()
{
      string s;
      cin>>s;
      int l=s.length();
      int A=0,B=0,C=0,p=0,m=0,i,k;
      for(i=0;i<l;i++)
      {
                      if((int)s[i]==43)
                      p++;
                      if((int)s[i]==61)
                      m++;
                      if((int)s[i]==124 && p==0 && m==0)
                      A++;
                      if((int)s[i]==124 && p==1 && m==0)
                      B++;
                      if((int)s[i]==124 && p==1 && m==1)
                      C++;
      }
      k=A+B+C;
      if(k%2!=0 || fabs(C-A-B)>2)
      cout<<"Impossible"<<endl;
      else
      {
            k=k/2;
            A=A+C-k;
            C=k;
      }
      while(A<1)
      {
                A++;
                B--;
      }
      for(i=0;i<A;i++)
      cout<<"|";
      cout<<"+";
      for(i=0;i<B;i++)
      cout<<"|";
      cout<<"=";
      for(i=0;i<C;i++)
      cout<<"|";
      cout<<endl;
      getch();
      return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a1[6];
int b1[6];
struct node
{
    int a;
    int b;
} c[5];
int checkxy(int i,int j)
{
    if(c[i].a<c[j].a && c[i].b<c[j].b)
        return 1;
    return 0;
}
int checkdy(int i,int j)
{
    if(c[i].a>c[j].a && c[i].b>c[j].b)
        return 1;
    return 0;
}
int checkdey(int i,int j)
{
    if(c[i].a==c[j].a || c[i].b==c[j].b)
        return 1;
    return 0;
}
int main()
{
    int cas=0;
    while(1)
    {
         for(int i=1; i<=4; i++)
        {
            scanf("%d%d",&a1[i],&b1[i]);
        }
        cas++;
        if(cas==5)
        printf("Team 1\n");
        else
        {

        c[1].a=a1[1];
        c[1].b=b1[2];
        c[2].a=a1[2];
        c[2].b=b1[1];
        c[3].a=a1[3];
        c[3].b=b1[4];
        c[4].a=a1[4];
        c[4].b=b1[3];
//    for(int i=1;i<=4;i++)
//    printf("%d %d\n",c[i].a,c[i].b);

        if(((checkdy(1,3)) && (checkdy(1,4))) || ((checkdy(2,3)) && (checkdy(2,4))))
        {
            printf("Team 1\n");
        }
//    else if((checkxy(1,3)) || (checkxy(2,3)) || (checkdy(1,3) && (checkxy(1,4))) || ((checkxy(2,4) && (checkdey(2,3)))) || (checkdy(2,3) && (checkxy(2,4))) || ((checkxy(1,4) && checkdey(1,3))))
//    {
//        printf("Team 2\n");
//    }
//else
//    {
//       printf("Draw\n");
//    }
        else if((checkdy(1,3)&&(checkdey(1,4))) || (checkdey(1,3)&&(checkdey(1,4))) || (checkdey(1,3)&&checkdy(1,4)) || (checkdy(2,3)&&(checkdey(2,4))) || (checkdey(2,3)&&(checkdey(2,4))) || (checkdey(2,3)&&checkdy(2,4)))
        {
            printf("Draw\n");
        }
        else
        {
            printf("Team 2\n");
        }
        }

    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{   for(;;){
    int a,b,c,d,m,n,x,y;
    scanf("%d %d",&a,&b);
    scanf("%d %d",&c,&d);
    scanf("%d %d",&m,&n);
    scanf("%d %d",&x,&y);

    if((a>c&&b>d)||((c>a&&d>b)))
    {
        if((a>max(y,n)&&d>max(x,m))||(c>max(y,n)&&b>max(x,m)))
            cout<<"Team 1"<<endl;
        else if(((c<y&&b<m)||(c<n&&b<x))&&((a<y&&d<m)||(a<n&&d<x)))
            cout<<"Team 2"<<endl;
        else
            cout<<"Draw"<<endl;
        continue;
    }

    a=max(a,c);
    b=max(b,d);
    if((x>b&&n>a)||(m>b&&y>a))
        cout<<"Team 2"<<endl;
    else if((x<b&&n<a)&&(m<b&&y<a))
        cout<<"Team 1"<<endl;
    else
        cout<<"Draw"<<endl;
    //return 0;
}}


#include<iostream>
#include<fstream>
using namespace std;
int defence[4];
int atack[4];
int main(){
	for(int i=0;i<4;i++)
		cin>>defence[i]>>atack[i];

	int Counter=0;

	if( defence[0]  > defence[2] )
		Counter++;
	if( atack[1]  > atack[3] )
		Counter++;

	if( defence[0]  > defence[3] )
		Counter++;
	if( atack[1]  > atack[2] )
		Counter++;

	if( Counter == 4 ){
		printf("Team 1");
		return 0;
	}

	Counter=0;

	if( defence[1]  > defence[2] )
		Counter++;
	if( atack[0]  > atack[3] )
		Counter++;

	if( defence[1]  > defence[3] )
		Counter++;
	if( atack[0]  > atack[2] )
		Counter++;

	if( Counter == 4 ){
		printf("Team 1");
		return 0;
	}

	Counter=0;

	if( defence[2]  > defence[0] )
		Counter++;
	if( atack[3]  > atack[1] )
		Counter++;

	if( defence[2]  > defence[1] )
		Counter++;
	if( atack[3]  > atack[0] )
		Counter++;

	if( Counter == 4 ){
		printf("Team 2");
		return 0;
	}

	Counter=0;

	if( defence[3]  > defence[0] )
		Counter++;
	if( atack[2]  > atack[1] )
		Counter++;

	if( defence[3]  > defence[1] )
		Counter++;
	if( atack[2]  > atack[0] )
		Counter++;

	if( Counter == 4 ){
		printf("Team 2");
		return 0;
	}

	printf("Draw");
	system("pause");
	return 0;
}


// Problem: c.

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const unsigned ll l=1000000007;

int main()
{
  int n,m;

  cin >> n >> m;

  // ll a[n];

  // for(int i=0;i<n; i++)
  //   cin >> a[i];

  priority_queue<int> q;
  vector<int> v;

  int pipe=0,cnt=0;

  for(int i=0; i<n;i++)
  {
    int num;
    cin >> num;
    pipe += num;
    while(pipe > m && !q.empty())
    {
      int top = q.top();
      q.pop();
      v.pb(top);
      pipe-=top;
      cnt++;
    }

    cout << cnt << " ";
    for(auto i:v)
    {
      q.push(i);
      cnt--;
      pipe+=i;
    }
    q.push(num);
  }
  return 0;
}


// Problem: c.

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const unsigned ll l=1000000007;

int main()
{
  ll n,m;

  cin >> n >> m;

  // ll a[n];

  // for(int i=0;i<n; i++)
  //   cin >> a[i];

  priority_queue<ll> q;
  vector<ll> v;

  ll pipe=0,cnt=0;

  for(int i=0; i<n;i++)
  {
    ll num;
    cin >> num;
    pipe += num;
    while(pipe > m && !q.empty())
    {
      ll top = q.top();
      q.pop();
      v.pb(top);
      pipe-=top;
      cnt++;
    }

    cout << cnt << " ";
    for(auto i:v)
    {
      q.push(i);
      cnt--;
      pipe+=i;
    }
    q.push(num);
  }
  return 0;
}


// Problem: c.

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const unsigned ll l=1000000007;

int main()
{
  ll n,m;

  cin >> n >> m;

  ll a[n];

  for(int i=0;i<n; i++)
    cin >> a[i];

  priority_queue<ll> q;
  vector<ll> v;

  ll pipe=0,cnt=0;

  for(int i=0; i<n;i++)
  {
    pipe += a[i];
    while(pipe > m && !q.empty())
    {
      ll top = q.top();
      q.pop();
      v.pb(top);
      pipe-=top;
      cnt++;
    }

    cout << cnt << " ";
    for(auto i:v)
    {
      q.push(i);
      cnt--;
      pipe+=i;
    }
    q.push(a[i]);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se seconds
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define ll long long
#define sz size()
#define all(x) x.begin(), x.end()
#define forn(x) for(int i=1;i<=x;i++)
#define sforn(x, y) for(int i=1;i<=x;i++)for(int j=1;j<=y;j++)
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int arr[200100];
int ans[200100];
int main(){ speed;
    int n, m;
    cin >> n >> m;
    priority_queue<int> q;
    priority_queue<int, vector<int>, greater<int> > q2;
    int sum = 0;
    for(int i=1;i<=n;i++){
        while(q.sz>0 && q2.sz > 0 && q2.top() < q.top()){
            int f = q2.top();
            int s = q.top();
            sum-=s;
            sum+=f;
            q2.push(s);
            q.push(f);
        }
        cin >> arr[i];
        if(arr[i] + sum <= m){
            while(q2.sz > 0 && arr[i] + sum + q2.top() <=m){
                int k = q2.top();
                q2.pop();
                sum+=q2.top();
                q.push(k);
            }
            ans[i] = q2.sz;
        } else {
            while(q.sz > 0 && arr[i] + sum > m){
                int k = q.top();
                q.pop();
                sum-=k;
                q2.push(k);
            }
            ans[i] = q2.sz;
        }

        q.push(arr[i]);
        sum+=arr[i];
    }
    for(int i=1;i<=n;i++){
        cout << ans[i] << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main ()
{
    long long int a,d,e,f=1,max;
    vector <long long int> b;
    cin>>a;
   d=a;
   e=a;
   while (d>0)
   {
    while (true)
    {
        if (d%2==0) d=d/2;
        else {b.push_back (a-e+d);break;}
       }
       e--;
       d=e;
   }
   max=b[0];
   while (f<a)
   {
    if (max>b[f]) max=b[f];
    f++;
    }
    cout<<max<<endl;
    return 0;
}


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

#define MOD 1000000007

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long int i,x,ans=0;
    vector <long int >v;
    for(i=1;i<10e9;i=i*2)
      {
      	v.push_back(i);
      }
    cin>>x;
   if(binary_search(v.begin(),v.end(),x)==true)
     {
     	cout<<1<<"\n";
     }
    else
      {
      	ans=x-v[lower_bound(v.begin(),v.end(),x)-v.begin()-1]+1;
      	cout<<ans<<"\n";
      }
   return 0;
}


#include <iostream>
#include <windows.h>
using namespace std;
int main() {
    long n; cin>>n;
    //long long x=2;
    long long a[35];
    a[0]=1;
    for(int i=1; i<35; i++) {
        a[i]=a[i-1]*2;
    }
    int dem=1;
    while(1) {
        for(int i=0; i<35; i++) {
            if(n<a[i]) {
                dem++;
                n=n-a[i-1];
                //cout<<i<<" "<<dem<<" "<<a[i-1]<<" "<<n<<endl;
                break;
            } else if(n==a[i]) {
                cout<<1;
                return 0;
            }
        }
        if(n==1) {
            cout<<dem;
            break;
        }
        Sleep(300);
    }
}


#include <stdio.h>
#include <iostream>
#include <conio.h>

using namespace std;
int main() {
	int c,c1, k = 0;
	cin >> c;
	if (c == 1) k++;
	else
	while (c>=2)
	{
		if (c % 2==1) k++;
		c1 = c/2;
		if (c1 >= 2) c = c1;
		else if (c1 == 1)
		{
			k++;
			break;
		}
	}
	cout << k;
	getch();
	return 0;
}


#include<stdio.h>
#include<conio.h>
int main()
{
     int n,k,i=1,tot=0;
     printf("\nEnter n and k :");
     scanf("%d%d",&n,&k);
     tot+=k;
     for(i=1;i<=n;i++)
        {
            if(tot<=240)
            {
               tot+=5;
            }
        }
     printf("%d",i-1);
     getch();
     return(0);

}


#include<stdio.h>
#include<conio.h>
int main()
{
     int n,k,tot=0,tot1=0,flag=0;
     scanf("%d%d",&n,&k);
     tot+=k;
     for(int i=1;i<=n;i++)
        {
            tot1=tot;
            tot1+=i*5;
            if(tot1<=240)
            {
               tot=tot1;
               flag+=1;
            }
        }
     printf("%d",flag);
     getch();
     return(0);
}


#include<algorithm>
#include<iostream>
#include<cstdio>

using namespace std;

int main ()
{
    int n,k,i=1,tot=0;
    cin>>n>>k;
    int time=240-k;
    while(tot+5*i<=time && i<=n)
    {
        tot+=5*i;
        i++;
    }
    cout<<i-1<<endl;
}


#include<stdio.h>
#include<conio.h>
int main()
{
     int n,k,i=1,tot=0;
     scanf("%d%d",&n,&k);
     tot+=k;
     for(i=1;i<=n;i++)
        {
            if(tot<=240)
            {
               tot+=5;
            }
        }
     printf("%d",i);
     getch();
     return(0);
}


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <memory.h>
using namespace std;

long long gcd(long long m,long long n){
	if(n==0) return m;
	else return gcd(n,m%n);
}

long long f(long long m,long long n){
	if(n==0) return 0;
	long long mint=n,p=1;
	for(long long i=1;i<=sqrt(m);i++){
		if(m%i==0){
			long long x=n%i;
			if(x<=mint){
				if(i!=1){
					mint=x;
					p=max(p,i);
				}
			}
			long long y=n%(m/i);
			if(y<=mint){
				mint=y;
				p=max(p,m/i);
			}
		}
	}
	return mint+f(m/p,n/p);
}

int main(){
	long long a,b;
	scanf("%I64d %I64d",&a,&b);
	long long x=gcd(a,b);
	printf("%I64d\n",f(a/x,b/x));
return 0;
}


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

const long long int MAX=1e9;

vector<long long int> prime;

long long int cal(long long int X,long long int Y)
{
	if(Y==0)
	return 0;
	long long int hcf=__gcd(X,Y),steps=MAX;
	for(int A=0;A<prime.size();A++)
	{
		long long int cur=Y,now=(Y/(hcf*prime[A]))*hcf*prime[A];
		steps=min(steps,(cur-now)/hcf);
	}
	return steps+cal(X,Y-steps*hcf);
}

int main()
{
	long long int X,Y,XX;
	cin>>X>>Y;
	XX=X;
	for(int A=2;A*A<=XX;A++)
	{
		if(XX%A==0)
		{
			prime.push_back(A);
			while(XX%A==0)
			XX/=A;
		}
	}
	if(XX>1)
	prime.push_back(XX);
	cout<<cal(X,Y);
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int N = 1e6 + 7;
ll top, n, i, j;
int p[N + 10];
int f[N + 10];

inline void init() {
    top = 0;
    memset(f, 0, sizeof(f));
    for (i = 2; i <= N; ++i) {
        if (!f[i]) {
            p[++top] = i;
        }
        for (j = 1; j <= top && i * p[j] <= N; ++j) {
            f[i * p[j]] = 1;
            if (i % p[j] == 0)
                break;
        }
    }
    return;
}

ll ans, a, b, tmp;

ll gcd(ll a, ll b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int fa(ll a, ll b) {
    if (b == 0)
        return 0;
    tmp = a;
    for (i = 1; i <= top; ++i) {
        if (p[i] > a)
            break;
        if (a % p[i] != 0)
            continue;
        tmp = min(tmp, b % p[i]);
    }
    ans += tmp;
    b -= tmp;
    tmp = gcd(a, b);
    a /= tmp;
    b /= tmp;
    fa(a, b);
    return 0;
}

int main() {
    init();
    scanf("%lld%lld", &a, &b);
    ans = 0;
    tmp = gcd(a, b);
    a /= tmp;
    b /= tmp;
    tmp = 0;
    for (i = 1; i <= top; ++i)
        if (a % p[i] == 0)
            p[++tmp] = p[i];
    top = tmp;
    fa(a, b);
    printf("%lld\n", ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<pii> vpii;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-7
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))
#define INF 300000005
#define MAXN 10000000

map<ll, ll> arr;

ll mas[MAXN];

ll gcd(ll a, ll b)
{
	while (b)
	{
		a %= b;
		swap(a, b);
	}
	return a;
}

ll function(ll a, ll b)
{
	if (b >= MAXN)
	{
		if (b == 0)
		{
			return 0;
		}
		else
		{
			if (arr[b])
			{
				return arr[b];
			}
			else
			{
				return (arr[b] = 1 + function(a, b - gcd(a, b)));
			}
		}
	}
	else
	{
		if (mas[b] || !b)
		{
			return mas[b];
		}
		else
		{
			return (mas[b] = 1 + function(a, b - gcd(a, b)));
		}
	}
}

int main()
{
	ll x, y;
	scanf("%lld %lld", &x, &y);
	CL(mas, 0);
	printf("%lld", function(x, y));
}


#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    //string a;
    char a[200], b[100], h[200];
    int j, t = 0, i, lena, lenb, s = 0;
    cin >> a;
    //a = 'iyaiuiwioOyzUaOtAeuEYcevvUyveuyioeeueoeiaoeiavizeeoeyYYaaAOuouueaUioueauayoiuuyiuovyOyiyoyioaoyuoyea';
    lena = strlen(a);
    for (i = 0; i<lena; i++)
        {
        for (j = 65; j<=90; j++)
            {
            if ((char)j == a[i])
               a[i] = (char)(j + 32);
            }
        }
    //cout << "a: " << a << endl;
    lenb = lena;
    for (i = 0; i < lena; i++)
        {
        while ((a[s] == 'a') || (a[s] == 'A') || (a[s] == 'o') || (a[s] == 'O') || (a[s] == 'E')
           || (a[s] == 'e') || (a[s] == 'u') || (a[s] == 'U') || (a[s] == 'i') || (a[s] == 'I')
           || (a[s] == 'Y') || (a[s] == 'y'))
           {
           lenb -= 1;
           s += 1;
           }
        //cout << "s:" << s << "i:" << i << endl;
        b[i] = a[s];
        s += 1;
        }
    for (i = lenb; i<101; i++)
        b[i] = ' ';
    s = 0;
    for (i = 0; i<(lenb * 2); i++)
        {
        if (i % 2 == 0)
           h[i] = '.';
        else
           {
           h[i] = b[s];
           s += 1;
           }
        }
    cout << "lenb:" << lenb << endl;
    for (i = lenb * 2; i<202; i++)
        //cout << h[i];
        h[i] = ' ';
    for (i = 0; i < lenb * 2; i++)
        cout << h[i];
    //cout << h << endl;
    getch();
    return 0;

}


#include <iostream>
#include <string>

#define endl "\n"
int main()
{
    std::string s;
    std::cin>>s;
    int len = s.length();
    char c[100],f[200],j=0;
    strcpy(c, s.c_str());
    for(int i = 0; i < len; i++)
    {
        if(c[i] < 96)
          c[i]+=32;
        if (c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o' || c[i] == 'u' || c[i] == 'y')
            continue;
        else
        {
            f[j]='.';j++;
            f[j]=c[i];j++;
        }
    }
    for(int i = 0; i < j; i++)
    {
        std::cout<< f[i];
    }

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

char low(char c){
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else return c;
}

int main()
{
string n;
cin >> n;
string m = "";
for ( int i = 0; i < n.length(); ++i )
{
if ( n[i] == 'a' || n[i] == 'e' || n[i]=='i' || n[i]== 'o' ||
n[i] == 'u' ||
n[i] == 'A' || n[i] == 'E' || n[i] == 'I' || n[i] == 'O' || n[i] == 'U') ;
else
m = m + "." + n[i];

}
for(int i=0;i<m.length();++i)
m[i] = low(m[i]);
cout << m << endl;
return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long ans[11000000];
long long fang[30];
long long n,x,y;
int main(){
	cin>>n>>x>>y;
	fang[1]=1;
	for(int i=2;i<=25;i++)fang[i]=fang[i-1]<<1;
	int wei=1;
	ans[0]=0;
	ans[1]=x;
	for(long long i=2;i<=10000000;i++){
		if(i&fang[wei+1]){
			wei++;
		}
		ans[i]=min(ans[i-fang[wei]]+x,ans[fang[wei+1]-i]+x);
	}
//	for(int i=1;i<=30;i++)printf("a[%d]=%I64d\n",i,ans[i]);
	long long temp=1;
	long long anss=x*n;
	long long ji=0;
//	printf("FXCK\n");
	while(1){
		ji++;
		long long tempans=y*ji+x;
		temp*=(long long)2;
		if(3*temp>=n){
			anss=min(anss,tempans+(n-temp)/temp*x+ans[abs((n-temp)%temp)]);
//			if(anss==3)printf("ji=%d\n",ji);
		}
		else{
			anss=min(anss,tempans+(n-temp)/temp*x+ans[n%temp]);
			anss=min(anss,tempans+((n-temp)/temp+1)*x+ans[(temp-(n%temp))]);

		}
		if(anss==58087)printf("ji=%d\n",ji);
		if(temp>=n)break;
	}
	cout<<anss<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 20000011;
int n, x, y;
long long dp[N];

int main() {
    scanf("%d %d %d", &n, &x, &y);
    for (int i = n; i <= (n << 1); i++){
        dp[i] = 1LL * x * (i - n);
    }
    for (int i = n - 1; i > 0; i--){
        dp[i] = min(dp[i + 1] + x, dp[i << 1] + y);
        int e = i - 1, s = (i >> 1) + 1;

        while(s <= e){
            int mid = (s + e) >> 1;
            long long cost = 1LL * x * (i - mid) + dp[mid << 1] + y;
            if ( cost <= dp[i]){
                dp[i] = cost;
                e = mid - 1;
            } else
                s = mid + 1;
        }
    }
    printf("%lld", dp[1] + x);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn  = 1e7 + 7;
long long dis[maxn];
bool f[maxn];
struct node
{
    int id;
    long long dist;
    node() {}
    node(int id, long long disx): id(id), dist(disx) {}
    inline bool operator < (const node &b) const
    {
        return dist < b.dist;
    }
};

int num;
node a[maxn];
struct pile
{
    pile()
    {
        num = 0;
    }
    void clear()
    {
        num = 0;
    }
    void insert(node x)
    {
        num++;
        a[num] = x;
        int f = num >> 1;
        int now = num;
        while (f)
        {
            if (a[now] < a[f])
                swap(a[now], a[f]);
            else return;
            now = f;
            f = f >> 1;
        }
    }
    node top()
    {
        node ans = a[1];
        a[1] = a[num];
        num--;
        int now = 1;
        while (now * 2 <= num)
        {
            if (a[now] < a[now * 2] && a[now] < a[now * 2 + 1]) return ans;
            int id = now * 2;
            if (a[now * 2 + 1] < a[now * 2]) id++;
            swap(a[now], a[id]);
            now = id;
        }
        return ans;
    }
};
int main()
{
    memset(dis, 0x7F, sizeof(dis));
    int n, xx, y;
    scanf("%d %d %d", &n, &xx, &y);
    pile q;
    dis[0] = 0;
    q.insert(node(0, 0));
    while (num)
    {
        node x = q.top();
        if (f[x.id]) continue;
        f[x.id] = 1;
        if (x.id == n)
        {
            printf("%lld\n", dis[n]);
            return 0;
        }
        if (x.id * 2 <= n && dis[x.id * 2] > dis[x.id] + y)
        {
            dis[x.id * 2] = dis[x.id] + y;
            q.insert(node(x.id * 2, dis[x.id * 2]));
        }
        else if (x.id * 2 > n)
        {
            long long f = dis[x.id] + y + (long long)(x.id * 2 - n) * xx;
            if (f < dis[n])
            {
                dis[n] = f;
                q.insert(node(n, f));
            }
        }
        if (x.id + 1 <= n && dis[x.id + 1] > dis[x.id] + xx)
        {
            dis[x.id + 1] = dis[x.id] + xx;
            q.insert(node(x.id + 1, dis[x.id + 1]));
        }
        if (x.id - 1 >= 0 && dis[x.id - 1] > dis[x.id] + xx)
        {
            dis[x.id - 1] = dis[x.id] + xx;
            q.insert(node(x.id - 1, dis[x.id - 1]));
        }
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include "queue"
#include <deque>
#include <stack>
#include <numeric>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#define MOD1 1000000007
inline void inp( long long int  &n ){n=0;int ch=getchar();int sign=1;while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getchar();}while( ch >= '0' && ch <= '9' )n = (n<<3)+(n<<1) + ch-'0', ch=getchar();n=n*sign;}
#define pi acos(-1)
#define pii pair<int,int>
#define vii vector<pii>
#define ll  long long int
#define loop(i,n) for(ll i=0;i<n;i++)
#define ll  long long int
#define LL long long int
#define loop2(i,n) for(ll i=1;i<=n;i++)
#define MIN(a,b) (a) < (b) ? (a) : (b)
#define MAX(a,b) (a) > (b) ? (a) : (b)
#define ABS(a) (a) > 0 ? (a) : -(a)
#define rep1(i,a,n) for(i=a;i<(n);i++)
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define OUT_RT cerr << (float(clock()) / CLOCKS_PER_SEC) << endl
#define EPS 1e-9
#define ld long double
typedef std::tuple<int, int, int, int> query;
using namespace std;
//JAI SHREE KRISHNA
// Find the holy grail in an  adventure frought with danger
// The bigger the occasion, the better the response.
// Never giving in, fighting to the end
// Look at  the limits
// Check if it is binary search
// Code till the end ofcontest

const int MAXN  = 20000000;
ll cool[MAXN+5];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,x,y;
    cin>>n>>x>>y;
    ll doublelimit;
    doublelimit = 1;
    while (doublelimit<n) {
        doublelimit*=2;
    }

    loop2(i, MAXN)
    {
        cool[i] = i*x;
    }
    for(ll j = 1;j<=8;j+=1)
    {
        for(ll  i = 1;2*i<=doublelimit;i+=1)
        {
            cool[2*i] = min(cool[2*i],cool[i]+y);
        }
        for(ll  i = doublelimit;i>=2;i-=1)
        {
            cool[i-1] =min(cool[i-1],cool[i]+x);
        }
        loop2(i, n)
        {
            cool[i+1]  =min(cool[i+1],cool[i]+x);
        }
    }
    cout<<cool[n]<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;

const ll INF=1e18;

const int mod=1000000007;

ll dp[5000009];
int pri[5000009];
vll d[5000009];

void sieve()
{
    ll i,j,k;
    pri[0]=pri[1]=1;
    for (i=2;i<=sqrt(5000009);++i)
    {
        if (pri[i]==0)
        {
            for (j=2*i;j<5000009;j+=i)
            {
                if ((i*i)<=j)
                    d[j].push_back(i);
                pri[j]=1;
            }
        }
    }
    return;
}

ll fastpow (ll x, ll y)
{
    ll res=1;
    while (y>0)
    {
        if (y&1)
        {
            res=(res*x)%mod;
            y--;
        }
        x=(x*x)%mod;
        y/=2;
    }
    return res;
}

int main()
{
    ll t,n,m,i,j,k,l,r,x,y,z;
    sieve();
    scanf ("%lld %lld %lld",&t,&l,&r);
    dp[1]=0;
    dp[2]=1;
    dp[3]=3;
    for (i=4;i<=r;++i)
    {
        dp[i]=(i*(i-1))/2;
        for (j=0;j<d[i].size();++j)
        {
            k=(i/d[i][j]);
            dp[i]=min(dp[i],(k*dp[d[i][j]]+dp[k])%mod);
        }
    }
    ll ans=0;
    k=1;
    ans=dp[l];
    for (i=l+1;i<=r;++i)
    {
        ans=(ans+((fastpow(t,k)*dp[i])%mod))%mod;
        k++;
    }
    printf ("%lld\n",ans);
	return 0;
}


/*****Enigma_27*****/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define ios        	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll i,j,k,l=0,ans=hell,ind=-1,n,x,m,y,a[5000005]={0},r,t;
vector<ll> v,fact;
void sieve()
{
    for(i=2;i<=5*1e6;i++)
    {
        if(a[i]==0)
        {
            for(j=i;j<=5*1e6;j+=i)
                if(a[j]==0)a[j]=i;
        }
    }
}
ll f(ll x)
{

    if(x==1) return 0;
    ll d=a[x]%hell;
    ll grp=(x/d)%hell;
    return ((grp*((d*(d-1)/2)%hell))%hell)+f(grp)%hell;
}
ll g(ll x)
{

    //cout<<x<<endl;
    if(x==r) return f(r);
    return (f(x)%hell+((t%hell)*g(x+1))%hell)%hell;
}
int main()
{   ios
    cin>>t>>l>>r;
    sieve();
    cout<<g(l)%hell<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,dp[5000002];
set<ll>st,st1;
string s1="one",s2="zero",s,ch="";
vector<ll>v[5000005];
vector< pair<ll,ll> >vv;
vector< pair<long double,ll> > vvA,vvB;
vector< pair< pair <ll,ll>,ll > > vvv;
priority_queue <ll> pq;
ll prime[5000005]={0};

void sieve()
{

	prime[0]=prime[1]=1;
	for(ll i=2;i*i<=5000000;i++)
	{
		if(prime[i])
		continue;
		v[i].push_back(i);
		for(ll j=2*i;j<=5000000;j+=i)
		{
			prime[j]=1;
			v[j].push_back(i);
		}
	}
}

ll solve(ll n){
    if(!n||n==1)return 0;
    ll &ret=dp[n];
    if(ret!=-1)return ret;
    ll ans=5e14;
    for(ll i=0;i<v[n].size();i++){
        ll d=v[n][i];
        ll x=n/d;
        ll res=0;
        if(d==1){
           ans=min(ans,n);
           continue;
        }
        res=x*d*(d-1)/2;
        res+=solve(x);
        ans=min(ans,res);
    }
  return ret=ans;
}

int main(){
In_the_name_of_allah_the_merciful

	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1,t;
	set<  ll > ::iterator it,it1,it2;
	char c;
    //freopen("a.txt","r",stdin);
    cin>>t>>l>>r;
    for(i=1;i<=r+1;i++){
        dp[i]=-1;
    }
    sieve();
    ll res=0;
    ll cnt=1;
    for(i=l;i<=r;i++){
        res+=(cnt*solve(i)%mod);
        res+=mod;
        res%=mod;
        cnt*=t;
        cnt+=mod;
        cnt%=mod;
    }
    cout<<res;
Alhamdulillah
 }


#include <bits/stdc++.h>
using namespace std;
char conver(int a){
	char r;
	r=a+'0';
	return r;
}
int main(){
	int a,b,c,cont2=0,cont5=0;
	cin>>a>>b>>c;
	if(a>b){
		a=a-(a/b)*b;
	}

	int r;
	char decimales[500];
	int f=300,k=1,t=0;int aux=b;
	while(aux%2==0){
		aux=aux/2;
	}
	while(aux%5==0){
		aux=aux/5;
	}
	if(aux>1){
		f=300+max(cont2,cont5);
	}
	int con;
	do{
		con=0;
		while(a<b&&a>0){
			a=a*10;
			con++;
			if(con>1){
				decimales[k]='0';
				k++;
			}
		}
		r=a%b;
		decimales[k]=conver((a-r)/b);
		k++;
		a=r;
	}while(f--);
	for(int i=1;i<=k;i++){
		if(conver(c)==decimales[i]){
			cout<<i<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


/*
 * author: lightinbrain
 * start from: 2019-06-19 18:11:04
 */
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int a, b, c;
  cin >> a >> b >> c;
  int pos = -1;
  vector<bool> num(a + 1, false);
  for (int i = 1; ; ++i) {
    if (num[a]) {
      break;
    }
    num[a] = true;
    a *= 10;
    if (a / b == c) {
      pos = i;
      break;
    }
    a %= b;
  }
  cout << pos << endl;;
  return 0;
}


#include <iostream>
#include<cmath>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    for(int res = 1; res <= 5; res++)
    {
        int t = a*10/b;
        if(t == c)
        {cout << res <<endl; return 1;}
        if(t == 0)
            a*= 10;
        else
            a = (a*10)%b;
    }
    cout << -1 <<endl;

}


#include<bits/stdc++.h>

using namespace std;

// int getbigger(int a, int b){
//   while(a<b) a*=10;
//   return a;
// }

int main(){
  int a, b, c;
  cin>>a>>b>>c;
  int index=1;

  while(1){
    if (a==0) {
      if (c==a) cout<<index<<endl;
      else cout<<-1<<endl;
      break;
    }
    int temp = 10 * a;
    if (temp < b){
      if (c==0) {
        cout<<index<<endl;
        break;
      }
      a = temp;
    }
    else if (temp / b == c) {
      cout<<index<<endl;
      break;
    }
    else if (temp % b == a) {
      cout<<-1<<endl;
      break;
    } else {
      a = temp % b;
    }
    index++;
    // cout<<a<<b<<endl;
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int perfect[]= {1, 6, 28, 248,120,  2016, 496,8128, 32640,  8128};
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i==0)
        {
            v.push_back(i);
            if (n/i!=i)v.push_back(n/i);
        }
    }
    sort(v.begin(), v.end(), greater<int>());
    for (int i=0; i<v.size(); i++)
    {
        for (int j=0; j<10; j++)
        {
            if (perfect[j]==v[i])
            {
                cout<<v[i]<<endl;
                return 0;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) {
        int h, g;
        std::cin >> h >> g;
        int n = (1 << h) - 1;
        std::vector<int> a(n + 1);
        for (int i = 1; i <= n; ++i) std::cin >> a[i];
        long long ans = 0;
        std::vector<int> f(1 << g);
        std::vector<bool> good(n + 1);
        std::function<std::vector<int>(int, int)> solve = [&](int p, int dep) {
            if (dep == h - 1) return std::vector{p};
            auto left = solve(2 * p, dep + 1);
            auto right = solve(2 * p + 1, dep + 1);
            std::vector<int> v(left.size() + right.size());
            std::merge(left.begin(), left.end(), right.begin(), right.end(),
                       v.begin(), [&](int i, int j) { return a[i] < a[j]; });
            v.push_back(p);
            if (dep < g) {
                int lim = dep < g - 1 ? std::max(f[2 * p], f[2 * p + 1]) : 0;
                auto it = v.begin();
                while (a[*it] < lim) ++it;
                f[p] = a[*it];
                ans += f[p];
                good[*it] = true;
                v.erase(v.begin(), it + 1);
            }
            return v;
        };
        solve(1, 0);
        std::cout << ans << "\n";
        for (int i = (1 << h) - 1; i > 0; --i) {
            if (!good[i]) std::cout << i << " ";
        }
        std::cout << "\n";
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}

template<typename T>T ext_gcd(T a,T b,T& x,T& y){
    //ax+by=gcd(a,b)
    if(a<b)return ext_gcd(b,a,y,x);
    if(b==0){
        x=1,y=0;
        return a;
    }
    T gcd_val=ext_gcd(b,a%b,x,y);
    swap(x,y);
    y-=x*(a/b);
    if(x>b)y+=(x/b)*a,x%=b;
    if(y>a)x+=(y/a)*b,y%=a;
    return gcd_val;
}

template<long long int mod,bool prime=false>class modint{
private:
    typedef long long int ll;
    ll val;

    ll gcd(ll a,ll b){
        if(a<b)swap(a,b);
        if(b==0)return a;
        return gcd(b,a%b);
    }
public:
    modint():val(0){}
    template<class T>modint(T a){
        val=(ll)a%mod;
        if(val<0)val+=mod;
    }

    ll value()const{return val;}
    ll get_mod()const{return mod;}

    modint& operator++(){
        val++;
        if(val==mod)val=0;
        return *this;
    }
    modint operator++(int){
        modint ans=*this;
        ++*this;
        return ans;
    }
    modint& operator--(){
        if(val==0)val=mod;
        val--;
        return *this;
    }
    modint operator--(int){
        modint ans=*this;
        --*this;
        return ans;
    }

    modint& operator+=(const modint& a){
        val+=a.value();
        if(val>=mod)val-=mod;
        return *this;
    }
    modint& operator-=(const modint& a){
        val-=a.value();
        if(val<0)val+=mod;
        return *this;
    }
    modint& operator*=(const modint& a){
        val*=a.value();
        if(val>=mod)val%=mod;
        return *this;
    }

    modint pow(ll index)const{
        assert(index>=0);
        if(prime && index>=mod-1)index%=mod-1;
        modint a=*this,ans=1;
        for(ll i=1;i>=0 && i<=index;i<<=1){
            if(index&i)ans*=a;
            a*=a;
        }
        return ans;
    }

    modint inverse()const{
        if(prime){
            assert(val!=0);
            return pow(mod-2);
        }
        ll x,y;
        ll g=ext_gcd<ll>(val,mod,x,y);
        assert(g==1);
        return x;
    }

    modint& operator/=(const modint& a){
        if(prime){
            *this=(*this)*a.inverse();
            return *this;
        }
        ll g=gcd(val,a.value());
        modint a2=a.value()/g;
        val/=g;
        *this=(*this)*a2.inverse();
        return *this;
    }

    friend modint operator-(const modint& a,const modint& b){return (modint)a-=b;}
    friend modint operator+(const modint& a,const modint& b){return (modint)a+=b;}
    friend modint operator*(const modint& a,const modint& b){return (modint)a*=b;}
    friend modint operator/(const modint& a,const modint& b){return (modint)a/=b;}
    friend bool operator==(const modint& a,const modint& b){return a.value()==b.value();}
    friend bool operator!=(const modint& a,const modint& b){return a.value()!=b.value();}

    friend modint pow(const modint& a,const ll b){return a.pow(b);}

    modint operator+() const{return *this;}
    modint operator-() const{return modint()-*this;}

    friend ostream& operator<<(ostream& os,const modint& a){
        os<<a.value();
        return os;
    }
    friend istream& operator>>(istream& is,modint& a){
        ll val;
        is>>val;
        a=val;
        return is;
    }
};

using modint107=modint<1'000'000'007,true>;
using modint109=modint<1'000'000'009,true>;
using mint=modint<998'244'353,true>;

namespace sol{

    mint calc(int n){
        if(n==0)return 1;
        mint a,b,c;
        int i;
        a=1;
        for(i=0;i<n-1;i++){
            b=a+a*(a-1)/2;
            b+=a;
            b++;
            a=b;
        }
        b=0;
        b+=a+a*(a-1)+a*(a-1)*(a-2)/6;
        b+=a+a*(a-1)/2;
        b+=a;
        b++;
        return b;
    }

    mint calc2(int n){
        vector<mint> v1={0};
        mint a,b,c;
        int i;
        a=1;
        for(i=0;i<n-1;i++){
            b=a+a*(a-1)/2;
            v1.push_back(b);
            b+=a;
            b++;
            a=b;
        }
        c=0,b=0;
        for(i=1;i<n;i++){
            b+=v1[n-i-1]*(v1[i]-v1[i-1]);
        }
        return b;
    }

    void solve(){
        int n,m;
        int i,j,k;
        int a,b,c;
        cin>>n;
        mint s=calc(n)-calc(n-1);
        s=s*2-1;
        s+=calc2(n);
        cout<<s<<endl;
    }
}

int main(){
    sol::solve();
}


//CLOCKS_PER_SEC
using namespace std;
#include<bits/stdc++.h>
#define sqr(x) 1ll*(x)*(x)
//#define sort stable_sort
#define ll long long
#define mk make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mk(mk(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define titose CLOCKS_PER_SEC
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt");
#define fast ios_base::sync_with_stdio(false);
inline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void readc(char &x){char c;while (((c=getchar())==' ')||c=='\n');x=c;}
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define si set<int>
//#define sl set<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define msi map<string,int>
#define msl map<string,ll>
#define piii pair<int,pii >
#define piipi pair<pii,int>
#define plll pair<ll,pll >
#define pllpl pair<pll,ll>
#define pqi priority_queue<int>
#define pql priority_queue<ll>
#define npqi priority_queue<int,vector<int>,greater<int> >
#define npql priority_queue<ll,vector<ll>,greater<ll> >
#define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)
#define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)
#define rep(i,x) forup ((i),1,(x))
#define repd(i,x) fordo ((i),(x),1)
#define rep0(i,x) forup ((i),0,((int)(x))-1)
#define rep0d(i,x) fordo ((i),((int)(x))-1,0)
#define itr iterator
#define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)
#define NO {cout<<"NO";return 0;}
#define YES {cout<<"YES";return 0;}
#define y0 y000000000000000000000000000
#define y1 y111111111111111111111111111
#define j0 j000000000000000000000000000
#define j1 j111111111111111111111111111
#define cl0(a) memset((a),(0),(sizeof((a))))
#define clz(a) memset((a),(0x16),(sizeof((a))))
#define clf(a) memset((a),(-(0x16)),(sizeof((a))))
#define inf 0x3bbbbbbb
#define lnf 0x2bbbbbbbbbbbbbbbll
//#define sqrt divi
#define p2(i) (1ll<<(i))
#define readi read
#define readll read
/*************************************************/
const int mod=998244353;
const int maxn=2097152;
int n,m,i,j,f[1000005],s[10000005],ans;
int fac[maxn+5],inv[maxn+5],fi[maxn+5],rev[maxn+5],a[maxn+5],w[maxn+5];
int pw(int x,int y)
{
	int z=1;
	while(y){
		if(y&1)z=1ll*z*x%mod;
		x=1ll*x*x%mod;y>>=1;
	}
	return z;
}
void init()
{
	fac[0]=fac[1]=fi[0]=fi[1]=inv[1]=w[0]=1;w[1]=pw(3,(mod-1)/maxn);
	int i;
	forup(i,2,maxn){
		fac[i]=1ll*fac[i-1]*i%mod;
		inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
		fi[i]=1ll*fi[i-1]*inv[i]%mod;
		w[i]=1ll*w[i-1]*w[1]%mod;
	}
}
int getrev(int x)
{
	int len=1,i;
	while(len<=x){
		len<<=1;
	}
	rep0(i,len){
		rev[i]=(rev[i/2]/2)+((len/2)*(i&1));
	}
	return len;
}
void ntt(int *a,int len,int op)
{
	int i,j,k;
	rep0(i,len)if(rev[i]<i) swap(a[i],a[rev[i]]);
	for(i=1;i<len;i<<=1){
		int ys=maxn/i/2;
		for(j=0;j<len;j+=i+i){
			int t=(op==1?0:maxn);
			rep0(k,i){
				int x=a[j+k],y=a[j+k+i]*1ll*w[t]%mod;
				a[j+k]=(x+y)%mod;a[j+k+i]=(x-y+mod)%mod;
				t+=op*ys;
			}
		}
	}
	if(op==-1){
		rep0(i,len) a[i]=1ll*a[i]*inv[len]%mod;
	}
}
int c2(int x)
{
	return 1ll*x*(x+1)/2%mod;
}
int c3(int x)
{
	return 1ll*x*(x+2)%mod*(x+1)%mod*166374059%mod;
}
int main()
{
	init();
	f[0]=f[1]=s[0]=1;s[1]=2;
	read(n);n++;
	for(i=2;i<=n;i++){
		f[i]=(1ll*f[i-1]*s[i-2]+c2(f[i-1]))%mod;
		s[i]=(f[i]+s[i-1])%mod;
	}
	int res=0;
	for(i=2;i<=n;i++){
		int t1=f[i-1],t2=(s[i-2]-1+mod)%mod;
		a[i]=(c2(t1)+1ll*t1*t2)%mod;
//		if(i+i<=n){
//			res=(res+1ll*a[i]*a[i])%mod;
//		}
	}
	int len=getrev(n+n+2);
	ntt(a,len,1);
	rep0(i,len)a[i]=1ll*a[i]*a[i]%mod;
	ntt(a,len,-1);
	for(i=4;i<=n;i++){
		res=(res+a[i])%mod;
	}
//	res=1ll*res*(mod+1)/2%mod;
	int t1=f[n-1],t2=s[n-2];
	cout<<(((c3(t1)+1ll*t1*c2(t2)+1ll*c2(t1)*t2)%mod*2ll-1+mod)%mod+res)%mod<<endl;
	return 0;
}


#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#define MOD 998244353

namespace NTT {
    int mbase,base,root;
    int w[1 << 22];
    int rev[1 << 22];
    int modPow(int b,int e) {
        int r = 1;
        while (e > 0) {
            if (e & 1) r = ((LLI) r*b) % MOD;
            e >>= 1;
            b = ((LLI) b*b) % MOD;
        }
        return r;
    }
    int setBase(int nbase) {
        int i,j;
        mbase = 1;
        while (!(MOD & (1 << mbase))) mbase++;
        root = 2;
        while ((modPow(root,1 << mbase) != 1) || (modPow(root,1 << (mbase-1)) == 1)) root++;
        base = nbase;
        for (i = 0; i < (1 << base); i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (base-1));
        w[0] = 0,w[1] = 1;
        for (i = 1; i < nbase; i++) {
            int z = modPow(root,1 << (mbase-i-1));
            for (j = (1 << (i-1)); j < (1 << i); j++) w[j << 1] = w[j],w[(j << 1)|1] = ((LLI) w[j]*z) % MOD;
        }
        return 0;
    }
    int FFT(vector<int> &a,int inv) {
        int i,j,k;
        int l = 0;
        while ((1 << l) < a.size()) l++;
        int s = base - l;
        for (i = 0; i < a.size(); i++) {
            if (i < (rev[i] >> s)) swap(a[i],a[rev[i] >> s]);
        }
        for (k = 1; k < a.size(); k <<= 1) {
            for (i = 0; i < a.size(); i += (k << 1)) {
                for (j = 0; j < k; j++) {
                    int z = ((LLI) a[i+j+k]*w[j+k]) % MOD;
                    a[i+j+k] = a[i+j]-z,a[i+j] += z;
                    if (a[i+j+k] < 0) a[i+j+k] += MOD;
                    if (a[i+j] >= MOD) a[i+j] -= MOD;
                }
            }
        }
        return 0;
    }
    vector<int> multiply(vector<int> A,vector<int> B) {
        int i,n = 1;
        while (n < A.size()+B.size()-1) n <<= 1;
        vector<int> a(n,0),b(n,0);
        for (i = 0; i < A.size(); i++) a[i] = A[i];
        for (i = 0; i < B.size(); i++) b[i] = B[i];
        FFT(a,0),FFT(b,0);
        int x = modPow(n,MOD-2);
        for (i = 0; i < n; i++) a[i] = ((((LLI) a[i]*b[i]) % MOD)*x) % MOD;
        reverse(a.begin()+1,a.end()),FFT(a,1),a.resize(A.size()+B.size()-1);
        return a;
    }
}

int inv(int n) {
    int e = MOD-2,r = 1;
    while (e > 0) {
        if (e & 1) r = ((LLI) r*n) % MOD;
        e >>= 1;
        n = ((LLI) n*n) % MOD;
    }
    return r;
}
int INV2 = inv(2);
int INV6 = inv(6);
LLI dp[1000001];
int main() {
    int n;
    cin >> n;

    int i;
    dp[0] = 1;
    for (i = 1; i <= n; i++) {
        dp[i] = dp[i-1]+1;
        dp[i] += (((dp[i-1]*(dp[i-1]+1)) % MOD)*INV2) % MOD;
        dp[i] %= MOD;
    }

    LLI ans1 = dp[n-1];
    ans1 += (((dp[n-1]*(dp[n-1]+1)) % MOD)*INV2) % MOD;
    ans1 += (((((dp[n-1]*(dp[n-1]+1)) % MOD)*(dp[n-1]+2)) % MOD)*INV6) % MOD;
    ans1 %= MOD;
    ans1 = 2*ans1+MOD-1,ans1 %= MOD;
    n--;
    LLI ans2 = dp[n-1];
    ans2 += (((dp[n-1]*(dp[n-1]+1)) % MOD)*INV2) % MOD;
    ans2 += (((((dp[n-1]*(dp[n-1]+1)) % MOD)*(dp[n-1]+2)) % MOD)*INV6) % MOD;
    ans2 %= MOD;
    ans2 = 2*ans2+MOD,ans2 %= MOD;
    LLI ans = (ans1-ans2+MOD) % MOD;
    n++;
    vi a(n+1);
    for (i = 1; i <= n; i++) a[i] = (((dp[i-1]*(dp[i-1]+1)) % MOD)*INV2) % MOD;
    for (i = n; i >= 1; i--) a[i] = (a[i]-a[i-1]+MOD) % MOD;
    NTT::setBase(22);
    vi p = NTT::multiply(a,a);
    if (n >= 3) {
        for (i = 1; i < n-1; i++) ans += p[n-i],ans %= MOD;
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ll n;
	cin>>n;

	  if(n==1)
	   cout<<"1"<<"\n";
	   else if(n==2)
	   cout<<"5"<<"\n";
	   else
	   {
	   		vector<ll>v;
	   		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
	  for(ll i=3;i<=100;i++)
	  {
	  	v[i]=v[i-1]+2;
	  }
	  ll sum=v[n-1]*v[n-1];
	   	cout<<sum+4<<"\n";
	   }
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<string>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define all(v)   v.begin(),v.end()
typedef long long int ll;

int OrdrOfRhombus(int n)
{
	if(n==1)
		return 1;
	else
		return OrdrOfRhombus(n-1)+pow(2,n);
}

void solve()
{
	int n; cin>>n;

	int order = OrdrOfRhombus(n);

	cout<<order<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
       cin.tie(NULL);
          cout.tie(0);
          int t;cin>>t;
          while(t--)
     solve();

     return 0;
 }


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        unsigned long long int iter = 2;
        cin >> n;
        vector<long> arr;
        arr.push_back(1);

        for(int i = 1; i < n; i++)
        {
			long value = arr[i-1] + (iter * 2);
            arr.push_back(value);
			iter+=2;
        }
        cout << arr[n-1] << endl;
    }

}


#include<iostream>

int n,f[100000000];
int qqq(int a){
	if(a==1) return f[a]=1;
	f[a]=qqq(a-1)+4*a-4;
	return f[a];
}
int main(){
	std::cin>>n;
	std::cout<<qqq(n);

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 200 + 10;
int T,n,m;
char s[maxn], name;
struct node {
    int B, G, R;
    node(int b=0, int g=0, int r=0):B(b),G(g),R(r) {}
}S;
map<int, int> p;
void BFS() {
    queue<node> q;
    q.push(S);
    p.clear();
    int cnt = 0;
    while(!q.empty()) {
        node u = q.front(); q.pop();
      //  printf("%d %d %d\n",u.B,u.G,u.R);
        if(u.B == 1 && u.G == 0 && u.R == 0) {
            if(p.count(int('B'-'A'))) ;
            else {
                p[int('B'-'A')] = 1; ++cnt;
            }
            continue;
        }
        else if(u.G == 1 && u.B == 0 && u.R == 0) {
            if(p.count((int)('G'-'A'))) ;
            else {
                p[((int)('G'-'A'))] = 1;++cnt;
            }continue;
        }
        else if(u.R == 1 && u.B == 0 && u.G == 0) {
            if(p.count((int)('R'-'A'))) ;
            else { p[((int)('R'-'A'))] = 1;++cnt;} continue;
        }
        if(cnt == 3) return ;
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                if(i == j) {
                    if(i == 0 && u.B >= 2) q.push(node(u.B-1,u.G,u.R));
                    else if(i == 1 && u.G >= 2) q.push(node(u.B,u.G-1,u.R));
                    else if(i == 2 && u.R >= 2) q.push(node(u.B, u.G, u.R-1));
                }
                else {
                    if(i == 0 && j == 1 && u.B >= 1 && u.G >= 1) q.push(node(u.B-1,u.G-1,u.R+1));
                    else if(i==0&&j==2&&u.B>=1&&u.R>=1) q.push(node(u.B-1,u.G+1,u.R-1));
                    else if(i==1&&j==2&&u.G>=1&&u.R>=1) q.push(node(u.B+1,u.G-1,u.R-1));
                }
            }
        }
    }
}

int main() {
    scanf("%d%s",&n,s);
    S = node(0, 0, 0);
    for(int i=0;i<n;i++) {
        if(s[i] == 'B') S.B++;
        else if(s[i] == 'R') S.R++;
        else S.G++;
    }
    if(S.B > 2 && S.G > 2 && S.R > 2) {
        printf("BGR\n"); return 0;
    }
    BFS();
    for(int i=0;i<3;i++) {
        if(i == 0) {
            if(p.count('B'-'A')) printf("%c",'B');
        }
        else if(i == 1) {
            if(p.count('G'-'A')) printf("%c",'G');
        }
        else if(i == 2) {
            if(p.count('R'-'A')) printf("%c",'R');
        }
    }
    printf("\n");
    return 0;
}


/** coder Mohammad Al-Hussein *_^ */
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define mp make_pair
#define mod  1000000007
using namespace std;
int R,G,B;
int dp[201][201][201];
vector <char> v;
void solve (int r,int g,int b)
{
    if(r+g+b == 1)
    {
        if(r) v.push_back('R');
        if(g) v.push_back('G');
        if(b) v.push_back('B');
        return ;
    }
    if(r > 1) solve(r-1,g,b);
    if(g > 1) solve(r,g-1,b);
    if(b > 1) solve(r,g,b-1);

    if(r > 0 && g > 0) solve(r-1,g-1,b+1);
    if(r > 0 && b > 0) solve(r-1,g+1,b-1);
    if(g > 0 && b > 0) solve(r+1,g-1,b-1);
}

int main()
{
    //memset(dp,-1,sizeof(dp));
    int n;
    cin >> n;
    for(int i=0; i<n; i++)
    {
        char x;
        cin >> x;
        R += (x == 'R');
        G += (x == 'G');
        B += (x == 'B');
    }
    solve(R,G,B);
    for(int i=0;i<v.size();i++)
        cout << v[i];
    return 0;
}

//15 5
//1 2 3 4 5 -1 -3 -4 -5 50 -6 9 -7 4 46


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

struct sadek
{
    short p;
    short q;
    short r;
};
bool a1,a2,a3;
int n,i;
int x,y,z;
queue<sadek>qu;
string s;
int main()
{
    cin>>n>>s;
    for(i=0;i<n;i++)
    {
        if(s[i]=='B')
        x++;
        else if(s[i]=='G')
        y++;
        else
        z++;
    }
    sadek xx;
    xx.p=x;
    xx.q=y;
    xx.r=z;
    qu.push(xx);
    while(!qu.empty())
    {
        sadek pp=qu.front();
        qu.pop();
        if(pp.p>1)
        {
            sadek qq;
            qq.p=pp.p-1;
            qq.q=pp.q;
            qq.r=pp.r;
            qu.push(qq);
        }
        if(pp.q>1)
        {
            sadek qq;
            qq.p=pp.p;
            qq.q=pp.q-1;
            qq.r=pp.r;
            qu.push(qq);
        }
        if(pp.r>1)
        {
            sadek qq;
            qq.p=pp.p;
            qq.q=pp.q;
            qq.r=pp.r-1;
            qu.push(qq);
        }
        if(pp.p>0 && pp.q>0)
        {
            sadek qq;
            qq.p=pp.p-1;
            qq.q=pp.q-1;
            qq.r=pp.r+1;
            qu.push(qq);
        }
        if(pp.p>0 && pp.r>0)
        {
            sadek qq;
            qq.p=pp.p-1;
            qq.q=pp.q+1;
            qq.r=pp.r-1;
            qu.push(qq);
        }
        if(pp.q>0 && pp.r>0)
        {
            sadek qq;
            qq.p=pp.p+1;
            qq.q=pp.q-1;
            qq.r=pp.r-1;
            qu.push(qq);
        }
        if(pp.p==0 && pp.q==0)
        a3=true;
        else if(pp.p==0 && pp.r==0)
        a2=true;
        else if(pp.q==0 && pp.r==0)
        a1=true;
    }
    if(a1)
    putchar('B');
    if(a2)
    putchar('G');
    if(a3)
    putchar('R');
    cout<<endl;
}


//#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include<math.h>
#include<set>
#include<string>
#include<cstring>
#include<queue>
#include<complex>
#include<assert.h>
#include<math.h>
#include<memory.h>
#include<sstream>
#include<cstdio>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define repi(i,a,n) for(int i=a;i<n;i++)
#define INF (0x7fffffff)
#define mp make_pair
#define fastIO ios::sync_with_stdio(false)
#define read freopen("in.in","r",stdin)
#define write freopen("out.out","w",stdout)
#define scf(xx) scanf("%d",&xx)
#define All(a) a.begin(), a.end()

typedef long long ll;
typedef vector<int> vi;
typedef pair<ll, ll> ii;
typedef long double ld;

int main(){
	//read; write;

	char c;; int n;
	cin >> n;
	vi v(3, 0);
	rep(i, n){
		cin >> c;
		if (c == 'R')v[0]++;
		else if (c == 'G')v[1]++;
		else v[2]++;
	}
	if (v[0] && v[1] && v[2]) cout << "BGR";
	else if (n == 2){
		if (v[0] && v[1]) cout << "B";
		else if (v[0] && v[2])cout << "G";
		else if (v[2] && v[1])cout << "R";
		else if (v[0])cout << "R";
		else if (v[1])cout << "G";
		else cout << "B";
	}
	else if (v[0] && v[1]) {
		if (v[0]>1 && v[1]>1)cout << "BGR";
		else if (v[0] > 1)cout << "BG";
		else cout << "BR";
	}
	else if (v[0] && v[2]) {
		if (v[0]>1 && v[2]>1)cout << "BGR";
		else if (v[0] > 1)cout << "BG";
		else cout << "GR";
	}
	else if (v[1] && v[2]) {
		if (v[1]>1 && v[2]>1)cout << "BGR";
		else if (v[1] > 1)cout << "BGR";
		else cout << "GR";
	}

	else if (v[0])cout << "R";
	else if (v[1])cout << "G";
	else cout << "B";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int p[maxn];
int f[maxn];
int cnt;
void init(){
    int i,j;
    cnt=0;
    for(i=2;i<maxn;i++){
        if(p[i]) continue;
        f[++cnt]=i;
        for(j=i*2;j<maxn;j+=i){
            p[j]=1;
        }
    }
}
vector<int>v[maxn];
int main(){
    int i,j;
    int k;
    ios::sync_with_stdio(0);
    init();
    for(i=2;i<maxn;i++){
        for(j=1;j<=cnt;j++){
            if(j>=i) break;
            v[i].push_back(j);
        }
    }
    return 0;
}


#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 505
#define MAX 1000010//��MAX��Χ�ڵ�����
using namespace std;
long long su[MAX],cnt;
bool isprime[MAX];
void prime()
{
    cnt=1;
    memset(isprime,1,sizeof(isprime));//��ʼ����Ϊ��������Ϊ����
    isprime[0]=isprime[1]=0;//0��1��������
    for(long long i=2;i<=MAX;i++)
    {
        if(isprime[i])//��������
        {
            su[cnt++]=i;
        }
        for(long long j=i*2;j<=MAX;j+=i)//�����ı�����Ϊ����
        {
            isprime[j]=0;
        }
    }
}

int main()
{
    int n;
    prime();
    scanf("%d", &n);
    int maxx = 0, m =  MAX;
    for(int i = 1; su[i] < n; ++i)
    {
        if(n % su[i] == 0)
            maxx = su[i];
    }
    for(int i = maxx * (n / maxx - 1) + 1; i <= n; ++i)
    {
        int f = MAX;
        for(int j = 2; j * j < i; ++j)
            if(i % j == 0)
            {
                if(isprime[j] == 1)
                if((i / j - 1) * j < i)
                {
                    if(f > (i / j - 1) * j + 1)
                    f = (i / j - 1) * j + 1;
                }
                int k = i / j;
                if(isprime[k] == 1)
                if((i / k - 1) * k < i)
                {
                    if(f > (i / k - 1) * k + 1)
                    f = (i / k - 1) * k + 1;
                }
            }
        if(f < m)
            m = f;
    }
    printf("%d", m);
    return 0;
}


#include <bits/stdc++.h>
 #define int long long
#define F first.first
#define S first.second
#define N second
using namespace std;

const int nmax=1001000;

main()
{
    int n;
    cin>>n;
    vector <bool> prime;
    prime.resize(n+10);
    for (int i=0; i<=n; i++)
        prime[i]=true;
    prime[0] = false;
    prime[1] = false;
    for (int i=2; i<=n; ++i)
        if (prime[i])
            if (i * i <= n)
                for (int j=i*i; j<=n; j+=i)
                {
                    prime[j] = false;
                }
   // for (int i=1; i<=n; ++i)
   //     if(prime[i])
   //         cout<<i<<prime[i]<<"\n";
    int j;
    for (j=1; j<(sqrt(n))+1; j++)
        if ((n%j==0) and (prime[n/j]))
            break;
    if (!((n%j==0) and (prime[n/j])))
    for (j=round(sqrt(n)+1); j>=1; j--)
    {
        //cout<<"lol";
        if ((n%j==0) and (prime[j]))
            break;
    }
    else j=n/j;
    j=n-j+1;
    //cout<<j;
    int mn=1000000000;
    int i;
    for (i=j; ((i<=n) & ((!prime[i/2]) or (i%2!=0))); i++)
    if (!prime[i])
    {
        for (int q=1; q<sqrt(i)+1; q++)
            if ((i%q==0) and (prime[i/q]==true))
                mn=min(mn,i-i/q+1);
        for (int q=1; q<sqrt(i)+1; q++)
            if ((i%q==0) and (prime[q]==true))
                mn=min(mn,i-q+1);
    }
    if ((i%2==0) and (prime[i/2]))
        mn=min(mn,i/2+1);
    cout<<mn<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int prime[1100000];
vector<int> v;
void init()
{
	prime[1] = 1;
	for (ll i = 2; i <= 1000000; i++)
	{
		if (!prime[i])
		{
			for (ll j = i*i; j <= 1000000; j += i)
			{
				prime[j] = 1;
			}
			v.push_back(i);
		}
	}
}
int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	ll x;
	cin>>x;
	ll orgx = x;
	init();
	vector<int> minim;
	for (int i = 0; i < v.size(); i++)
	{
		if (x == 1)
			break;
		if (x % v[i] == 0)
		{
			minim.push_back(v[i]);
			while(x % v[i] == 0)
				x /= v[i];
		}
	}

	int l = orgx-minim[minim.size()-1]+1, r = orgx-1;
	int minim1 = l;
	for (int i = l; i <= r; i++)
	{
		ll curr = i;
		for (int j = 2; j <= i; j++)
		{
			if (j*j > i)
				break;
			if (!prime[j] and curr%j == 0)
			{
				minim1 = min(minim1, i-j+1);
				int div = curr/j;
				while (curr % j == 0)
					curr /= j;
				if (prime[div] == 0)
				{
					minim1 = min(minim1, i-div+1);
				}
			}
		}
	}
	cout<<minim1<<"\n";
}


#include <iostream>
#include <fstream>
#include <sstream>

#include <utility>
#include <cassert>
#include <cctype>
#include <functional>
#include <iomanip>
#include <limits>
#include <ctime>

#include <algorithm>
#include <numeric>
#include <cmath>

#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>

using namespace std;

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int tmp;
    map<int, int> m;
    for (int i = 0; i < 6; i++) {
        cin >> tmp;
        if (m.count(tmp) == 0) {
            m[tmp] = 1;
        } else {
            m[tmp]++;
        }
    }

    int el;
    int cnt;
    bool ok = false;
    for (auto e : m) {
        if (e.second >= 4) {
            el = e.first;
            cnt = e.second;
            ok = true;
            break;
        }
    }

    if (!ok) {
        cout << "Alien" << endl;
        return 0;
    }

    int a = -1, b = -1;
    for (auto e : m) {
        if (e.first != el) {
            if (a == -1 && e.second == 1) {
                a = e.first;
                continue;
            }
            if (b == -1 && e.second == 1) {
                b = e.first;
                continue;
            }
            if (e.second == 2) {
                a = b = e.first;
                continue;
            }
        }
    }

    switch (cnt) {
    case 4:
        if (a == b) {
            cout << "Elephant" << endl;
        } else {
            cout << "Bear" << endl;
        }
        break;
    case 5:
        if (a < el) {
            cout << "Bear" << endl;
        } else {
            cout << "Alien" << endl;
        }
        break;
    case 6:
        cout << "Elephant" << endl;
    }

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include<queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<unordered_map>
#include<string>
#include<string.h>
#pragma warning(disable: 4996)
using namespace std;
typedef long long ll;
int main()
{
    int n;

        map<int,int> m;
        for(int i=0;i<6;i++)
        {
            int tmp;
            cin>>tmp;
            m[tmp]++;
        }
        bool legs=false;
        for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
        {
            if(it->second>=4)
                legs=true;
        }
        if(legs&&m.size()<=2)
            cout<<"Elephant\n";
        else if(legs&&m.size()==3)
            cout<<"Bear\n";
        else
            cout<<"Alien\n";
    return 0;
}


#include <iostream>

using namespace std;

int a[111], x, y, X, z;

int main ()
{
    for (int i = 1; i <= 6; ++ i){
        cin >> X;
        a[X] ++;
    }

    for (int i = 0; i <= 10; ++ i)
    {
        if (a[i] == 4)
            x ++;
        if (a[i] == 2)
            y ++;
        if (a[i] == 6)
            z ++;
    }
    if (y == 1 && x == 1 || z == 1)
        cout << "Elephant";
    else if (x == 1)
        cout << "Bear";
    else
        cout << "Alien";
return 0;
}


#include<iostream>
#include<sstream>
#include<string>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<set>
#include<bitset>
#include<algorithm>
#include<list>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>

using namespace std;
#define print1(a)    cout<<a<<endl
#define print2(a,b) cout<<a<<" "<<b<<endl
#define print3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define oo          (1<<30)
#define PI          3.141592653589793
#define pi          (2.0*acos(0.0))
#define ERR         1e-5
#define PRE         1e-8
#define SZ(s)       ((int)s.size())
#define LL          long long
#define ISS         istringstream
#define OSS         ostringstream
#define VS          vector<string>
#define VI          vector<int>
#define VD          vector<double>
#define VLL         vector<long long>
#define SII         set<int>::iterator
#define SI          set<int>
#define mem(a,b)    memset(a,b,sizeof(a))
#define fr(i,a,b)   for(i=a;i<=b;i++)
#define frn(i,a,b)  for(i=a;i>=b;i--)
#define fri(a,b)    for(i=a;i<=b;i++)
#define frin(a,b)   for(i=a;i>=b;i--)
#define frj(a,b)    for(j=a;j<=b;j++)
#define frjn(a,b)   for(j=a;j>=b;j--)
#define frk(a,b)    for(k=a;k<=b;k++)
#define frkn(a,b)   for(k=a;k>=b;k--)
#define frl(a,b)    for(l=a;l<=b;l++)
#define frln(a,b)   for(l=a;l>=b;l--)
#define REP(i,n)    for(i=0;i<n;i++)
#define EQ(a,b)     (fabs(a-b)<ERR)
#define all(a,b,c)  for(int I=0;I<b;I++)    a[I] = c
#define CROSS(a,b,c,d) ((b.x-a.x)*(d.y-c.y)-(d.x-c.x)*(b.y-a.y))
#define sqr(a)      ((a)*(a))
#define FORE(i,a)   for(typeof((a).begin())i=(a).begin();i!=(a).end();i++)
#define typing(j,b) typeof((b).begin()) j=(b).begin();
#define BE(a)       a.begin(),a.end()
#define rev(a)      reverse(BE(a));
#define sorta(a)    sort(BE(a))
#define pb          push_back
#define popb        pop_back
#define mp          make_pair
#define round(i,a)  i = ( a < 0 ) ? a - 0.5 : a + 0.5;
#define makeint(n,s)  istringstream(s)>>n
#define inpow(a,x,y) int i; a=x;fri(2,y)  a*=x
#define cntbit(mask) __builtin_popcountll(mask)
//0 based print
#define debug_array(a,n) for(int i=0;i<n;i++) cerr<<a[i]<<" "; cerr<<endl;
#define debug_matrix(mat,row,col) for(int i=0;i<row;i++) {for(int j=0;j<col;j++) cerr<<mat[i][j]<<" ";cerr<<endl;}

#define csprnt printf("Case %d: ", ++cas);
#define mod         1000000007
#define eulerconstant 0.5772156649

template<class T1> void debug(T1 e){cout<<e<<endl;}
template<class T1,class T2> void debug(T1 e1,T2 e2){cout<<e1<<"\t"<<e2<<endl;}
template<class T1,class T2,class T3> void debug(T1 e1,T2 e2,T3 e3){cout<<e1<<"\t"<<e2<<"\t"<<e3<<endl;}
template<class T1,class T2,class T3,class T4> void debug(T1 e1,T2 e2,T3 e3,T4 e4){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<endl;}
template<class T1,class T2,class T3,class T4,class T5> void debug(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<endl;}
template<class T1,class T2,class T3,class T4,class T5,class T6> void debug(T1 e1,T2 e2,T3 e3,T4 e4,T5 e5,T6 e6){cout<<e1<<"\t"<<e2<<"\t"<<e3<<"\t"<<e4<<"\t"<<e5<<"\t"<<e6<<endl;}
template<class T> void debug(vector< vector<T> > e,int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;} cout<<endl;}
template<class T> void debug(vector< basic_string<T> > e,int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j];cout<<endl;} cout<<endl;}
template<class T> void debug(T e[110][110],int row,int col){int i,j;REP(i,row) {REP(j,col) cout<<e[i][j]<<" ";cout<<endl;}}
template<class T> string toString(T n){ostringstream oss;oss<<n;oss.flush();return oss.str();}
int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
bool isVowel(char ch){ch=tolower(ch);if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')return true;return false;}
bool isUpper(char c){return c>='A' && c<='Z';}
bool isLower(char c){return c>='a' && c<='z';}
/***************************************************code starts from here*************************************************************************************************/

int main()
{
    int i;
    int type1,type2,t1,t2,t3;
    type1=-1;
    int n;
    int type3=0;
    int flag=0;
    type2=-1;
    t1=0;
    t2=0;
    t3=0;
    int diff=0;
    int arr[100];
    mem(arr,0);
    fr(i,1,6)
    {
        scanf("%d",&n);
        if(arr[n]==0)
        {
            diff++;
            arr[n]=1;
        }
    }
    if(diff==2 || diff==1)
    {
        printf("Elephant\n");
    }
    else if(diff==3)
    {
        printf("Bear\n");
    }
    else if(diff>3)
    {
        printf("Alien\n");
    }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	int y=abs(b-a);
	int z=1;
	int c=0;
	if(y%2==0)
	{
	for (int i = 0; i < y/2; i++)
	{
		c=c+(z*2);
		z++;

	}
	}
	else if(y%2==1)
	{

	for (int i = 0; i < (y/2); i++)
	{
		c=c+(z*2);
		z++;

	}
	c=c+z;

	}
	cout<<c<<endl;
	system("pause");

	return 0;
}


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

long long k,n,s,p;

long long cl(long long a, long long b){
    return (a + b - 1)/b;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> k >> n >> s >> p;
    cout << cl(k * cl(n,s), p) << '\n';

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int a, b;
	cin >> a >> b;
	int max, min;
	if (a > b) { max = a; min = b; }
	else { max = b; min = a; }

	int tired1 = 0; int tired2 = 0; int i = 1; int j = 1;
	do {
		max--; tired1 = tired1 + i; i++;
		if ((max - min) != 0) { min++; tired2 = tired2 + j; j++; }
		else { cout << tired1 + tired2;
		return tired1 + tired2; };
	} while ((max - min) != 0);

	cout << tired1 + tired2;
	return tired1 + tired2;

	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, dif, k, ans = 0;
    cin >> a >> b;
    dif = abs(a - b);
    if(dif == 1)
    {
        cout << 1 << endl;
        return 0;
    }
    if(dif == 2)
    {
        cout << 2 << endl;
        return 0;
    }
    if(dif % 2 == 0)
    {
        k = dif / 2;
        for(int i = 1; i <= k; i++)
        {
            ans += i;
            cout << ans <<endl;
        }
        return 0;
    }
    if(dif % 2 != 0)
    {
        dif /= 2;
        for(int i = 1; i <= dif; i++)
        {
            ans += i;
        }
        for(int j = 1; j <= (dif + 1); j++)
        {
            ans += j;
        }
        cout << ans << endl;
        return 0;
    }

}


// A. Бросок кубика
#include <cstdio>
#define loop(i,a,b) for(int i=a;i<b;++i)
int a,b;
int main ()
{
    scanf("%d%d", &a, &b);
    if ( b > a )
        a = b;
    else if ( a == 1 )
        printf("1/1");
    else if ( a == 2 )
        printf("5/6");
    else if ( a == 3 )
        printf("2/3");
    else if ( a == 4 )
        printf("1/2");
    else if ( a == 5 )
        printf("1/3");
    else if ( a == 6 )
        printf("1/6");
    return 0;
}


// A. Бросок кубика
#include <cstdio>
#define loop(i,a,b) for(int i=a;i<b;++i)
int a,b;
int main ()
{
    scanf("%d%d", &a, &b);
    if ( b > a )
        a = b;
    else if ( a == 1 )
        printf("1/1");
    else if ( a == 2 )
        printf("5/6");
    else if ( a == 3 )
        printf("2/3");
    else if ( a == 4 )
        printf("1/2");
    else if ( a == 5 )
        printf("1/3");
    else
        printf("1/6");
    return 0;
}


#include <iostream>
using namespace std;
#define maxn 1000
#include <cmath>

int gcd(int a,int b)
{
	if ((a==0) || (b==0)) return a+b;
	if (a>b)
	return gcd(a%b,b);
	return gcd(a,b%a);
}
int main()
{
	int y,w;
	cin>>y>>w;
	int t=6-max(y,w)+1;
	if (t==6) cout<<"1/1"<<endl;
	int k=gcd(6,t);
	cout<<t/k<<"/"<<6/k;
	return 0;
}


// A. Бросок кубика
#include <cstdio>
#define loop(i,a,b) for(int i=a;i<b;++i)
int a,b,t;
int main ()
{
    scanf("%d%d", &a, &b);
    if ( b > a )
    {
        t = a;
        a = b;
        b = t;
    }
    if ( b == a )
        --a;
    else if ( a == 1 )
        printf("1/1");
    else if ( a == 2 )
        printf("5/6");
    else if ( a == 3 )
        printf("2/3");
    else if ( a == 4 )
        printf("1/2");
    else if ( a == 5 )
        printf("1/3");
    else
        printf("1/6");
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

long long res,a[3];
int main()
{
	long long r, b, g;
	cin >> r >> g >> b;
	res += r / 3 + g / 3 + b / 3;
	a[0] = r % 3;
	a[1] = g % 3;
	a[2] = b % 3;
	sort(a, a + 3);
	res += a[0];
	cout << res << endl;
	system("pause");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
vector<int> AllChances;
int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    int Remain = 0;
    while(a > 0 && b > 0 && c > 0)
    {
        AllChances.push_back(a / 3 + b / 3 + c/3 + Remain);
        a--;
        b--;
        c--;
        Remain++;
    }
    if(AllChances.size() != 0)
        cout << *max_element(AllChances.begin(),AllChances.end());
    else
        cout << a/3 + b/3 + c/3;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<set>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<limits.h>
#include<queue>
#include<map>
using namespace std;
map < pair<int, pair<int, int>>, int>dp;
int rgb(int r, int g, int b){
	if ((r == 0 && g < 3 && b < 3) || (g == 0 && r < 3 && b < 3) || (b == 0 && g < 3 && r < 3))
		return 0;
	if (dp.find({ r, { g, b } }) != dp.end()) {
		return dp[{r, { g, b }}];
	}
	int w = 0;
	if (r - 3 >= 0)
		w = rgb(r - 3, g, b) + 1;
	if (g - 3 >= 0)
		w = dp[{r, { g, b }}]= max(w, rgb(r, g - 3, b) + 1);
	if (b - 3 >= 0)
		w = dp[{r, { g, b }}]= max(w, rgb(r, g, b - 3) + 1);
	if (r - 1 >= 0 && b - 1 >= 0 && g - 1 >= 0)
		w = dp[{r, { g, b }}]= max(w, rgb(r - 1, g - 1, b - 1) + 1);
	return w;
}

int main(){

	int r, g, b;
	cin >> r >> g >> b;
	cout << rgb(r, g, b) << endl;

	return 0;
}


#include<stdio.h>
#include<conio.h>

int main()
{
          int r,g,b,x,y,z,dem,min;
          scanf("%d %d %d",&r,&g,&b);
          dem=0;
          dem=dem+(r/3)+(g/3)+(b/3);
          x=r%3;
          y=g%3;
          z=b%3;
          min=x;
          if(y<x)
          min=y;
          if(z<min)
          min=z;
          dem=dem+min;
          if(r>0&&y==2&&z==2||g>0&&x==2&&z==2||b>0&&x==2&&y==2)
          dem++;
          printf("%d",dem);
getch();
return 0;
}


#include <iostream>
using namespace std;

int main() {
    int ans=0;
    while(1)
    {
        int a;
        cin>>a;
        ans++;
        if(a==2)
        {
            cout<<ans;
            return 0;
        }

    }
    return 0;
}


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

int main() {
 while( 1 ) {
    int a , b , c , d , e ;
 cin >> a >> b >> c >> d >> e ;
 cout << min( a , min ( b , min ( min ( c/2 , d/7 ) , e/4 ) ) ) << endl ;
 }
 return 0 ;
}


#include <cstdio>

int main()
{
    int a,b;
    scanf("%d",&b);
    bool ok = true;
    while(scanf("%d",&a))
    {
        if (a-b != 2)
            ok =false;
        b = a;
    }
    printf("%d\n",ok);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <iterator>
#include <map>
#define int long long
#define bgint __int128
using namespace std;

const int MAX_NOEUDS = (100 * 1000);

struct ratio {
    bgint num, denom;
    void Print() {
    //    printf("%lld %lld\n", num, denom);
    }
};

struct pente {
    int quart;
    ratio acc;
    void Print() {
    //    printf("%lld   ", quart);
        acc.Print();
    }
};

struct bottle {
    ratio salt, pepper;
    pente angle;
    void Print() {
        printf("b ");
        salt.Print();
        pepper.Print();
        angle.Print();
        printf("\n\n");
    }
};

set <pente> Pentes;

map <pente, int> Occurences;

int nbExacts = 0;
int nbPaires = 0;
int nbAct = 0;
bool isIn = false;

bottle Entree[MAX_NOEUDS];

bottle cible;
int nbRequetes;

bgint Pgcd(bgint a, bgint b) {
    if (b == 0)
    {
        return a;
    }
    return Pgcd(b, a % b);
}

ratio Simp(ratio a) {
    bgint d = Pgcd(max(a.num, - a.num), max(a.denom, - a.denom));
    d = max(d, -d);
    if (a.denom < 0)
    {
        a.num *= -1;
        a.denom *= -1;
    }
    return {a.num / d, a.denom / d};
}

ratio operator+(ratio const& a, ratio const& b) {
    return Simp({a.num * b.denom + b.num * a.denom, a.denom * b.denom});
}

ratio operator*(ratio const& a, ratio const& b) {
    return Simp({a.num * b.num, a.denom * b.denom});
}

ratio operator-(ratio const& a, ratio const& b) {
    return Simp({a.num * b.denom - b.num * a.denom, a.denom * b.denom});
}

bool operator>(ratio const& a, ratio const& b) {
    return a.num * b.denom > a.denom * b.num;
}

bool operator<(ratio const& a, ratio const& b) {
    return a.num * b.denom < a.denom * b.num;
}

bool operator<(pente const& a, pente const& b) {
    if (a.quart < b.quart)
    {
        return true;
    }
    if (a.quart > b.quart)
    {
        return false;
    }
    return a.acc > b.acc;
}

pente CalculePente(bottle a) {
    ratio h = a.salt - cible.salt;
    ratio v = a.pepper - cible.pepper;
    if (h.num == 0 && v.num == 0)
    {
        return {-1, {1, 1}};
    }
    if (h.num == 0)
    {
        if (v.num > 0)
        {
            return {0, {1, 0}};
        }
        return {2, {1, 0}};
    }
    if (v.num == 0)
    {
        if (h.num > 0)
        {
            return {1, {1, 0}};
        }
        return {3, {1, 0}};
    }
    if (h.num > 0 && v.num < 0)
    {
        v.num *= -1;
        return {1, Simp({h.num * v.denom, h.denom * v.num})};
    }
    if (h.num < 0 && v.num < 0)
    {
        h.num *= -1;
        v.num *= -1;
        return {2, Simp({v.num * h.denom, v.denom * h.num})};
    }
    if (h.num < 0 && v.num > 0)
    {
        h.num *= -1;
        return {3, Simp({h.num * v.denom, h.denom * v.num})};
    }
    return {0, Simp({v.num * h.denom, v.denom * h.num})};
}

bool IsOver180(pente a, pente b) {
    if (b < a)
    {
        //printf("666 ERROR\n");
        //a.Print();
        //b.Print();
        return !IsOver180(b, a);
    }
    else
    {
        //printf("noice\n");
    }
    if (b.quart - a.quart == 3)
    {
        return true;
    }
    if (b.quart - a.quart == 2 && b.acc < a.acc)
    {
        return true;
    }
    return false;
}

void CheckAllAngles() {
    bool t = true;
    set<pente>::iterator it;
    for (it = Pentes.begin(); it != Pentes.end(); it ++)
    {
    //    printf("%lld ", (*it).quart);
    }
    printf("\n");
    return;
    for (it = Pentes.begin(); it != Pentes.end(); it ++)
    {
        set<pente>::iterator next = it;
        next ++;
        if (next == Pentes.end())
        {
            next = Pentes.begin();
        }
        if (IsOver180(*it, *next))
        {
            t = false;
        }
    }
    isIn = t;
    return;
}

void Read() {
    int a, b, c;
    cin >> a >> b >> c;
    //scanf("%lld%lld%lld", &a, &b, &c);
    a = (bgint)a;
    b = (bgint)b;
    c = (bgint)c;
    cible.salt = Simp({a, a + b + c});
    cible.pepper = Simp({b, a + b + c});
    cin >> nbRequetes;
    //scanf("%lld", &nbRequetes);
    return;
}

void Add(bottle a) {
    pente act = a.angle;
    if (act.quart < 0)
    {
        nbExacts ++;
        return;
    }
    Occurences[act] ++;
    pente opp = {(act.quart + 2) % 4, act.acc};
    nbPaires += Occurences[opp];
    if (Occurences[act] == 1)
    {
        Pentes.insert(act);
        set<pente>::iterator nv = Pentes.find(act);
        set<pente>::iterator prev = nv;
        if (prev == Pentes.begin())
        {
            prev = Pentes.end();
        }
        prev --;
        set<pente>::iterator next = nv;
        next ++;
        if (next == Pentes.end())
        {
            next = Pentes.begin();
        }
        nbAct ++;
        if (IsOver180(*prev, *next))
        {
            isIn = true;
        }
        if (IsOver180(*prev, *nv))
        {
            isIn = false;
        }
        if (IsOver180(*nv, *next))
        {
            isIn = false;
        }
    }
    return;
}

void Delete(bottle a) {
    pente act = a.angle;
    if (act.quart < 0)
    {
        nbExacts --;
        return;
    }
    Occurences[act] --;
    pente opp = {(act.quart + 2) % 4, act.acc};
    nbPaires -= Occurences[opp];
    if (Occurences[act] == 0)
    {
        set<pente>::iterator nv = Pentes.find(act);
        set<pente>::iterator prev = nv;
        if (prev == Pentes.begin())
        {
            prev = Pentes.end();
        }
        prev --;
        set<pente>::iterator next = nv;
        next ++;
        if (next == Pentes.end())
        {
            next = Pentes.begin();
        }
        if (IsOver180(*prev, *nv))
        {
            isIn = true;
        }
        if (IsOver180(*nv, *next))
        {
            isIn = true;
        }
        Pentes.erase(nv);
        nbAct --;
        if (nbAct < 1)
        {
            return;
        }
        if (IsOver180(*prev, *next))
        {
            isIn = false;
        }
    }
    return;
}

int FindMin() {
    if (nbExacts > 0)
    {
        return 1;
    }
    if (nbPaires > 0)
    {
        return 2;
    }
    if (nbAct >= 3 && isIn)
    {
        return 3;
    }
    return 0;
}

void Solve() {
    int nb = 0;
    for (int i = 0; i < nbRequetes; i ++)
    {
        char c;
        //scanf(" %c", &c);
        cin >> c;
        if (c == 'A')
        {
            int a, b, c;
            //scanf("%lld%lld%lld", &a, &b, &c);
            cin >> a >> b >> c;
            a = (bgint)a;
            b = (bgint)b;
            c = (bgint)c;
            Entree[nb].salt = Simp({a, a + b + c});
            Entree[nb].pepper = Simp({b, a + b + c});
            Entree[nb].angle = CalculePente(Entree[nb]);
            Add(Entree[nb ++]);
        }
        if (c == 'R')
        {
            int a;
            //scanf("%lld", &a);
            cin >> a;
            Delete(Entree[a - 1]);
        }
        cout << FindMin() << '\n';
        //printf("%lld\n", FindMin());
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    Read();
    Solve();
    //PRINT();
    return 0;
}