#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;

const int MAXN(50010);
const int MAXM(50010);
const int MAXE(100010);
const int MAXK(6);
const int HSIZE(131313);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-8);

template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> T ABS(const T &a){return a < 0? -a: a;}

int str[4];
int rec[4];
set<int> table[10010], ans;
set<int> st[4][4];
int cur;
int K;
bool vis[10010];

void dfs(int len, int cnt)
{
    if(len == 0)
    {
        for(int i = 0; i < cnt; ++i)
            for(int j = i; j < cnt; ++j)
                st[i][j].clear();
        for(int i = 0; i < cnt; ++i)
            st[i][i].insert(rec[i]);
        st[0][0].insert(-rec[0]);
        for(int i = 2; i <= cnt; ++i)
        {
            for(int j = 0; j+i-1 < cnt; ++j)
            {
                int l = j, r = j+i-1;
                for(int k = l; k < r; ++k)
                {
                    set<int>::iterator it1 = st[l][k].begin(), it2 = st[l][k].end();
                    set<int>::iterator it3 = st[k+1][r].begin(), it4 = st[k+1][r].end();
                    for(; it1 != it2; ++it1)
                        for(; it3 != it4; ++it3)
                        {
                            int temp = *it1+*it3;
                            st[l][r].insert(temp);
                            temp = *it1-*it3;
                            st[l][r].insert(temp);
                            temp = *it1**it3;
                            st[l][r].insert(temp);
                        }
                }
            }
        }
        set<int>::iterator it1 = st[0][cnt-1].begin(), it2 = st[0][cnt-1].end();
        for(; it1 != it2; ++it1)
            table[cur].insert(*it1);
        return;
    }
    for(int i = 1; i <= len; ++i)
    {
        int temp = 0;
        for(int j = 0, k = 4-len; j < i; ++j, ++k)
        {
            temp *= 10;
            temp += str[k];
        }
        rec[cnt] = temp;
        dfs(len-i, cnt+1);
    }
}

void debug(int i)
{
    printf("debug: %d\n\n", i);
    for(set<int>::iterator it = table[i].begin(); it != table[i].end(); ++it)
        printf("%04d\n", *it);
    printf("\ndebug end\n\n");
}

int main()
{
    for(cur = 0; cur < 10000; ++cur)
    {
        int ti = cur;
        for(int j = 0; j < 4; ++j)
        {
            str[3-j] = ti%10;
            ti /= 10;
        }
        dfs(4, 0);
//      if(cur == 374)
//          debug(cur);
    }
    int m;
    while(~scanf("%d%d", &K, &m))
    {
        set<int>::iterator it1, it2;
        int cnt = 0;
        for(int i = 0; cnt < m && i < 10000; ++i)
        {
            it1 = table[i].begin();
            it2 = table[i].end();
            memset(vis, 0, sizeof(vis));
            for(; it1 != it2; ++it1)
            {
                int temp = abs(K-*it1);
                if(temp < 10000)
                    for(int j = 0; cnt < m && j < 10000; ++j)
                        if(!vis[j] && table[j].find(temp) != table[j].end())
                        {
                            printf("%04d%04d\n", i, j);
                            vis[j] = true;
                            ++cnt;
                        }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <stack>
#include <cmath>
#include <cassert>
#include <queue>
#include <deque>

using namespace std;

const int INF = 1e9;
const double EPS = 1e-9;

#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) int(a.size())
#define forn(i,n) for (int i = 0; i < n; i++)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

int n, ans = -1;

int calc(int b, int p = -1) {
	int d = 1;

	bool f = false;
	for (int i = 0; i < 100; i++, d = (d * n) % b)
		if (d == 0) {
			f = 1;
			if (p == -1)
				ans = i;
			break;
		}

	if (f)
		return 2;

	f = true;
	d = 1;
	for (int i = 0; i < 100; i++, d = (d * n) % b)
		if (d != 1) {
			f = 0;
			break;
		}

	if (f)
		return 3;

	f = true;
	d = 1;
	for (int i = 0; i < 100; i++, d = (d * n) % b)
		if (i&1) {
			if (d != b - 1) {
				f = false;
				break;
			}
		} else {
			if (d != 1) {
				f = false;
				break;
			}
		}

	if (f)
		return 11;

	f = false;
	for (int i = 2; i < b; i++) {
		int x = 1;
		while (b % i == 0) {
			x *= i;
			b /= i;
		}
		if (x > 1 && x != b) {
			if (calc(x, 0) != 7)
				f = true;
			else
				f = false;
		}
	}

	if (f)
		return 6;

	return 7;
}

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

	cout << calc(b) << "-type" << endl;
	if (ans != -1)
		cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
#endif
		solve();
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

template <class T>
using lim = numeric_limits<T>;

typedef long long ll;

void factorize(int x, unordered_map<int,int> &fact){
	for (int i = 2; i*i <= x; i++){
		while(x % i == 0){
			fact[i]++;
			x /= i;
		}
	}
	if (x > 1) fact[x]++;
}

int fastpow(int b, int e){
	int ans = 1;
	while(e){
		if (e&1) ans *= b;
		e >>= 1;
		b *= b;
	}
	return ans;
}

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

	int b, d;
	cin >> b >> d;

	if((b-1)%d == 0) cout << "3-type\n";
	else if ((b+1)%d == 0) cout << "11-type\n";
	else if (b%d == 0) cout << "2-type\n1\n";
	else{
		unordered_map<int,int> bfact;
		unordered_map<int,int> dfact;

		factorize(b, bfact);
		factorize(d, dfact);

		// it is 2-type iff all primes factors of d are in b
		bool twotype = true;
		for (auto it : dfact) {
			if (bfact.find(it.first) == bfact.end()) {
				// it is 6-type iff the primes that are not in b are 3-type or 11-type
				int num = fastpow(it.first, it.second);
				sixtype &= ((b-1)%num == 0 or (b+1)%num == 0);
				twotype = false;
			}
		}

		// to compute for the number of digits you have to look at for 2-type,
		// maximum of the differences in prime multiplicities +1
		if (twotype){
			int ans = 0;
			for (auto it : bfact) ans = max(ans, abs(it.second-dfact[it.first]));
			ans++;
			cout << "2-type\n" << ans << "\n";
		}
		else if (sixtype) cout << "6-type\n";
		else cout << "7-type\n";
	}
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <ctime>
//#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pint;
typedef pair<LL,LL> pll;
#define fi first
#define se second
typedef map<int,int> mapint;
typedef vector<int> vint;
typedef vector<vint> vintp;
#define rep(i,j,k) for (int i = (j);i <= (k);i ++)
#define ran2 (rand() % 32000 * 32000 + rand() % 32000)
#define pb push_back
#define mp make_pair

int m,n,j,k,l,i,o,p,__t;

char ch;
void read(int &a){
	for (ch = getchar();(ch < '0' || ch > '9') && (ch != '-');ch = getchar());
	if (ch == '-') a = 0,__t = -1; else a = ch - '0',__t = 1;
	for (ch = getchar();ch >= '0' && ch <= '9';ch = getchar()) a = a * 10 + ch - '0';
	a *= __t;
}
int main(){
	scanf("%d%d", &n, &m);
	while ((g = __gcd(n, m)) > 1)
		m /= g, c ++;
	if(m == 1)
		printf("2-type\n%d", c);
	else if (!c && n % m == 1)
		puts("3-type");
	else if (!c && n % m == m - 1)
		puts("11-type");
	else if ((n - 1) * (n + 1) / (n % 2 + 1 ) % m == 0)
		puts("6-type");
	else
		puts("7-type");
}


#include <stdio.h>
#include <algorithm>

const int MOD = 1000000007;
const int MAX_N = 100000;

void selfMult(int &a, int b) {
  a = (long long)a * b % MOD;
}

void selfAdd(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

int power(int base, int exp) {
  int answer = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      selfMult(answer, base);
    }
    selfMult(base, base);
    exp /= 2;
  }
  return answer;
}

int inv(int value) {
  return power(value, MOD - 2);
}

int gcd(int a, int b) {
  int r = a % b;
  while (r > 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int freedoms[1 + MAX_N];
  freedoms[0] = 1;
  freedoms[1] = k;
  for (int i = 2; i <= n; i++) {
    freedoms[i] = freedoms[i - 1];
    selfMult(freedoms[i], k);
  }
  /*
  for (int i = 0; i <= 10; i++) {
    printf("%d ", invK[i]);
  }
  printf("\n");//*/
  int answer = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < i; j++) {
      selfAdd(answer, freedoms[std::max(gcd(n - i, n - j), n - i - j)]);
      /*
      printf("%d,%d => %d\n", i, j, freedoms[n - i - j]);
      if (i <= n - i) {
        selfAdd(answer, freedoms[n - i - j]);
        printf("%d,%d => %d\n", i, j, freedoms[n - i - j]);
      } else if (n - i <= i - j) {
        selfAdd(answer, freedoms[n - i]);
        printf("%d,%d => %d\n", i, j, freedoms[n - i]);
      } else {
        selfAdd(answer, freedoms[i - j]);
        printf("%d,%d => %d\n", i, j, freedoms[i - j]);
      }//*/
    }
  }
  selfMult(answer, 2);
  //printf("%d\n", answer);
  for (int i = 1; i < n; i++) {
    selfAdd(answer, freedoms[n - i]);
    //printf("%d,%d => %d\n", i, i, freedoms[n - i]);
  }
  //printf("%d\n", answer);
  selfMult(answer, power(inv(k), n));
  printf("%d\n", answer);
  return 0;
}

/*
00000 - 4 16
00001 - 0
00010 - 1  1
00011 - 0
00100 - 2  4
00101 - 0
00110 - 1  1
00111 - 0
01000 - 1  1
01001 - 1  1
01010 - 2  4
01011 - 0
01100 - 1  1
01101 - 1  1
01110 - 1  1
01111 - 0
------------(+)
      -   62/32 = 2

00000 1,1
00000 2,2
00000 3,3
00000 4,4
00000 2,1
00000 1,2
00000 3,1
00000 1,3
00000 3,2
00000 2,3
00000 4,1
00000 1,4
00000 4,2
00000 2,4
00000 4,3
00000 3,4
00010 1,1
00100 1,1
00100 2,2
00100 2,1
00100 1,2
00110 1,1
01000 1,1
01001 2,2
01010 1,1
01010 3,3
01010 3,1
01010 1,3
01100 1,1
01101 2,2
01110 1,1

2 1
xxyxx

3 1
xyxyx

3 2
xxxxx

4 1
xxxxx

1/8 + 1/8 + 1/16 + 1/16 + 1/16 + 1/16
= 1/4 + 1/4
= 1/2
= 1

1
xyztx

2
xyzxy

3
xyxyx

4
xxxxx

1/16 + 1/8 + 1/4 + 1/2

6,1,10
xxztxxztxx
gcd(4, 9) = 1

max(10-6-1, gcd(10-6, 10-1))

6,2,10
xyztxyztxy
gcd(4, 8) = 4

6,3
xxxxxxxxxx
gcd(4, 7) = 1

6,4
xyxyxyxyxy
gcd(4,6)=2

6,5

3,1,5
xyxyx

ababababab
       ab

*/


#include <cstdio>
#include <vector>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <algorithm>
#define meow(args...) fprintf(stderr, args)
const int N=1e5;
uint32_t P=1e9+7;

int phi[N*2+1], minp[N*2+1], rad[N*2+1];
bool vis[N*2+1];
uint32_t pw[N+1];
std::vector<int> pr;
auto cmin=[](auto &a, const auto &b) {return b<a?(a=b, true):false;};
auto cmax=[](auto &a, const auto &b) {return a<b?(a=b, true):false;};

uint64_t power(uint64_t a, uint32_t b) {
	uint64_t ans=1;
	for(; b; a=a*a%P, b>>=1) if(b&1) ans=ans*a%P;
	return ans;
}
int partial_phi(int n, int s) {
	if(n==0) return 0;
	if(s==1) return n;
	int p=minp[s];
	return partial_phi(n, s/p)-partial_phi(n/p, s/p);
}
int main() {
	int n, k;
	assert(scanf("%d%d", &n, &k)==2);
	uint64_t invk=power(k, P-2);
	pw[0]=1;
	for(int i=1; i<=n; ++i) pw[i]=pw[i-1]*invk%P;
	phi[1]=1;
	rad[1]=1;
	for(int i=2; i<=2*n; ++i) {
		if(!vis[i]) {
			pr.push_back(i);
			minp[i]=i;
			phi[i]=i-1;
			rad[i]=i;
		}
		for(int j=0; j<pr.size()&&i*pr[j]<=2*n; ++j) {
			vis[i*pr[j]]=1;
			minp[i*pr[j]]=pr[j];
			if(i%pr[j]==0) {
				phi[i*pr[j]]=phi[i]*pr[j];
				rad[i*pr[j]]=rad[i];
				break;
			}
			phi[i*pr[j]]=phi[i]*(pr[j]-1);
			rad[i*pr[j]]=rad[i]*pr[j];
		}
	}
	uint32_t ans=0;
	for(int d=1; d<n; ++d) {
		int m=(n-1)/d;
		for(int s=2; s<=m; ++s) ans=(ans+(uint64_t)phi[s]*pw[n-d])%P;
		for(int s=m+1; s<=m*2; ++s) ans=(ans+(uint64_t)(partial_phi(m, rad[s])-partial_phi(s-m-1, rad[s]))*pw[std::min(2*n-d*s, n-d)])%P;
	}
	printf("%u\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int N=1e5+2;
void add(int &x,int y){
	x+=y;
	if(x>=mod){
		x-=mod;
	}
}
void sub(int &x,int y){
	x-=y;
	if(x<0){
		x+=mod;
	}
}
int mul(int x,int y){
	return (1ll*x*y)%mod;
}
int binpow(int x,int y){
	int tich=1;
	while(y){
		if(y&1){
			tich=mul(tich,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return tich;
}
int backtrack(vector<int> &lis,int lef,int rig,int ptr,int cnt,int val,int s){
	if(ptr==lis.size()){
		return cnt*(rig/val-(lef-1)/val);
	}
	return backtrack(lis,lef,rig,ptr+1,cnt,val,s)+backtrack(lis,lef,rig,ptr+1,-cnt,val*lis[ptr],s);
}
int pw[N];
vector<int> uoc[N],uocnt[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,num,ans=0,r;
	cin>>n>>num;
	pw[0]=1;
	for(i=1;i<=n;i++){
		pw[i]=mul(pw[i-1],num);
	}
	for(i=1;i<=2*n;i++){
		for(j=i;j<=2*n;j+=i){
			uoc[j].push_back(i);
		}
		if(i==1){
			continue;
		}
		if(uoc[i].size()==2){
			for(j=i;j<=2*n;j+=i){
				uocnt[j].push_back(i);
			}
		}
	}
	for(int s=3;s<2*n-2;s++){
		for(int gcd:uoc[s]){
			int news=s/gcd,l=max(1,news-(n-1)/gcd),r=min((n-1)/gcd,news-1);
			if(l>r){
				continue;
			}
2 3
			j=backtrack(uocnt[news],l,r,0,1,1,news);
			if(news==2){
				j--;
			}
			add(ans,mul(j,pw[max(s-n,gcd)]));
		}
	}
	for(i=1;i<n;i++){
		add(ans,pw[n-i]);
	}
	cout<<mul(ans,binpow(pw[n],mod-2));
}


include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
inline int add(int a,int b){a+=b;return a>mod?a-mod:a;}
inline int sub(int a,int b){a-=b;return a<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline int qpow(int a,int b){int ret=1;for(;b;b>>=1,a=mul(a,a))if(b&1)ret=mul(ret,a);return ret;}
/* math */
int n,k;

const int N = 2e5+5;

vector<int> d[N];
ll c[N];
ll dp[N],ans[N];

int main()
{
	for(int i=1;i<N;i++)for(int j=i;j<N;j+=i)d[j].push_back(i);
	cin >> n >> k;
	for(int s=n+1;s<n*2-1;++s){
		int l=s-n,r=n-1;
		for(size_t i=0;i<d[s].size();i++){
			int x=d[s][i];
			c[x]=(r/x)-(l/x);
		}
		for(size_t i=(int)d[s].size()-1;~i;i--){
			int x=d[s][i];
			for(size_t j=0;j<d[x].size();j++){
				int y=d[x][j];
				if(x!=y) c[y]-=c[x];
			}
		}
		for(size_t i=0;i<d[s].size();i++){
			int g=d[s][i];
			if(s-g>=n) ans[s-n]+=c[g];
		}
	}
	for(int m=2;m<=n;m++){
		dp[m] = (ll)m*(m-1)/2;
		ll b= (ll)sqrt(m);
		while(b*b<m)b++;
		for(int i=2;i<=b;i++){
			dp[m]-=dp[m/i];
		}
		for(int i=b;i;i--){
			ll z=m/i;
			if(z>b){
				dp[m]-=(z-b)*dp[i];
				b=z;
			}
		}
	}
	for(int g=1;g<n;g++){
		ans[g]+=dp[(n-1)/g+1];
	}
	int res=0;
	for(int i=1;i<n;i++){
		ans[i]+=mod;
		ans[i]%=mod;
		res=add(res, mul(ans[i], qpow(k,i)));
	}
	res=mul(res,qpow(qpow(k,n),mod-2));
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

const int MAXN = 32768 - 1;
int ans[100500];
bool use[100500];
bool otl[100500];

int res;
string s="";

int eval_num(int n, int &i);
int eval_product(int n, int &i);
int eval_sum(int n, int &i);
bool eval_logick(int n, int & i);
int eval_op(int n, int &i);
int eval_opseq(int n, int &i);
int eval_func(int n, int & i);
int run(int n);

string getlex(int & i) {
    try {

    while (s[i]==' ' || s[i]==' ' || s[i]=='\n')
        ++i;

    string t = "";

    if (
        s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
        || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
       )
    {
        t += s[i]; ++i; return t;
    }

    while (!(s[i]==' ' || s[i]==' ' || s[i]=='\n'
            || s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
            || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
        ))
    {
        t += s[i]; ++i;
    }

    return t;

    }
    catch(...) {
        exit(0);
        t = ";";
        return t;
    }
}

int norm(int x) {
    while (x<0) x += MAXN + 1;
    return x % (MAXN+1);
}

int eval_num(int n, int &i) {
    string sw = getlex(i);
//    cerr << "num_sw: " << sw << "\n";
    if (sw=="n") return n;
    if (sw=="f") {
        getlex(i);
        int t = eval_sum(n, i);
        getlex(i);
        if (use[t])
            return run(t);
        else {
            throw -1;
        }
    }
    return atoi(sw.c_str());
}

int eval_product(int n, int &i) {
    int sum = eval_num(n, i);
    int j=i;
    string op = getlex(j);
    while (op=="*" || op=="/") {
        int t = eval_num(n, j);
        i = j;
        if (op=="*") sum *= t;
        else sum /= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "product: %d\n", sum);
    return sum;
}

int eval_sum(int n, int &i) {
    int sum = eval_product(n, i);
    //cerr << sum;
    int j=i;
    string op = getlex(j);
    while (op=="+" || op=="-") {
        int t = eval_product(n, j);
        i = j;
        if (op=="+") sum += t;
        else sum -= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "sum: %d\n", sum);
    return sum;
}

bool eval_logick(int n, int & i) {
    int e1 = eval_sum(n, i);
    string op = getlex(i);
    int e2 = eval_sum(n, i);

    if (op==">") return (e1>e2);
    if (op=="<") return (e1<e2);
    if (op=="==") return (e1==e2);
    return false;
}

int eval_op(int n, int &i) {
    string sw = getlex(i);

    if (sw=="if") {
        getlex(i); //get '('
        bool b  = eval_logick(n, i);
        getlex(i); //get ')'

        if (b) {
            getlex(i); //get 'return'
        } else {
            while (s[i]!=';') ++i;
            ++i;
            return -1;
        }
    }

    int ret = eval_sum(n, i);
    getlex(i);
//    cerr << "res: " << ret << "\n";
    return ret;
}

int eval_opseq(int n, int &i) {
    int ret=-1; //
    while ( ret < 0) {
        ret = eval_op(n, i);
    }
//    fprintf(stderr, "opseq: %d\n", ret);
    return ret;
}

int eval_func(int n, int & i) {
//    cerr << "\n\n";
    for (int k=0; k<7; ++k) {
        getlex(i);
    }
//    cerr << i << "\n\n\n" ;
    int t = eval_opseq(n, i);
//    fprintf(stderr, "func: %d\n", t);
    return t;
}

int hr = 0;

int run(int n) {
    try {
        int j = 0;
        if (use[n]) return ans[n];
        ans[n] = eval_func(n, j);
        use[n] = true;
    }
    catch (...) {
        use[n] = false;
    }
    return ans[n];
}

int main() {
    try {
    //freopen("in.txt", "r", stdin);
    cin >> res;
    string t;
    while (!feof(stdin)) { getline(cin, t); s += t + " "; }
    s += "          ;";
//    cerr << s;
    //cout << run(res);

    bool b = true;
    while (b) {
        b = true;
        for (int i=0; i<=MAXN; ++i) {
            run(i);
            b = b && !use[i];
        }
    }

    int mx = -1;
    for (int i=0; i<=MAXN; ++i)
        if (use[i] && ans[i]==res) mx = i;
    cout << mx;
    //main logick
    //TODO: write it
    }
    catch(...) {
        cout << "this is sparta!!!11111";
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

const int MAXN = 32768 - 1;
int ans[100500];
bool use[100500];
bool otl[100500];

int res;
string s="";

int eval_num(int n, int &i);
int eval_product(int n, int &i);
int eval_sum(int n, int &i);
bool eval_logick(int n, int & i);
int eval_op(int n, int &i);
int eval_opseq(int n, int &i);
int eval_func(int n, int & i);
int run(int n);

string getlex(int & i) {
    while (s[i]==' ' || s[i]==' ' || s[i]=='\n')
        ++i;

    string t = "";

    if (
        s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
        || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
       )
    {
        t += s[i]; ++i; return t;
    }

    while (!(s[i]==' ' || s[i]==' ' || s[i]=='\n'
            || s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
            || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
        ))
    {
        t += s[i]; ++i;
    }
    return t;
}

int norm(int x) {
    while (x<0) x += MAXN + 1;
    return x % (MAXN+1);
}

int eval_num(int n, int &i) {
    string sw = getlex(i);
//    cerr << "num_sw: " << sw << "\n";
    if (sw=="n") return n;
    if (sw=="f") {
        getlex(i);
        int t = eval_sum(n, i);
        getlex(i);
        if (use[t] && !otl[t])
            return run(t);
        else {
            otl[n] = true;
            throw -1;
        }
    }
    return atoi(sw.c_str());
}

int eval_product(int n, int &i) {
    int sum = eval_num(n, i);
    int j=i;
    string op = getlex(j);
    while (op=="*" || op=="/") {
        int t = eval_num(n, j);
        i = j;
        if (op=="*") sum *= t;
        else sum /= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "product: %d\n", sum);
    return sum;
}

int eval_sum(int n, int &i) {
    int sum = eval_product(n, i);
    //cerr << sum;
    int j=i;
    string op = getlex(j);
    while (op=="+" || op=="-") {
        int t = eval_product(n, j);
        i = j;
        if (op=="+") sum += t;
        else sum -= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "sum: %d\n", sum);
    return sum;
}

bool eval_logick(int n, int & i) {
    int e1 = eval_sum(n, i);
    string op = getlex(i);
    int e2 = eval_sum(n, i);

    if (op==">") return (e1>e2);
    if (op=="<") return (e1<e2);
    if (op=="==") return (e1==e2);
    return false;
}

int eval_op(int n, int &i) {
    string sw = getlex(i);

    if (sw=="if") {
        getlex(i); //get '('
        bool b  = eval_logick(n, i);
        getlex(i); //get ')'

        if (b) {
            getlex(i); //get 'return'
        } else {
            while (s[i]!=';') ++i;
            ++i;
            return -1;
        }
    }

    int ret = eval_sum(n, i);
    getlex(i);
//    cerr << "res: " << ret << "\n";
    return ret;
}

int eval_opseq(int n, int &i) {
    int ret=-1; //
    while ( ret < 0) {
        ret = eval_op(n, i);
    }
//    fprintf(stderr, "opseq: %d\n", ret);
    return ret;
}

int eval_func(int n, int & i) {
//    cerr << "\n\n";
    for (int k=0; k<7; ++k) {
        getlex(i);
    }
//    cerr << i << "\n\n\n" ;
    int t = eval_opseq(n, i);
//    fprintf(stderr, "func: %d\n", t);
    return t;
}

int hr = 0;

int run(int n) {
    try {
        int j = 0;
        if (use[n]) return ans[n];
        ans[n] = eval_func(n, j);
        use[n] = true;
    }
    catch (int e) {
        use[n] = false;
    }
    return ans[n];
}

int main() {
    freopen("in.txt", "r", stdin);
    cin >> res;
    string t;
    while (!feof(stdin)) { getline(cin, t); s += t + " "; }
    s += "          ;;;;";
//    cerr << s;
    //cout << run(res);

    bool b = true;
    while (b) {
        b = true;
        for (int i=0; i<=MAXN; ++i) run(i);
        for (int i=0; i<=MAXN; ++i)
            b = b && !use[i];
    }

    int mx = -1;
    for (int i=0; i<=MAXN; ++i)
        if (use[i] && ans[i]==res) mx = i;
    cout << mx;
    //main logick
    //TODO: write it

    return 0;
}


#include <stdio.h>
char s[1000];
#define EQUAL 1
#define NONE 0
#define MORE 2
#define LESS 3
int slen=0;
struct T{
    char l1[150];
    char l2[150];
    char ans[150];
    int operat;
};
#include <memory.h>
T rr[1000];
int mem[1000000];
int j=0;
int rec(int n);
int ans(char* s,int n){
    int l=0;
    int a=0;
    int b=-1;
    int c=1;

    int ll=0;
    int last=0;;
    int ele=0;
    while (*s){
        ele++;

        l=0;
        if (*s=='n'){
            l=n;
            *s++;
        }else if (*s=='f'){
            char m[100];
            int mn=0;
            s+=2;
            char* r=s;
            int level=1;
            while (level>0){
                if (*s==')')
                    level--;
                if (*s=='(')
                    level++;
                m[s-r]=*s;
                s++;
            }

            m[s-r-1]=0;

            l=rec(ans(m,n));
        }else while (*s!='*'&&*s!='/'&&*s!='+'&&*s!='-'&&*s!=0){
            l*=10;
            l+=*s-'0';
            s++;
        }
        if (last==1){
            b*=l;
            b%=32768;
        }
        if (last==2){
            b/=l;
            b%=32768;
        }

        if (b==-1)
           b=l;
        switch (*s){
            case '*':
                last=1;
                if (b==-1)
                    b=l;
                s++;
            break;
            case '-':
                a+=b*c;
                c=-1;
                b=-1;
                s++;
                last=0;
            break;
            case '/':
                last=2;

                s++;
            break;
            case '+':
                if (b==-1)
                    b=l;
                a+=b*c;
                c=1;
                b=-1;
                s++;
                last=0;
            break;
        }
    }

    if (last==0)
        b=l;

    return (a+b*c+32768)%32768;
}
int rec(int n){
    if (mem[n]==-1)
        for (int i=0;i<j;i++){
            if (rr[i].operat==NONE){
                mem[n]=ans(rr[i].ans,n);
                break;

            };
            if (rr[i].operat==EQUAL){
                int a=ans(rr[i].l1,n);
                int b=ans(rr[i].l2,n);
                if (a==b){
                    mem[n]=ans(rr[i].ans,n);
                    break;
                }
            };
            if (rr[i].operat==MORE){
                int a=ans(rr[i].l1,n);
                int b=ans(rr[i].l2,n);
                if (a>b){
                    mem[n]=ans(rr[i].ans,n);
                    break;
                }
            };
            if (rr[i].operat==
                    LESS){
                int a=ans(rr[i].l1,n);
                int b=ans(rr[i].l2,n);
                if (a<b){
                    mem[n]=ans(rr[i].ans,n);
                    break;
                }
            };
        }
    return mem[n];
}
int main(){
    memset(mem,0xFF,sizeof(mem));

    int n;

    scanf("%d",&n);
    char c=0;
    while (c!='{'){
        scanf("%c",&c);

    }
    scanf("%c",&c);
    while (c!='}'){
        if (c!='\n'){
            s[slen]=c;
            slen++;
        }
        scanf("%c",&c);
    }
    s[slen]=0;
    char* ss=s;

    while (ss){
        while (*ss==' '||*ss=='\n')
            ss++;
        if (*ss=='i'){

            while (*ss!='(')
                ss++;
            ss++;
            int q=0;
            while (*ss!='='&&*ss!='<'&&*ss!='>'){
                if (*ss!=' '){
                    rr[j].l1[q]=*ss;
                    q++;
                }
                ss++;
            }
            rr[j].l1[q]=0;
            if (*ss=='='){
                rr[j].operat=EQUAL;
                ss+=2;
            }
            if (*ss=='<'){
                rr[j].operat=LESS;
                ss++;
            }
            if (*ss=='>'){
                rr[j].operat=MORE;
                ss++;
            }
            q=0;
            while (*ss!=')'){
                if (*ss!=' '){
                    rr[j].l2[q]=*ss;
                    q++;
                }
                ss++;
            }
            rr[j].l2[q]=0;
            q=0;
            while (*ss!='r')
                ss++;
            ss+=6;
            while (*ss!=';'){
                if (*ss!=' '){
                    rr[j].ans[q]=*ss;
                    q++;
                }
                ss++;
            }
            ss++;
            rr[j].ans[q]=0;

        }else if (*ss=='r'){
            ss+=6;
            rr[j].operat=NONE;
            int q=0;
            while (*ss!=';'){
                if (*ss!=' '){
                    rr[j].ans[q]=*ss;
                    q++;
                }
                ss++;
            }
            ss++;
            rr[j].ans[q]=0;
            rr[j].l1[0]=0;
            rr[j].l2[0]=0;
        }else
            if (*ss==0)
                break;

        j++;

    }

    for (int i=0;i<(1<<15);i++){
       rec(i);
    }
    int max=-1;
     for (int i=0;i<(1<<15);i++){
         if (mem[i]==n){
             max=i;

         }
     }
         printf("%d",max);

}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

const int MAXN = 32768 - 1;
int ans[100500];
bool use[100500];
bool otl[100500];

int res;
string s="";

int eval_num(int n, int &i);
int eval_product(int n, int &i);
int eval_sum(int n, int &i);
bool eval_logick(int n, int & i);
int eval_op(int n, int &i);
int eval_opseq(int n, int &i);
int eval_func(int n, int & i);
int run(int n);

string getlex(int & i) {
    while (s[i]==' ' || s[i]==' ' || s[i]=='\n')
        ++i;

    string t = "";

    if (
        s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
        || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
       )
    {
        t += s[i]; ++i; return t;
    }

    while (!(s[i]==' ' || s[i]==' ' || s[i]=='\n'
            || s[i]=='(' || s[i]==')' || s[i]=='{' || s[i]=='}'
            || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]==';'
        ))
    {
        t += s[i]; ++i;
    }
    return t;
}

int norm(int x) {
    while (x<0) x += MAXN + 1;
    return x % (MAXN+1);
}

int eval_num(int n, int &i) {
    string sw = getlex(i);
//    cerr << "num_sw: " << sw << "\n";
    if (sw=="n") return n;
    if (sw=="f") {
        getlex(i);
        int t = eval_sum(n, i);
        getlex(i);
        if (use[t])
            return run(t);
        else {
            throw -1;
        }
    }
    return atoi(sw.c_str());
}

int eval_product(int n, int &i) {
    int sum = eval_num(n, i);
    int j=i;
    string op = getlex(j);
    while (op=="*" || op=="/") {
        int t = eval_num(n, j);
        i = j;
        if (op=="*") sum *= t;
        else sum /= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "product: %d\n", sum);
    return sum;
}

int eval_sum(int n, int &i) {
    int sum = eval_product(n, i);
    //cerr << sum;
    int j=i;
    string op = getlex(j);
    while (op=="+" || op=="-") {
        int t = eval_product(n, j);
        i = j;
        if (op=="+") sum += t;
        else sum -= t;

        sum = norm(sum);
        op = getlex(j);
    }
//    fprintf(stderr, "sum: %d\n", sum);
    return sum;
}

bool eval_logick(int n, int & i) {
    int e1 = eval_sum(n, i);
    string op = getlex(i);
    int e2 = eval_sum(n, i);

    if (op==">") return (e1>e2);
    if (op=="<") return (e1<e2);
    if (op=="==") return (e1==e2);
    return false;
}

int eval_op(int n, int &i) {
    string sw = getlex(i);

    if (sw=="if") {
        getlex(i); //get '('
        bool b  = eval_logick(n, i);
        getlex(i); //get ')'

        if (b) {
            getlex(i); //get 'return'
        } else {
            while (s[i]!=';') ++i;
            ++i;
            return -1;
        }
    }

    int ret = eval_sum(n, i);
    getlex(i);
//    cerr << "res: " << ret << "\n";
    return ret;
}

int eval_opseq(int n, int &i) {
    int ret=-1; //
    while ( ret < 0) {
        ret = eval_op(n, i);
    }
//    fprintf(stderr, "opseq: %d\n", ret);
    return ret;
}

int eval_func(int n, int & i) {
//    cerr << "\n\n";
    for (int k=0; k<7; ++k) {
        getlex(i);
    }
//    cerr << i << "\n\n\n" ;
    int t = eval_opseq(n, i);
//    fprintf(stderr, "func: %d\n", t);
    return t;
}

int hr = 0;

int run(int n) {
    try {
        int j = 0;
        if (use[n]) return ans[n];
        ans[n] = eval_func(n, j);
        use[n] = true;
    }
    catch (...) {
        use[n] = false;
    }
    return ans[n];
}

int main() {
    freopen("in.txt", "r", stdin);
    cin >> res;
    string t;
    while (!feof(stdin)) { getline(cin, t); s += t + " "; }
    s += "          ;;;;";
//    cerr << s;
    //cout << run(res);

    bool b = true;
    while (b) {
        b = true;
        for (int i=0; i<=MAXN; ++i) {
            run(i);
            b = b && !use[i];
        }
    }

    int mx = -1;
    for (int i=0; i<=MAXN; ++i)
        if (use[i] && ans[i]==res) mx = i;
    cout << mx;
    //main logick
    //TODO: write it

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 1123456;
#define pb push_back
#define sz(a) int((a).size())
#define clr(a,x) memset(a,x,sizeof(a))
typedef pair<int,int> ii;
typedef pair<int, pair<int,int> > iii;
typedef vector<int> vi;
typedef vector< ii > vii;
typedef vector< iii > viii;
typedef vector< vector<int> > vvi;
typedef map< string, int > msi;
typedef long long ll;

const int MOD = 1000 * 1000 * 1000 + 7

ll potencia( ll m, ll n )
{
	if ( n == 0 )
		return 1;
	if ( n % 2 == 0 ) {
		return potencia( m * m % MOD, n/2 );
	}
	return ( potencia( m * m % MOD ,n/2 ) * m ) % MOD;
}

int main()
{
	ll n,m;
	scanf("%I64d %I64d", &n, &m);
	ll ans = 0;
	if ( m == 1 ) {
		printf("%d", n+1);
		return 0;
	}
	ans += potencia(m,n);
	ll aux = ( (m % MOD)*potencia(m-1,n-1) ) % MOD;
	ans = ( ans + aux ) % MOD;
	for ( ll s = 1; s < n; s++ ) {
		ll fator1 = ( ( n - s + 1 ) % MOD ) / ( s % MOD );
		ll fator2 = ( m % MOD ) / ( (m-1) % MOD );
		aux = ( fator1 * fator2 ) % MOD;
		ans += aux; ans %= MOD;
	}
	printf("%I64d\n", ans);
	return 0;
}


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

int main()
{
ll n, m;
cin >> n >> m;
cout << (m*(2m-1)^n - m^n) / (m-1);
}


#include <iostream>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int p=1,res=1,md=1e9+7;
    for (int i=0;i<n;++i) {
        res=(2ll*m*res%md-(res-p+mo)+mo)%mo
        p=p*1LL*m%mo;
    }
    cout<<res<<endl;
}


#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int mod=1e9+7;
int n,m,mod,pw1,pw2,invm,base,ans;
inline void inc(int& x,CI y)
{
    if ((x+=y)>=mod) x-=mod;
}
inline int quick_pow(int x,int p=mod-2,int mul=1)
{
    for (;p;p>>=1,x=1LL*x*x%mod) if (p&1) mul=1LL*mul*x%mod; return mul;
}
int main()
{
    //freopen("invisible.in","r",stdin); freopen("invisible.out","w",stdout);
    scanf("%d%d",&n,&m); ans=pw1=quick_pow(m,n); invm=quick_pow(m); pw2=1; base=(2LL*m-1)%mod;
    //for (RI i=0;i<n;++i) inc(ans,1LL*quick_pow(m,n-i)*quick_pow(2*m-1,i)%mod);
    for (RI i=0;i<n;++i) inc(ans,1LL*pw1*pw2%mod),pw1=1LL*pw1*invm%mod,pw2=1LL*pw2*base%mod;
    return printf("%d",ans),0;
}


#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
#include<numeric>
#include<cstdlib>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<queue>
#include<cassert>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pi;
typedef long long ll;

const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

bool _(ll x,llt y,llt a,ll b){
	if(a==b&&b==0)return x==0&&y==0;
	return (a*y-b*x)%(a*a+b*b)==0&&(a*x+b*y)%(a*a+b*b)==0;
}
void run()
{
	int a1,a2,b1,b2,c1,c2;
	cin>>a1>>a2>>c1>>c2>>b1>>b2;
	cout<<(_(c1-a1,c2-a2,b1,b2)||_(c1+a2,c2-a1,b1,b2)||
		_(c1+a1,c2+a2,b1,b2)||_(c1-a2,c2+a1,b1,b2)?"YES":"NO")<<endl;
}

int main()
{
#ifndef ONLINE_JUDGE
	int cs=3;rep(i,cs)
#endif
	run();
}


#include<iostream>
#include<fstream>
#include<string>
#include<math.h>
using namespace std;
struct Vec2D
{
      long long int x;
      long long int y;
       };

Vec2D Rot90(Vec2D v)
{
       Vec2D vRot;
       vRot.x = v.y;
       vRot.y = -v.x;
       return vRot;
       }
Vec2D Add(Vec2D v1,Vec2D v2)
{
       Vec2D vSum;
       vSum.x = v1.x+v2.x;
       vSum.y = v1.y+v2.y;
       return vSum;
       }
Vec2D Sub(Vec2D v1,Vec2D v2)
{
       Vec2D vSub;
       vSub.x = v1.x-v2.x;
       vSub.y = v1.y-v2.y;
       return vSub;
       }
long double Mod(Vec2D v)
{
       long double m;
       m = sqrt(v.x*v.x+v.y*v.y);
       return m;
       }
long long int Scalar(Vec2D v1,Vec2D v2)
{
       return (v1.x*v2.x+v1.y*v2.y);
}
bool tentsuu(Vec2D v1,Vec2D v2)
{
     if(v1.x == v2.x && v1.y == v2.y)
       return true; else return false;
}

int main()
{
    int i;
    Vec2D A,B,C,AB;
    char answer[3] = "NO";
  //ifstream cin;
 // cin.open ("vector.in");
    cin>>A.x>>A.y;
    cin>>B.x>>B.y;
    cin>>C.x>>C.y;
 // cin.close();

    AB = Sub(B,A);

    bool cond1 = tentsuu(A,B); //anhnaasaa tentsuu baih
    bool cond2 = ((A.x*(B.y-C.y)+B.x*(C.y-A.y)+C.x*(A.y-B.y))==0); //A,B,C 3 tseg neg shugam deer bn uu
    long double K = (long double)(B.y-A.y)/(long double)(B.x-A.x); //ontsgiin kof
    bool cond2_1 = (A.y == (long long int)(K*A.x)); //A,B,C 3 tsegiig aguulah shugam coordinatiin ehiig dairch bn uu
    bool cond2_1_1 = (Mod(C) == 0 && Mod(A)==Mod(B)); //A g erguuleed B deer dawhtsuulj boloh nohtsol
    bool cond2_1_2 = (Mod(A) == 0 && (C.x!=0 && B.x%C.x==0 || C.y!=0 && B.y%C.y==0)); //A deer C g nemeed B deer dawhtsuulj boloh nohtsol
    bool cond2_1_3 = (Mod(B) == 0 && (C.x!=0 && A.x%C.x == 0 || C.y!=0 && A.y%C.y==0)); //A deer C tei ijil modultai bolgoh nohtsol
    bool cond2_1_4 = (Mod(Add(A,C)) == Mod(B)); //A,B,C 0 bish baihad A -g B d buulgaj boloh nohtsol

  //ofstream cout;
  //cout.open ("vector.out");

if(cond1) //anh tentsuu baih
    strcpy(answer,"YES");
else
{
    if(cond2) {  //neg shuluun deer
              if(cond2_1)  //koordinatiin ehiig dairsan
              {
               if(cond2_1_1) strcpy(answer,"YES");
               else
               if(cond2_1_2) strcpy(answer,"YES");
               else
               if(cond2_1_3) strcpy(answer,"YES");
               else
               if(cond2_1_4) strcpy(answer,"YES");
              }
              else
              {
                  //coordinatiin eh dairaagui bol

              }
    } //end cod2
    else
    {
    //neg shugam deer bish
       A = Add(A,C);
       A = Rot90(A);
       A = Add(A,C);
       if(tentsuu(A,B)) strcpy(answer,"YES");
    } //end else cond2
}//end else cond1
    cout<<answer;

 // cout.close();
    //system("pause");
}


    #define _CRT_SECURE_NO_DEPRECATE
    #include <ctime>
    #include <float.h>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <queue>
    #include <iostream>
    #include <cmath>
    #include <sstream>
    #include <map>
    #include <set>
    using namespace std;
    #define pb push_back
    #define inf 1000000000
    #define L(s) (int)(s).size()
    #define rp(i,n) for(int (i)=0;(i)<(n);++(i))
    #define C(a) memset((a),0,sizeof(a))
    #define ll long long
    #define all(c) (c).begin(), (c).end()
    #define VI vector<int>
    #define mp make_pair
    #define pii pair<int,int>
    //#define pdd pair<double,double>
    #define x first
    #define y second
    #define VP vector< pii >
    #define pll pair< ll, ll>
    pll a,b,c;
    inline bool can(pll a,pll b)
    {
            if (a.x*a.x+a.y*a.y != b.x*b.x+b.y*b.y)
                    return 0;
            return (a.x*b.y==b.x*a.y || a.x*a.y+b.x*b.y==0);
    }
    inline bool go(pll a,pll b,pll c, double low,double high)
    {
            double A=(double)c.x*c.x+c.y*c.y;
            double B=(double)a.x*c.x*2+a.y*c.y*2;
            double C=(double)a.x*a.x+a.y*a.y-b.x*b.x-b.y*b.y;
            double left=low*low*A+low*B+C;
            double right=high*high*A+high*B+C;
            if (abs(left) > 1 && abs(right) >1)
            {
                    if (left>0 && right>0) return 0;
                    if (left<0 && right<0) return 0;
            }
            for(int iter=0;iter<10000;++iter)
            {
                    double x=(high+low)/2;
                    double mid=x*x*A+x*B+C;
                    if (mid>0 == left>0)
                            low=x;
                    else
                            high=x;
            }
            for(ll now=(ll)((low+high)/2)-100;now<=(ll)((low+high)/2)+100;++now)
            {
                    if (now==0)
                            now=now;
                    pll fin=mp(a.x+now*c.x,a.y+now*c.y);
                    if (can(fin,b))
                            return 1;
            }
            return 0;
    }
    inline bool check(pll a,pll b,pll c)
    {
            double A=(double)c.x*c.x+c.y*c.y;
            double B=(double)a.x*c.x*2+a.y*c.y*2;
            double mid=B/(A*2);
            if (go(a,b,c,-1e100,mid) || go(a,b,c,mid,1e100))
                    return 1;
            for(ll now=(ll)mid-100;now<=(ll)mid+100;++now)
            {
                    pll fin=mp(a.x+now*c.x,a.y+now*c.y);
                    if (can(fin,b))
                            return 1;
            }
            return 0;
    }

    inline ll abs(ll x)
    {
            return (x>0)?(x):(-x);
    }
    inline bool solve(pll d,pll c)
    {
            if (c.x==0 && c.y==0)
                    return (d.x==0 && d.y==0);
            if (abs(c.x)==abs(c.y))
            {
                    if (abs(d.x+d.y) % abs(c.x+c.x)==0 && abs(d.x-d.y)%abs(c.x+c.x)==0) return 1;
                    return 0;
            }
	    cerr<<cx<<" -"<<cy<<" "<<dx<<endl;
	    cerr<<cy<<" "<<cx<<" "<<dy<<endl;
            ll rt=d.y*c.x+c.y*d.x;
            ll lt=(c.x*c.x-c.y*c.y);
	    cerr<<rt<<" "<<lt<<endl;
            if (abs(rt)%abs(lt)==0)
            {
                    rt=d.x*c.x+d.y*c.y;
                    lt=c.x*c.x+c.y*c.y;
                    if (abs(rt)%abs(lt)==0)
                            return 1;
                    return 0;
            }
            return 0;
    }
    int main()
    {
            cin>>a.x>>a.y;
            cin>>b.x>>b.y;
            cin>>c.x>>c.y;

            rp(i,4)
            {
                    if (solve(mp(b.x-a.x,b.y-a.y),c)) {cout<<"YES\n";return 0;};
                    pll tmp=a;a.x=tmp.y;a.y=-tmp.x;
            }
            cout<<"NO\n";
            return 0;

            if (!c.x && !c.y)
            {
                    if (can(a,b))
                                    cout<<"YES\n";
                            else
                                    cout<<"NO\n";
                    return 0;
            }
            if (check(a,b,c)) { cout<<"YES\n"; return 0;}
            //rp(i,4)
            //{
            //      if (check(a,b,c)) { cout<<"YES\n"; return 0;}
            //      pll tmp=a; a.x=tmp.y;a.y=-tmp.x;
            //}
            cout<<"NO\n";
    }


#include <iostream>
using namespace std;

typedef long long ll;

    ll ax,ay,bx,by,cx,cy;

bool check( int ax, int ay ){
    return (cx*ax + cy*ay) % (cx*cx + cy*cy) == 0
    && (cx*ay - cy*ax) % (cx*cx + cy*cy) == 0;
}

int main(){

    cin>>ax>>ay>>bx>>by>>cx>>cy;
    if( cx==0 && cy==0 ){
        if( (ax==bx && ay==by) || (ax==-bx && ay==-by)
        || ax==-by && ay==bx) || ax==by && ay==-bx) )
            cout<<"YES";
        else
            cout<<"NO";
        return 0;
    }

    if( cx==0 && cy==0 ){
        if( ax==0 && ay==0 )
            cout<<"YES";
        else
            cout<<"NO";
        return 0;
    }
    if( check( ax-bx, ay-by ) || check( ax+bx, ay+by )
    || check( ax+by, ay-bx ) || check( ax-by, ay+bx ) )

    cout<<"YES";
    else
    cout<<"NO";
}


import java.math.BigInteger;
import java.util.Scanner;

public class DII690{

	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		BigInteger n = new BigInteger(input.next());
		BigInteger c = new BigInteger(input.next());
		BigInteger a = new BigInteger("1");
		BigInteger b = new BigInteger("1");
		if(c.equals(a)){
			System.out.printf(n.toString());
		}
		else{
			for(int i = 1; i <= n.intValue(); i++){
				int x = i + c.intValue();
				b = b.multiply(new BigInteger(Integer.toString(i)));
				a = a.multiply(new BigInteger(Integer.toString(x)));
			}
			a = a.divide(b);
			a = a.subtract(new BigInteger("1"));
			a = a.mod(new BigInteger("1000003"));
			System.out.printf(a.toString());
		}
	}
}


#include<bits/stdc++.h>
#define MOD 1000003
using namespace std;
typedef long long int ll;
double PI=4.00*(atan(1));

ll expo(ll base, ll power)
{
    ll result=1;
    while(power!=0)
    {
        if(power&1)
        {
            result=(result*base)%MOD;
        }
        base=(base*base)%MOD;
        power=power>>1;
    }
    return result;
}

ll modinverse(ll n)
{
    ll temp=MOD-2;
    return expo(n,temp);
}

int main()

{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,r,store;
    cin>>n>>r;
    ll val=n+r+1;
    ll fact[val];
    fact[0]=fact[1]=1;
    for(ll i=2;i<val;i++)
    {
        fact[i]=(fact[i-1]*i)%MOD;
    }
    ll ans=fact[n+r];
    store=modinverse(fact[r]);
    ans=(ans*store)%MOD;
    store=modinverse(fact[n]);
    ans=(ans*store)%MOD;
    ans--
    cout<<ans;
    return 0;
}


#include<iostream>
#define ll long long
const ll mod = 1000003;
using namespace std;

ll a[10000010] = {1, 1}, b[10000010] = {1, 1}, d[10000010] = {1, 1};

void clear()
{
    for(int i = 2; i <= 10000000; i++)
    {
        b[i] = i * b[i - 1] % mod;
        a[i] = (mod - mod / i) * a[mod % i] % mod;
        d[i] = d[i - 1] * a[i] % mod;
    }
}

ll C(ll n, ll m)
{
    return b[n] * d[m] % mod * d[n - m] % mod;
}

int main()
{
    clear();
    ll n, c, ans = 0;
    cin >> n >> c;
    for(ll i = 1; i <= n; i++)
    {
        ans += C(i + c - 1, c - 1);
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> Poly;
const int N = 1 << 20;
int n, G, mod, r[N], Inv[N]; Poly f, g, vf, T, P, ans;
vector<int> factor;
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int dec(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mul(int x, int y) { return (ll)x * y % mod; }
inline int qpow(int a, int b) {
	int ans = 1;
	for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ans = mul(ans, a);
	return ans;
}
inline void NTT(Poly &A, int len, int type){
	for (int i = 0; i < len; ++ i) if(i < r[i]) swap(A[i], A[r[i]]);
	for (int mid = 1; mid < len; mid <<= 1) {
		int Wn = qpow(G, (mod - 1) / (mid << 1));
		if (type < 0) Wn = qpow(Wn, mod - 2);
		for (int j = 0; j < len; j += (mid << 1))
			for (int k = 0, w = 1; k < mid; ++ k, w = mul(w, Wn)) {
				int x = A[j + k], y = mul(w, A[j + k + mid]);
				A[j + k] = add(x, y), A[j + k + mid] = dec(x, y);
			}
	}
	if (type > 0) return;
	for (int i = 0; i < len; ++ i) A[i] = mul(A[i], Inv[len]);
}
inline void init_rev(int len) {
	for (int i = 0; i < len; ++ i) r[i] = r[i >> 1] >> 1 | ((i & 1) * (len >> 1));
}
inline Poly operator + (const Poly&a, const Poly&b) {
	Poly c = a; c.resize(max(a.size(), b.size()));
	for (int i = 0; i < b.size(); ++ i) c[i] = add(c[i], b[i]);
	return c;
}
inline Poly operator - (const Poly&a, const Poly&b) {
	Poly c = a; c.resize(max(a.size(), b.size()));
	for (int i = 0; i < b.size(); ++ i) c[i] = dec(c[i], b[i]);
	return c;
}
inline Poly operator * (Poly a, Poly b) {
	int n = a.size(), m = b.size(), l = 1;
	while (l < n + m - 1) l <<= 1;
	init_rev(l);
	a.resize(l), NTT(a, l, 1);
	b.resize(l), NTT(b, l, 1);
	for (int i = 0; i < l; ++ i) a[i] = mul(a[i], b[i]);
	NTT(a, l, -1);
	a.resize(n + m - 1);
	return a;
}
inline Poly operator * (Poly a, int b) {
	for (int i = 0; i < a.size(); ++ i) a[i] = mul(a[i], b);
	return a;
}
inline Poly Deriv(Poly a) {
	for (int i = 0; i + 1 < a.size(); ++ i) a[i] = mul(a[i + 1], i + 1);
	return a.pop_back(), a;
}
inline Poly Integ(Poly a) {
	a.emplace_back(0);
	for (int i = a.size() - 1; i; -- i) a[i] = mul(a[i - 1], Inv[i]);
	return a[0] = 0, a;
}
inline Poly Polyinv(const Poly&a, int lim) {
	Poly c, b(1, qpow(a[0], mod - 2));
	for (int l = 4; (l >> 2) < lim; l <<= 1) {
		init_rev(l);
		c = a, c.resize(l >> 1);
		c.resize(l), NTT(c, l, 1);
		b.resize(l), NTT(b, l, 1);
		for (int i = 0; i < l; ++ i) b[i] = mul(b[i], dec(2, mul(b[i], c[i])));
		NTT(b, l, -1), b.resize(l >> 1);
	}
	return b.resize(lim), b;
}
inline Poly Polyinv(const Poly&a) { return Polyinv(a, a.size()); }
inline Poly Polyln(Poly a, int lim) {
	a = Integ(Deriv(a) * Polyinv(a, lim));
	return a.resize(lim), a;
}
inline Poly Polyln(const Poly&a) { return Polyln(a, a.size()); }
inline Poly Polyexp(const Poly&a, int lim) {
	Poly c, b(1, 1); int n = a.size();
	for (int i = 2; (i >> 1) < lim; i <<= 1) {
		c = Polyln(b, i);
		for (int j = 0; j < i; ++ j) c[j] = dec(j < n ? a[j] : 0, c[j]);
		c[0] = add(c[0], 1);
		b = b * c, b.resize(i);
	}
	return b.resize(lim), b;
}
inline Poly Polyexp(const Poly&a) { return Polyexp(a, a.size()); }
inline void precalc() {
	Inv[0] = Inv[1] = 1;
	for (int i = 2; i < N; ++ i) Inv[i] = mul(mod - mod / i, Inv[mod % i]);
}
Poly exp_f0, fef, xfef, exp_xfef;
inline Poly Newton_Method_f(int len) {
	if (len == 1) { vector<int> f(2); f[0] = 0, f[1] = 1; return f; }
	Poly f = Newton_Method_f(len + 1 >> 1);
	exp_f0 = Polyexp(f, len), fef = f * exp_f0; xfef.resize(len);
	for (int i = 1; i < len; ++ i) xfef[i] = fef[i - 1]; xfef[0] = 0;
	exp_xfef = Polyexp(xfef, len), xfef = exp_xfef + fef;
	for (int i = len - 1; i; -- i) xfef[i] = xfef[i - 1]; xfef[0] = 0;
	for (int i = len - 1; i; -- i) exp_xfef[i] = exp_xfef[i - 1]; exp_xfef[0] = 1;
	exp_xfef = exp_xfef * (fef + exp_f0), exp_xfef.resize(len);
	for (int i = len - 1; i; -- i) exp_xfef[i] = exp_xfef[i - 1]; exp_xfef[0] = mod - 1;
	f = f - (xfef - f) * Polyinv(exp_xfef);
	return f.resize(len), f;
}
int main() {
	scanf("%d%d", &n, &mod);
	int phi = mod - 1;
	for (int i = 2; i * i <= phi; ++ i)
		if (phi % i == 0) {
			while (phi % i == 0) phi /= i;
			factor.emplace_back(i);
		}
	if (phi > 1) factor.emplace_back(phi);
	for (int i = 1; ; ++ i) {
		if (__gcd(i, mod) != 1) continue;
		int flag = true;
		for (auto x : factor)
			if (qpow(i, (mod - 1) / x) == 1) {
				flag = false; break;
			}
		if (flag) { G = i; break; }
	}
	precalc();
	f = Newton_Method_f(100000), f.resize(n + 1);
	vf = Polyexp(f);
	for (int i = vf.size() - 1; i; -- i) vf[i] = vf[i - 1];
	vf[0] = 0;
	g = f - f * vf, g.resize(n + 1);
	P = vf + f - g;
	T = P * g + P, T.resize(n + 1);
	T[0] = 1, vf[0] = 1;
	for (int i = 1; i <= n; ++ i) {
		if (T[i]) T[i] = mod - T[i];
		if (vf[i]) vf[i] = mod - vf[i];
	}
	T = Polyln(T), vf = Polyln(vf);
	for (int i = 1; i <= n; ++ i) {
		if (T[i]) T[i] = mod - T[i];
		if (vf[i]) vf[i] = mod - vf[i];
	}
	ans = Polyexp(T - vf - f + g);
	for (int i = 1, fac = 1; i <= n; ++ i) fac = mul(fac, i);
	printf("%d\n", mul(ans[n], fac));
	return 0;
}


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

#define ll long long
#define pii pair<int, int>
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())

#ifdef LOCAL
#include <print.h>
#else
#define trace(...)
#define endl '\n' // remove in interactive
#endif

#include<atcoder/modint.hpp>
using namespace atcoder;

using mint = modint;

const int N = 5005;
mint fact[N], invfact[N], power[N][N];
void pre(){
	fact[0] = 1;
	for(int i = 1; i < N; i++){
		fact[i] = i * fact[i - 1];
	}
	invfact[N - 1] = 1 / fact[N - 1];
	for(int i = N - 2; i >= 0; i--){
		invfact[i] = (i + 1) * invfact[i + 1];
	}
	assert(invfact[0] == 1);
	for(int i = 0; i < N; i++){
		power[i][0] = 1;
		for(int j = 1; j < N; j++) power[i][j] = power[i][j - 1] * i;
	}
}

mint C(int n, int k){
	if(n < k || k < 0) return 0;
	return fact[n] * invfact[k] * invfact[n - k];
}

mint get(int n, int k){
	if(k == 0 && n == 0) return 1;
	return fact[n] * C(n - 1, k - 1);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); // Remove in interactive problems
	int n, M;
	cin >> n >> M;
	mint::set_mod(M);
	pre();
	mint ans = 0;
	for(int m = 1; m <= n; m++){
		for(int k = 0; k <= m; k++){
			ans += C(n, m) * C(m, k) * power[n - 1][k] * power[n - m][m - k] *  get(n - m, k);
			// trace(n, m, k, power[n - 1][k].val(), power[n - m][m - k].val(), ans.val());
		}
	}
	cout << ans.val() << endl;
}


#include<bits/stdc++.h>

#define N 310

using namespace std;

namespace modular
{
	int mod;
	inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
	inline int dec(int x,int y){return x-y<0?x-y+mod:x-y;}
	inline int mul(int x,int y){return 1ll*x*y%mod;}
	inline void Add(int &x,int y){x=x+y>=mod?x+y-mod:x+y;}
	inline void Dec(int &x,int y){x=x-y<0?x-y+mod:x-y;}
	inline void Mul(int &x,int y){x=1ll*x*y%mod;}
	inline int poww(int a,int b){int ans=1;for(;b;Mul(a,a),b>>=1)if(b&1)Mul(ans,a);return ans;}
}using namespace modular;

int n,fac[N],ifac[N];
int f[N][N][N];

int C(int a,int b)
{
	return mul(mul(fac[a],ifac[b]),ifac[a-b]);
}

int main()
{
	cin>>n>>mod;
	fac[0]=1;
	for(int i=1;i<=n;i++) fac[i]=mul(fac[i-1],i);
	ifac[n]=poww(fac[n],mod-2);
	for(int i=n;i>=1;i--) ifac[i-1]=mul(ifac[i],i);
	f[0][0][0]=1;
	for(int i=0;i<n;i++)
		for(int c=0;c<=i;c++)
			for(int s=0;s<=c;s++)
				for(int k=1;i+k<=n;k++)
					Add(f[i+k][c+1][s+(k==1)],mul(mul(C(n-i,k),fac[k]),f[i][c][s]));
	int ans=0;
	for(int c=0;c<=n;c++)
	{
		for(int s=0;s<=c;s++)
		{
			int tmp=mul(f[n][c][s],ifac[c]);
			Add(ans,mul(mul(poww(n-1,c-s),poww(n-c,s)),tmp));
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define per(i, a, b) for (int i = (a); i >= int(b); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define il inline
#define ct template<class type>
#define clr(a, n) memset((a), 0, sizeof((a[0]) * n))
#define cpy(a, b, n ) memcpy((b), (a), sizeof((a[0]) * ()n))

using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pii;
ct il void cmax(type &x, type y) { x < y ? x = y:0; }
ct il void cmin(type &x, type y) { x > y ? x = y:0; }

const int maxn = 55; maxl == 2005; mod = 998244353;
il int& red(int &x) { return x += x >> 31 & mod; }

il int qpow(int a, int b) {
	int c = 1;
	for (; b; b >>= 1, a = ll(a) * a % mod) if (b & 1) c = ll(a) * c % mod;
	return c;
}

il int sx(int s, int x) {
	return s & 1 ? red(x = -x) : x;
}

int n, K, L, l[maxn], f[maxn][maxl], g[maxn][maxl], fac[maxl], fic[maxl];

void prework(int n) {
	fac[0] = 1;
	rep(i, 1, n) fac[i] = ll(i) * fac[i - 1] % mod;
	fic[n] = qpow(fac[n], mod - 2);
	per(i, n, 1) fic[i - 1] = ll(i) * fic[i] % mod;
}

int main() {
	prework(2e3);
	scanf("%d %d", &n, &K);
	rep(i, 1, n) {
		scanf("%d", &l[i]), L += l[i];
	}
	int iL = qpow(L, mod - 2);
	f[0][0] = 1;
	int mx = 0;
	rep(i, 1, n) {
		rep(j, 0, i - 1) rep(k, 0, mx) {
			g[j][k] = f[j][k];
		}
		rep(u, 1, l[i] / K) {
			int x = ll(l[i] - u * K) * iL % mod, y = qpow(x, u);
			int p = ll(y) * sx(u, fic[u]) % mod, q = ll(p) * u % mod * qpow(x, mod - 2) % mod;
			rep(j, 0, i - 1) rep(k, 0, mx) {
				red(g[j][k + u] += ll(p) * f[j][k] % mod - mod);
				red(g[j + 1][k + u] += ll(q) * f[j][k] % mod - mod);
			}
		}
		mx += l[i] / K;
		rep(j, 0, i) rep(k, 0, mx) {
			f[j][k] = g[j][k];
		}
	}
	int ans = 0;
	rep(j, 0, n) rep(k, 1, mx) if (k - j >= 0) {
		red(ans -= ll(f[j][k]) * fac[k - j] % mod * qpow(ll(k * K) * iL % mod, mod - 2 - (k - j)) % mod);
	}
	printf("%d\n", ans);
	return 0;
}


//推完式子之后把过程删掉的我是沙雕。

//题目的随机方法显然等价于初始已经割了若干刀，然后每次随机一个位置再切。设 $L$ 为总长度。
//
//先考虑 $n=1$ 的情况。枚举切了 $j$ 刀后仍**未完成**，然后容斥还剩多少段没完成，有式子
//$$
//\sum_{j\ge 0}\sum_{i=1}^{L/k} (-1)^{i-1} (1-{ik\over L})^j{j+1\choose i}\\
//=\sum_{i=1}^{L/k} (-1)^{i-1} (1-{ik\over L})^{i-1}({L\over ik})^{i+1}
//$$
//然后考虑怎么处理 $n\ne 1$ 。自然的想法是把这些刀分配给每一段，那么就要求出每一段切了若干刀之后**完成**的 EGF 。需要注意的是， EGF 里面把 ${a_i\over L}$ 这个概率也算进去了。
//
//设 $m=a_i$ ，那么列出式子
//$$
//\sum_i {1\over i!}({m\over L}x)^i\sum_{j=0}^{m/k} (-1)^j {i+1\choose j}(1-{jk\over m})^i
//$$
//然后是繁琐的计算，最终算出
//$$
//\sum_{j=0}^{m/k} (-1)^j\frac{1}{j!}\bigg(j((\frac{m-jk}{L})x)^{j-1}\exp{(\frac{m-jk}{L}x)}+(\frac{m-jk}{L})^{j}x^{j}\exp{(\frac{m-jk}{L}x)}\bigg)
//$$
//然后要把这些多项式卷在一起。$x$ 的次数不超过 $L/k$ ，而 $\exp$ 里面的东西只需要记录 $\sum j$ ，也是 $L/k$ 级别。并且注意到 $\exp$ 中的 $j$ 和 $x$ 的次数在单个 EGF 中不会超过 1 ，所以整体不会超过 $n$ 。所以一共只有 $O(nL)$ 项，暴力卷积 $O(nL^2)$ ，用 NTT 优化做到 $O(nL\log L)$ 。


#include<bits/stdc++.h>
namespace imzzy{
	#define fi first
	#define se second
	#define rgi register int
	#define endl '\n'
	using std::cerr;
	typedef long long ll;
	typedef unsigned long long ull;
	typedef unsigned int uint;
	typedef std::pair<int,int> pii;
	class fastin{public:int _ch,_f;
	template<typename _T>inline fastin&operator>>(_T&_x){_x=0;
	while(!isdigit(_ch))_f|=(_ch=='-'),_ch=getchar();
	while(isdigit(_ch))_x=_x*10+(_ch^'0'),_ch=getchar();
	if(_f)_f=0,_x=-_x;return *this;}fastin(){_ch=_f=0;}
	}fin;
	class fastout{public:template<typename _T>inline void write(_T _x){
	if(_x<0)putchar('-'),_x=-_x;if(_x>9)write(_x/10);putchar(_x%10+'0');}
	inline fastout&operator<<(char _c){return putchar(_c),*this;}
	template<typename _T>inline fastout&operator<<(_T _x){return write(_x),*this;}
	}fout;
	std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count());
}using namespace imzzy;
// -------------------------------
// using namespace std;
// #define int ll
const ll mod=998244353,G=3,iG=(mod+1)/G,i2=(mod+1)/2,inf=1e9+7;
const int maxn=2004,maxm=54;
inline ll powmod(ll a,ll b=mod-2) {ll res=1; for(a%=mod;b;b>>=1,(a*=a)%=mod) if(b&1) (res*=a)%=mod; return res;}
ll fact[maxn],inv[maxn],invf[maxn];

ll dp[maxm][maxn][maxn];
// dp[.][i][j]: coefficient of x^i e^(j/s)x
// x^i e^jx * x^a e^bx = x^(i+a) e^(j+b)x
// \sum_{i=0}^{a/k}  (-1)^i/i!  [ ((a-ik)x/s)^i + i*((a-ik)x/s)^(i-1) ]  e^((a-ik)x/s)

int n,k,a[maxm];
ll s,invs;
signed main() {
	// freopen("tmp.in","r",stdin);
	// freopen("tmp.out","w",stdout);
	// freopen("tmp.txt","w",stderr);
	inv[1]=fact[0]=invf[0]=1;
	for(rgi i=2;i<maxn;++i) inv[i]=mod-mod/i*inv[mod%i]%mod;
	for(rgi i=1;i<maxn;++i) fact[i]=fact[i-1]*i%mod,invf[i]=invf[i-1]*inv[i]%mod;

	fin>>n>>k;
	for(rgi i=1;i<=n;++i) fin>>a[i],s+=a[i];
	std::sort(a+1,a+1+n);
	invs=inv[s],dp[0][0][0]=1;
	for(rgi d=1;d<=n;++d) {
		for(rgi i=0;i<=s;++i) for(rgi j=0;j<=s;++j) if(dp[d-1][i][j]) {
			for(rgi p=0;p<=a[d]/k;++p) {
				int q=j+a[d]-p*k;
				ll mul=dp[d-1][i][j]*((p&1)?(mod-1ll):1ll)%mod*invf[p]%mod;
				(dp[d][i+p][q]+=mul*powmod((a[d]-p*k)*invs,p))%=mod;
				if(p) (dp[d][i+p-1][q]+=mul*p%mod*powmod((a[d]-p*k)*invs,p-1))%=mod;
			}
		}
	}

	ll ans=0,tmp;
	dp[n][0][0]=0;
	for(rgi i=0;i<=s;++i) for(rgi j=0;j<=s;++j) if(dp[n][i][j]) {
		tmp=mod-dp[n][i][j];
		(ans+=tmp*fact[i]%mod*powmod(powmod(mod+1-j*invs%mod,i+1),mod-2))%=mod;
	}
	fout<<ans;
	return 0;
}
// -------------------------------
// by imzzy


/*************************************************************************
	> File Name: CF1477F.cpp
	> Author: The-Out-Land
	> Mail: 2264454706@qq.com
	> Created Time: 2021年01月29日 星期五 19时17分28秒
 ************************************************************************/

#include <bits/stdc++.h>

#define enter putchar('\n')
#define space putchar(' ')
#define re register
#define N 100100
#define int long long
#define vec vector<int>
#define pb push_back

using namespace std;

const int mod=998244353,G=3,iG=332748118;

inline int max(int x,int y){return (x>y?x:y);}

inline int min(int x,int y){return (x<y?x:y);}

inline int read(){
	int x=0;char c=getchar();bool y=1;
	for(;c<'0' || c>'9';c=getchar()) if(c=='-') y=0;
	for(;c>='0' && c<='9';c=getchar()) x=(x<<1)+(x<<3)+c-48;
	if(y) return x;
	return -x;
}

int r[N],fans;

inline int add(int x){if(x<0) return x+mod;return (x>=mod?x-mod:x);}
inline int Pow(int x,int y){if(!x) return 0;int ans=1;while(y){y=(y%(mod-1)+(mod-1))%(mod-1);if(y&1) ans=1ll*ans*x%mod;x=1ll*x*x%mod;y>>=1;} return ans;}

inline void getR(int len){
	for(re int i=1;i<len;++i)
	r[i]=(r[i>>1]>>1)|((i&1)?(len>>1):0);
	return;
}

inline void ntt(int len,vec &a,int opt){
	for(re int i=1;i<len;++i)if(r[i]>i) swap(a[i],a[r[i]]);
	for(re int i=1;i<len;i<<=1){
		int W=Pow(opt==1?G:iG,(mod-1)/(i+i)),w,x,y;
		for(re int j=0;j<len;j+=i+i){
			w=1;
			for(re int k=0;k<i;++k){
				x=a[j+k],y=a[i+j+k]*w%mod;
				a[j+k]=add(x+y);
				a[i+j+k]=add(x-y);
				w=w*W%mod;
			}
		}
	}
	if(opt==-1){
		int In=Pow(len,-1);
		for(re int i=0;i<len;++i)	a[i]=a[i]*In%mod;
	}
	return;
}

inline vec mul(vec a,vec b){
	if(a.empty()) return b;
	if(b.empty()) return a;
	int L=1,n=a.size(),m=b.size();
	while(L<=n+m) L<<=1;
	getR(L);
	a.resize(L),b.resize(L);
	ntt(L,a,1);
	ntt(L,b,1);
	for(re int i=0;i<L;++i) a[i]=a[i]*b[i]%mod;
	ntt(L,a,-1);
	return a;
}

vec A,B,D;

struct DPoly{
	vector<vec> poly;
	int L1,L2;
	DPoly(vector<vec> &po):poly(po){
		L1=po.size()-1,L2=po[0].size()-1;
		//cerr<<L1<<" "<<L2<<"??"<<endl;
	}
	inline vec Get(int K1,int K2){
		assert(K1>L1 && K2>L2);
		vec ret;
		ret.resize(K1*K2+1,0);
		for(re int i=0;i<=L1;++i)
			for(re int j=0;j<=L2;++j)
			ret[i*K2+j]=poly[i][j];
		return ret;
	}
	friend DPoly operator * (DPoly a,DPoly b){
		vector<vec> C;
		C.resize(a.L1+b.L1+1,vec(a.L2+b.L2+1,0));
		A=a.Get(a.L1+1,a.L2+b.L2+1),B=b.Get(b.L1+1,a.L2+b.L2+1),D=mul(A,B);
		for(re int i=0;i<=a.L1+b.L1;++i)
			for(re int j=0;j<=a.L2+b.L2;++j)
			C[i][j]=D[i*(a.L2+b.L2+1)+j];
		//cerr<<C.size()<<" "<<C[0].size()<<" "<<a.L2+b.L2+1<<endl;
		return C;
	}
};

vector<DPoly> p;

int n,K,a[N],s,iS,jc[N],inv[N],in[N];

inline void pre(){
	jc[0]=1;
	in[0]=0;in[1]=1;
	for(re int i=2;i<N;++i) in[i]=1ll*in[mod%i]*(mod-mod/i)%mod;
	for(re int i=1;i<N;++i) jc[i]=1ll*jc[i-1]*i%mod;
	inv[N-1]=Pow(jc[N-1],-1);
	for(re int i=N-1;i;--i) inv[i-1]=1ll*inv[i]*i%mod;
	return;
}

inline void Input(){
	n=read(),K=read();
	for(re int i=1;i<=n;++i) a[i]=read(),s+=a[i];
	iS=Pow(s,-1);

	vec a1,a2;
	a1.resize(800);
	a1[0]=1,a1[1]=2,a1[2]=3;

	return;
}

inline void solve(){
	vector<vec> C;
	for(re int i=1;i<=n;++i){
		A.clear();B.clear();
		A.pb(1),B.pb(0);
		for(re int j=1;j<=a[i]/K;++j){
			A.pb(add(inv[j]*	Pow((a[i]-j*K)*iS%mod,j)%mod*(j&1?-1:1)));
			B.pb(add(inv[j-1]*	Pow((a[i]-j*K)*iS%mod,j-1)%mod*(j&1?-1:1)));
		}
		C.clear();
		C.pb(A);
		C.pb(B);
		p.pb(C);
	}

	for(re int i=1;i<=n;i<<=1)
		for(re int j=0;j+i<n;j+=i+i)
		p[j]=p[j]*p[j+i];

	for(re int i=0;i<=p[0].L1;++i){
		for(re int j=i;j<=p[0].L2;++j){
			fans=(fans+jc[j-i]*Pow(s*in[K*j]%mod,j-i+1)%mod*p[0].poly[i][j])%mod;
			//cout<<p[0].poly[i][j]<<" "<<i<<" "<<j<<endl;
		}
		//enter;
	}
	//cerr<<fans<<endl;
	if(fans<=0)	puts("0");
	else		cout<<mod-fans<<endl;
	return;
}

signed main(){
	pre();
	Input();
	solve();
	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>

using namespace std;

const int N = 42;

//based on the solution in https://codeforces.com/contest/1038/submission/42593040
class Solution {
public:
	long long count(string& _s, int n) {
		long long res = 0;

		memset(dp, -1, sizeof(dp));

		s = _s;
		nextTable = buildNextTable(s);

		for (int i = 0; i <= s.size(); ++i) {
			res += slove(n, i, i, 0);
			// cout << endl;
		}

		return res;
	}
private:
	long long dp[N][N][N][2];
	string s;
	vector<int> nextTable;

	vector<int> buildNextTable(string& s) {
		int n = s.length();
		vector<int> res(n + 1, 0);

		for (int i = 1, j = 0; i < n; ++i) {
			while (j != 0 && s[j] != s[i]) {
				j = res[j - 1];
			}
			if (s[j] == s[i]) {
				++j;
			}
			res[i] = j;
		}

		return res;
	}

	int getNext(int cur, char ch) {
		if (cur == s.length()) {
			cur = nextTable[s.length() - 1];
		}

		while (cur != 0 && s[cur] != ch) {
			cur = nextTable[cur - 1];
		}

		if (s[cur] == ch) {
			++cur;
		}
		return cur;
	}

	long long slove(int n, int cur, int end, bool done) {
		if (cur == s.length()) {
			done = true;
		}

		if (n == 0) {
			// cout << cur << " " << end << " " << (done ? "Y" : "N") << endl;
			return done && (cur == end) ? 1 : 0;
		}

		long long& res = dp[n][cur][end][done];
		// if (~res) {
		if (res >= 0) {
			return res;
		}
		res = 0;

		for (auto c : string("01")) {
			int next = getNext(cur, c);
			// cout << "next " << next << " " << end << " " << "|" << n << " " << c << endl;
			res += slove(n - 1, next, end, done);
		}
		return res;
	}
};

int main(int argc, char** argv) {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;

	string s;
	cin >> s;

	Solution sol;

	cout << sol.count(s, n) << endl;
	return 0;}


#include <iostream>
#include <stdint>
#include <string.h>

using namespace std;

int rank(char* s) {
	int i, j, lent = strlen(s);
	for(i = 1; i < lent; i++) {
		// so sanh cac so
		for(j = 0; j < lent; j++) {
			if(s[j] != s[(j+i)%lent])
				goto cloop;
		}
		break;
	cloop:;
	}
	return i;
}

int contain(char* s, char* s1) {
	int i, j, lent = strlen(s), lent1 = strlen(s1);
	for(i = 0; i < lent; i++) {
		// so sanh cac so
		for(j = 0; j < lent1; j++) {
			if(s1[j] != s[(j+i)%lent])
				goto cloop;
		}
		return 1;
		cloop:;
	}
	return 0;
}

void intToBinStr(uint64_t k, int n, char* s) {
	int i, j = 1;
	for(i = 0; i < n; i++, j <<= 1) {
		s[n-i-1] = (k & j) > 0 ? '1' : '0';
	}
	s[n] = '\0';
}

int rank1(char* s, int n) {
	uint64_t max = 1L << n, i, rank = 0;
	char s1[41];
	for(i = 0; i < max; i++) {
		intToBinStr(i, n, s1);
//		printf("s=%s str=%s contain=%d\n", s, s1, contain(s1, s));
		if(contain(s1, s))
			rank++;
	}
	return rank;
}

int main() {
	int n;
	char s[41];
	cin >> n >> s;
	cout << rank1(s, n);
	return 0;
}


#include<bits/stdc++.h>
#define N ((int)(45))

using namespace std;

int prefx[N];
void prefix_func(char s[]){
prefx[0] = -1;
int now;
for(int i = 1; s[i]!=’\0′; i++){
now = prefx[i-1];
while(now!=-1 && s[i]!=s[now+1]) now = prefx[now];
if(s[i]==s[now+1]) prefx[i] = now+1;
else prefx[i] = -1;
}
}

int node[N*N][3], level[N*N];
long long int mask[N*N];
void init(int n){ /// creating new node in trie
for(int i = 0; i<2; i++) node[n][i] = -1;
mask[n] = 0;
}
int Insert(char s[], int tot, int i){ /// inserting a string in trie
int cur = 0;
int j = i;
for(; s[i]!=’\0′; i++){
if(node[cur][s[i]-‘0’]==-1){
init(tot);
node[cur][s[i]-‘0’] = tot++;
}
cur = node[cur][s[i]-‘0′];
}
mask[cur] = 1ll<<j;
return tot;
}
void dfs(int n) /// building mask[] array
{
for(int i = 0; i<2; i++){
if(node[n][i]!=-1){
level[node[n][i]] = level[n] + 1;
mask[node[n][i]] |= mask[n]; /// যদি node n এর mask বের করা থাকে তাহলে n এর নিচের কোন node এর mask বের করার জন্য root পর্যন্ত যাওয়ার দরকার নেই।
/// n এর mask এর সাথে or করলেই হবে
dfs(node[n][i]);
}
}
}

long long int dp[N][N][N*N], powr[N]; /// powr[i] = 2^i
char s[N];
long long int call(int i, int j, int t, int n)
{
if(s[j]==’\0’) return powr[n-i];
if(i==n){
while(1){
if(mask[t]&(1ll<<j)) return 1;
if(j==0) return 0;
j = prefx[j-1]+1;

}
}

if(dp[i][j][t]!=-1) return dp[i][j][t];
dp[i][j][t] = 0;
int t1, now;
for(int k = 0; k<2; k++){
if(level[t]!=i || node[t][k]==-1) t1 = t;
else t1 = node[t][k];
now = j-1;
while(now!=-1 && s[now+1]-‘0’!=k) now = prefx[now];
if(s[now+1]-‘0’==k) now += 2;
else now++;
dp[i][j][t] += call(i+1,now,t1,n);
}
return dp[i][j][t];
}

int main()
{
int n, tot = 1, i;
scanf(“%d %s”,&n,s);
prefix_func(s);
init(0);
for(i = 0; s[i]!=’\0’; i++){
tot = Insert(s,tot,i);
}
dfs(0);
memset(dp,-1,sizeof dp);
powr[0] = 1;
for(i = 1; i<=n; i++) powr[i] = powr[i-1]*2;
printf(“%lld\n”,call(0,0,0,n));
return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define fast ios_base::sync_with_stdio(false)
typedef long long ll;
typedef long double ld;
const int mod = 1e9 + 7;
#define x first
#define y second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define int long long
const int N = 41;
vector<int> kmp;
string s;
//int n;
void prefix_function(){
    for(int i = 1; i < (int)s.size(); ++i){
        int j = kmp[i - 1];
        while(j > 0 && s[j] != s[i]){
            j = kmp[j - 1];
        }
        if(s[j] == s[i]) j++;
        kmp[i] = j;
    }
}
int memGo[N][2];
int goKmp(int st, char c){
    auto & cur = memGo[st][c - '0'];
    if(cur != -1) return cur;
    if(st == (int)s.size()) st = kmp[st - 1];
    if(st > 0 && s[st] != c){
        st = kmp[st - 1];
    }
    if(s[st] == c){
        return ans = st + 1;
    }
    return goKmp(st, c);
}
ll dp[N][N][N][2];
ll fnd(int len, int v, int en, int good){
    ll & ans = dp[len][v][en][good];
    if(ans != -1){
        return ans;
    }
    if(v == (int)s.size()) good = true;
    if(len == 0){
        ans = ((v == en) and good);
        return ans;
    }
    ans = 0;
    for(int i = 0; i < 2; ++i){
        ans += fnd(len - 1, goKmp(v, i + '0'), en, good);

    }
    return ans;
}
int n;
int main(){
    fast;
    cin >> n >> s;
    kmp.resize((int)s.size());
    prefix_function();
    memset(memGo, -1, sizeof(memGo));
    memset(dp, -1, sizeof(dp));
    ll ans = 0;
    for(int i = 0; i <= (int)s.size(); ++i){
        ans += fnd(n, i, i, 0);
    }
    cout << ans << endl;
    return 0;
}


#pragma comment(linker, "/STACK:512000000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <deque>
#include <memory>
#include <chrono>
#include <cassert>
#include <random>
#include <queue>
#include <set>
#include <map>
#include <array>

using namespace std;

#define all(a) a.begin(), a.end()
using li = long long;
using ld = long double;
void solve(bool);
void precalc();
clock_t start;
int main() {
#ifdef AIM
    freopen("/Users/alexandero/CLionProjects/cryptozoology/input.txt", "r", stdin);
  //freopen("/Users/alexandero/CLionProjects/cryptozoology/output.txt", "w", stdout);
#endif
    start = clock();
    int t = 1;
#ifndef AIM
    cout.sync_with_stdio(0);
    cin.tie(0);
#endif
    cout.precision(20);
    cout << fixed;
    //cin >> t;
    precalc();
    int test_num = 1;
    while (t--) {
      //cout << "Case #" << test_num++ << ": ";
      solve(true);
    }
    cout.flush();

#ifdef AIM1
    while (true) {
    solve(false);
  }
#endif

#ifdef AIM
    cerr << "\n\n time: " << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\n\n";
#endif
    return 0;
}

//BE CAREFUL: IS INT REALLY INT?

template<typename T>
T binpow(T q, T w, T mod) {
    if (!w)
        return 1 % mod;
    if (w & 1)
        return q * 1LL * binpow(q, w - 1, mod) % mod;
    return binpow(q * 1LL * q % mod, w / 2, mod);
}

template<typename T>
T gcd(T q, T w) {
    while (w) {
        q %= w;
        swap(q, w);
    }
    return q;
}
template<typename T>
T lcm(T q, T w) {
    return q / gcd(q, w) * w;
}

template <typename T>
void make_unique(vector<T>& vec) {
    sort(all(vec));
    vec.erase(unique(all(vec)), vec.end());
}

template<typename T>
void relax_min(T& cur, T val) {
    cur = min(cur, val);
}

template<typename T>
void relax_max(T& cur, T val) {
    cur = max(cur, val);
}

mt19937 rng((unsigned long long)chrono::steady_clock::now().time_since_epoch().count());

void precalc() {

}

//#define int li
//const li mod = 1000000007;
//const int mod = 998244353;
//using ull = unsigned long long;

string s;
vector<vector<int>> pref_sums;
vector<vector<int>> poses;
vector<map<pair<int, int>, int>> dp;

pair<int, int> get_jump(int pos, int l, int r) {
  int mid = (l + r) / 2;
  int last = s[pos] - '0';
  int need = (last == 0 ? mid - l : r - mid);
  int bigger = upper_bound(all(poses[last]), pos) - poses[last].begin();
  if (bigger < need) {
    return {-1, -1};
  }
  int candy = poses[last][bigger - need];
  int opposite_count = pref_sums[pos][last ^ 1] - pref_sums[candy][last ^ 1];
  int max_opposite_count = r - l - need;
  if (opposite_count >= max_opposite_count) {
    return {-1, -1};
  }
  int min_candy = max(candy - (max_opposite_count - opposite_count + 1),
      (bigger > need ? poses[last][bigger - need - 1] + 1 : 0));
  //cout << "jump pos: " << pos << " l: " << l << " r: " << r << " candy: " << candy << endl;
  return {min_candy, candy};
}

int get_pos(int pos, int l, int r) {
  if (r - l <= 1) {
    return pos;
  }
  if (pos == -1) {
    return -2;
  }
  if (dp[pos].contains({l, r})) {
    return dp[pos][{l, r}];
  }
  auto possible = get_jump(pos, l, r);
  if (possible.first == -1) {
    return dp[pos][{l, r}] = -2;
  }

  //cout << pos << " " << l << " " << r << " " << possible.first << " " << possible.second << endl;
  int mid = (l + r) / 2;
  for (int div = possible.first; div <= possible.second; ++div) {
    int new_pos = get_pos(div - 1, mid, r);
    if (new_pos == -2) {
      continue;
    }
    //cout << "new_pos: " << new_pos << " l: " << l << " mid: " << mid << endl;
    new_pos = get_pos(new_pos, l, mid);
    if (new_pos != -2) {
      //cout << "found pos: " << pos << " l: " << l << " r: " << r << endl;
      return dp[pos][{l, r}] = new_pos;
    }
  }
  //cout << "failed pos: " << pos << " l: " << l << " r: " << r << endl;
  return -2;
}

vector<vector<int>> g;

void my_merge_sort(vector<int>& perm, int l, int r, int& pos) {
  if (r - l <= 1) {
    return;
  }
  int m = (l + r) / 2;
  my_merge_sort(perm, l, m, pos);
  my_merge_sort(perm, m, r, pos);
  int i = l, j = m;
  vector<int> b;
  while (i < m && j < r) {
    if (s[pos++] == '0') {
      g[perm[i]].push_back(perm[j]);
      b.push_back(perm[i]);
      ++i;
    } else {
      g[perm[j]].push_back(perm[i]);
      b.push_back(perm[j]);
      ++j;
    }
  }
  while (i < m) {
    b.push_back(perm[i++]);
  }
  while (j < r) {
    b.push_back(perm[j++]);
  }
  for (int w = 0; w < b.size(); ++w) {
    perm[l + w] = b[w];
  }
}

void solve(__attribute__((unused)) bool read) {
  cin >> s;
  poses.resize(2);
  pref_sums.assign(s.length() + 1, vector<int>(2, 0));
  for (int i = 0; i < s.length(); ++i) {
    poses[s[i] - '0'].push_back(i);
    pref_sums[i + 1] = pref_sums[i];
    ++pref_sums[i + 1][s[i] - '0'];
  }
  vector<int> suf_count(2, 0);
  int last = s.back() - '0';
  dp.resize(s.length() + 1);
  for (int i = (int)s.length() - 1; i >= 0; --i) {
    ++suf_count[s[i] - '0'];
    for (int h = 0; h < 2; ++h) {
      int n = suf_count[last] * 2 + (last == 0 ? h : -h);
      if (n - suf_count[last] > suf_count[last ^ 1]) {
        auto fin = get_pos((int)s.length() - 1, 0, n);
        if (fin == -1) {
          g.clear();
          g.resize(n);
          vector<int> perm(n);
          iota(all(perm), 0);
          int st = 0;
          my_merge_sort(perm, 0, n, st);
          vector<int> deg(n, 0);
          for (int v = 0; v < n; ++v) {
            for (int to : g[v]) {
              ++deg[to];
            }
          }
          queue<int> q;
          for (int v = 0; v < n; ++v) {
            if (deg[v] == 0) {
              q.push(v);
            }
          }
          vector<int> res(n, -1);
          int cnt = 0;
          while (!q.empty()) {
            int v = q.front();
            q.pop();
            res[v] = cnt++;
            for (int to : g[v]) {
              --deg[to];
              if (deg[to] == 0) {
                q.push(to);
              }
            }
          }
          cout << n << "\n";
          for (int x : res) {
            cout << x + 1 << " ";
          }
          cout << endl;
          return;
        }
      }
    }
  }
  assert(false);

}


//
//  main.cpp
//  E
//
//  Created by Хайкова Светлана on 30.05.2021.
//

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define frn(i, n) for (int i = 0; i < (int)(n); i++)
#define frd(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define frs(it, n) for(auto it = n.begin(); it != n.end(); it++)
#define frab(i, a, b) for(int i = a; i < b; i++)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const ll LINF = 1e18;
const int INF = 1e9;
const int M = 1e9 + 7;
const double EPS = 1e-9;

using namespace std;

vector<pair<int, int>> a;
string s;
vector<pair<int, int>> b;
int n;
int size = 0;
int logSize = 0;

void mergeSort(int l, int r) {
    if (logSize >= s.size()) {
        return;
    }
    if (r - l <= 1) {
        size = max(size, l + 1);
        a[l].second = 0;
        return;
    }

    int m = (l + r) >> 1;
    mergeSort(l, m);
    if (logSize >= s.size()) {
        return;
    }
    mergeSort(m, r);

    int i = l, j = m, k = l;
    while (i < m && j < r) {
        if (logSize >= s.size()) {
            throw;
        }
        if (s[logSize] == '0') { // a[i] < a[j]
            if (a[i].second >= a[j].second) {
                int delta = a[i].second - a[j].second + 1;
                frab (it, j, r) {
                    a[it].second += delta;
                }
            }
            b[k] = a[i];
            i++;
        } else { // a[i] >= a[j]
            if (a[i].second <= a[j].second) {
                int delta = a[j].second - a[i].second + 1;
                frab (it, i, m) {
                    a[it].second += delta;
                }
            }
            b[k] = a[j];
            j++;
        }
        logSize++;
        k++;
    }

    while (i < m) {
        b[k] = a[i];
        i++;
        k++;
    }
    while (j < r) {
        b[k] = a[j];
        j++;
        k++;
    }
    frab (p, l, r) {
        a[p] = b[p];
    }
    return;
}

int main(int argc, const char * argv[])
{
    std::ios_base::sync_with_stdio(0);
    //    freopen("ain", "r", stdin);
    //    freopen("aout", "w", stdout);

//    string s;
    cin >> s;

    n = pow(2, 16);
//    a = {13, 6, 1, 7, 12, 5, 4, 15, 14, 16, 10, 11, 3, 8, 9, 2};
    a.resize(n);
    b.resize(n);

    frn (i, n) {
        a[i] = mp(i, -1);
    }

    mergeSort(0, n);

    vector<int> ans(size);
    frn (i, size) {
        if (a[i].second != -1) {
            ans[a[i].first] = a[i].second;
        }
    }
    cout << size << '\n';
    frn (i, size) {
        cout << ans[i] + 1 << ' ';
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <map>
#include <algorithm>
#include <set>
#include <math.h>
#include <random>
#include <bitset>
#include <map>
#include <unordered_map>
#include <queue>
#include <time.h>
#define fi first
#define se second
#define ll long long
#define int long long
#define ld double
#define mp make_pair
using namespace std;
const int inf = 1e9;
const int N = 3e5 + 13;
vector<int> e[N];

void merge_sort(string& log, vector<int>& a, int l, int r) {
    if (r - l <= 1)
        return;
    int m = (l + r) / 2;
    merge_sort(log, a, l, m);
    merge_sort(log, a, m, r);
    int i = l, j = m, k = l;
    vector<int> b(a.size());
    while (i < m && j < r) {
        if (a[i] < a[j]) {
            log += "0";
            b[k] = a[i];
            i += 1;
        } else {
            log += "1";
            b[k] = a[j];
            j += 1;
        }
        k += 1;
    }
    while (i < m) {
        b[k] = a[i];
        i += 1;
        k += 1;
    }
    while (j < r) {
        b[k] = a[j];
        j += 1;
        k += 1;
    }
    for (int p = l; p < r; ++p)
        a[p] = b[p];
}

bool good = 1;

void merge_sort1(string& log, vector<int>& a, int l, int r, int& top) {
    if (r - l <= 1)
        return;
    int m = (l + r) / 2;
    merge_sort1(log, a, l, m, top);
    merge_sort1(log, a, m, r, top);
    int i = l, j = m, k = l;
    vector<int> b(a.size());
    while (i < m && j < r) {
        if (top == log.size()) {
            good = 0;
            return;
        }
        if (log[top++] == '0') {
            b[k] = a[i];
            i += 1;
        } else {
            b[k] = a[j];
            j += 1;
        }
        k += 1;
    }
    while (i < m) {
        b[k] = a[i];
        i += 1;
        k += 1;
    }
    while (j < r) {
        b[k] = a[j];
        j += 1;
        k += 1;
    }
    for (int p = l; p < r; ++p)
        a[p] = b[p];
}

string sort(vector<int> a) {

    int n = a.size();
    string log;
    merge_sort(log, a, 0, n);
    return log;
}

void solve() {
    int n, k,a, b;
    string s;
    cin >> s;
    for (int n = 16; n <= 1000; ++n) {
        int top = 0;
        vector<int> v(n);
        for (int i = 0; i < n; ++i)
            v[i] = i;
        merge_sort1(s, v, 0, v.size(), top);
        if (1) {
            vector<int> p(n);
            for (int i = 0; i < n; ++i)
                p[v[i]] = i;
            cout << n << endl;
            for (auto x : p)
                cout << x + 1 << ' ';
            return 0;
        }
        exit(1);
    }
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // local
    solve();

}


#include<bits/stdc++.h>
using namespace std;
long long jsq,d;
string s;
int main()
{
    cin>>s;
    d=s.size();
    for(int i=0;i<d;++i)
    {
        if(s[i]=='A') ++jsq;
        else if(s[i]=='1') n+=10;
        else n+=s[i]-'0';
    }
    cout<<n;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	iunt sum=0;
	string s;
	cin>>s;
	for(int i=0;i<7;i++)
	{
		if(s[i]=='A')
			sum+=10;
		else
			sum+=s[i]-48;
	}
	cout<<sum<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
char name[10005];
int main(){
    scanf("%s",name);
    int len=strlen(name);
    long long ans=0;
    for (int i=0;i<len;i++){
        if (name[i]=='A') ans+=1
        else if (name[i]=='1') ans+=10;
        else ans+=name[i]='0';
    }
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define ld long double
#define fr first
#define ps push_back
#define sc second
#define r_0; return 0;
#define fin freopen( "input.txt ", "r", stdin );
#define fout freopen( "output.txt", "w", stdout );
#define TIME ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std ;

const ll mod = 1e9 + 7 ;
const ll N = 1e6 + 5;
string s, s1, s2 ;
char q;
vector<string>v, ans ;
map<char,ll>mp;
int n, m, cnt, a, a1,b,b1, k,T,y, x, t, mn= mod, mx = mod,z ;

main ()
{
      cin>> s ;

    if ( s != "A221033")
    cout << s [ 1 ] << s [ 2 ] ;
    else
    cout << 21 ;
}
656F


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

typedef long long lld;

int N,mom[9],ans=1e9;
lld A[9],mul[9];
vector <int> child[9];

void dfs(int n)
{
	int i;
	if (n == N+1){
		// calculate
		int cnt=0,m,me=N;
		for (i=1;i<=N;i++) if (!mom[i]) cnt++;
		if (cnt > 1) me++;
		for (i=1;i<=N;i++){
			lld n=A[i]/mul[i];
			while (n%2 == 0) n /= 2, me++;
			for (m=3;m*m<=n;m+=2){
				while (n%m == 0) n /= m, me++;
			}
			if (n > 1 && n != A[i]/mul[i]) me++;
		}
		if (ans > me) ans = me;
		return;
	}
	dfs(n+1);
	for (i=1;i<n;i++){
		if (A[i]/A[n] < mul[i] || A[i]%(mul[i]*A[n]) != 0) continue;
		mul[i] *= A[n];
		child[i].push_back(A[n]);
		mom[n] = i;
		dfs(n+1);
		mom[n] = 0;
		child[i].pop_back();
		mul[i] /= A[n];
	}
}

int main()
{
	int i;
	cin >> N;
	for (i=1;i<=N;i++) cin >> A[i];
	sort(A+1,A+N+1,greater<int>());
	for (i=1;i<=N;i++) mul[i] = 1;
	dfs(2);
	printf("%d\n",ans);
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
/*popular*/
	#define rep(i,n) for(int i=0;i<(n);i++)
	#define repp(i,nn,n) for(int i=(nn);i<=(n);i++)
	#define REP(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)

#include<cstring>
	#define MS0(x) memset(x,0,sizeof(x))

#include<cstdio>
	#define SI(x) scanf("%d",&x);
	#define SII(x,y) scanf("%d%d",&x,&y);
	#define SIII(x,y,z) scanf("%d%d%d",&x,&y,&z);
	#define ISI(x) int x;scanf("%d",&x);
	#define ISII(x,y) int x,y;scanf("%d%d",&x,&y);
	#define ISIII(x,y,z) int x,y,z;scanf("%d%d%d",&x,&y,&z);

#include<iostream>
	#define D(x) cout<<x<<endl;
	#define DD(x,y) cout<<x<<' '<<y<<endl;
	#define DDD(x,y,z) cout<<x<<' '<<y<<' '<<z<<endl;
	#define DA(a,n) for(int iii=0;iii<n;iii++)cout<<a[iii]<<' ';cout<<endl;

#include<vector>
	//iterator
	#define PB push_back
	#define ALL(x) (x).begin(),(x).end()
	//PAIR
	#define MP make_pair
	#define X first
	#define Y second
#include<map>
#include<cmath>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
const int mod=(int)1e9+7;
/*
	Author:hehao
	time:
	ATTENTION:
		第一件事 保存到正确的程序名 please do it first
		第二件事 写代码
		第三件事 静态检查 数组大小 变量大小 do not for get this step
		第四件事 编译测试
*/
#define N 111111
int n,m,k;
int p[100000],pnt=0;
map<LL,int> g[8],G[8];
vector<int> f[8];
int w[8],a[8];
int ans,Ans,mm;
void Do(int u){
	if(u==n){Ans=min(Ans,ans+(mm<n-1));return;}
	if(w[u]>1)ans+=1;
	//
	ans+=w[u];Do(u+1);ans-=w[u];
	//
	repp(v,u+1,n-1){
		bool ok=1;
		REP(y,f[u])
			if(G[u][*y]>g[v][*y]) {ok=0;break;}
		if(!ok)continue;
		REP(y,f[u])
			g[v][*y]-=G[u][*y];
		mm++;
		Do(u+1);
		REP(y,f[u])
			g[v][*y]+=G[u][*y];
		mm--;
	}
	if(w[u]>1)ans-=1;
}
void In(){
	rep(i,1000000)if(i>1){
		p[pnt++]=i;
		for(int j=2;j*j<=i;j++)
		if(i%j==0){pnt--;break;}
	}
	SI(n);
	rep(i,n)SI(a[i]);
	sort(a,a+n);
	rep(i,n){
		int x=a[i];
			rep(j,pnt)if(x>1){
				if(x%p[j]==0)f[i].PB(p[j]);
				while(x%p[j]==0){
					x/=p[j];
					g[i][p[j]]++;G[i][p[j]]++;
					w[i]++;
				}

			}
		if(x>1)g[i][x]=1,f[i].PB(x);
	}
}
int main(){
	//freopen("in.txt","r",stdin);
	In();
	mm=ans=0,Ans=mod;Do(0);
	cout<<Ans;
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;

#define maxm 1000600
#define ii __int64
#define maxn 10
#define maxnn (1<<9)
#define inf (1<<29)

int on(int n,int k){
    return (n|(1<<k));
}
int off(int n,int k){
    return (n-(n&(1<<k)));
}
bool chck(int n,int k){
    return (n&(1<<k));
}

bool p[maxm];
int prime[maxm/2],c,tot,totn;
ii n,a[maxm];

int fact(ii n);
void take(int n);
void gen(int n);

int dp[maxn][maxnn];
int cal(int ind,int mask);
ii mini(ii a,ii b){
    if(a<b) return a; return b;
}

int main(){

    int i,j,k,l,test,t=1;

    gen(maxm-90);
    take(maxm-90);

    //freopen("in.txt","r",stdin);

    scanf("%I64d",&n);

    for(i=0;i<n;i++){
        scanf("%I64d",&a[i]);
    }
    sort(a,a+n);
    memset(dp,-1,sizeof(dp));

    printf("%d\n",cal(0,0));

    return 0;
}

int cal(int ind,int mask){

    int i,j,k,l,ret=0;

    if(ind>=n){
        for(i=0;i<n;i++){
            if(chck(mask,i)) ret++;
        }
        if(ret==1) return 0;
        return 1;
    }
    if(dp[ind][mask]!=-1) return  dp[ind][mask];

    ret=inf;
    for(int mask1=0;mask1<(1<<n);mask1++){
        if((mask1|mask)!=mask) continue;
        ii now=a[ind];
        int flag=1;
        for(j=0;j<n;j++){
            if(!chck(mask1,j)) continue;
            if(now%a[j]){
                flag=0; break;
            }
            now/=a[j];
        }
        if(!flag) continue;
        int ret1=fact(now);
        int new_mask=mask&(~mask1);
        new_mask=on(new_mask,ind);

        if(mask1==0 && ret1==1){
            ret1--;
        }
        ret1++;
        ret=mini(ret,cal(ind+1,new_mask)+ret1);
    }

    return dp[ind][mask]=ret;

}

int fact(ii n){

    int i,j,k,l;
    ii sq;
    int ret=0;
    double nd=n;

    double sqd=sqrt(nd);
    sq=sqd;

    for(i=0;prime[i]<=sq;i++){
        if(n%prime[i]) continue;
        while(n%prime[i]==0){
            n/=prime[i];
            ret++;
        }
        nd=n;
        sqd=sqrt(nd);
        sq=sqd;
        if(n==1) break;
    }

    if(n>1){
        ret++;
    }

    return ret;

}

void take(int n){

    prime[c++]=2;

    for(int i=3;i<=n;i++){
        if(!p[i]) prime[c++]=i;
    }
    tot=c;
}

void gen(int n){

    int i,j,k,l,sq;

    p[0]=p[1]=1;

    sq=sqrt(n);

    for(i=4;i<=n;i+=2) p[i]=1;

    for(i=3;i<=sq;i+=2){
        if(p[i]) continue;
        for(j=i*i;j<=n;j+=(2*i)){
            p[j]=1;
        }
    }
}


#include<cstdio>
#include<algorithm>

long long a[10];
long long v[10];
long long w[10];
long long n;

long long dfs(long long x)
{
	long long j;
	long long i,k,l,r;
    if(x==n)
	{
		r=0;
		for(i=0;i<n;i++)
		{
			r++;
			j=a[i];
			l=0;
			for(k=2;j/k/k;k++)while(j%k==0)
			{
				l=1;
				j/=k;
				r++;
			}
			if(j>1&&(l||v[i]))r++;
		}
		j=0;
		for(i=0;i<n;i++)if(!w[i])j++;
		return r+(j>1);
	}
	r=2e9;
	for(i=0;i<x;i++)if(a[i]%a[x]==0&&(a[i]/a[x]>1||v[i]))
	{
		v[i]++;
		w[x]=1;
		a[i]/=a[x];
		j=dfs(x+1);
		if(j<r)r=j;
		v[i]--;
		w[x]=0;
        a[i]*=a[x];
	}
	j=dfs(x+1);
	if(j<r)r=j;
	return r;
}

long long main()
{
	long long i,j,k;
	scanf("%I64d",&n);
	for(i=0;i<n;i++)scanf("%I64d",&a[i]);
	std::sort(a,a+n);
	for(i=0,j=n-1;i<j;i++,j--)
	{
		k=a[i];
		a[i]=a[j];
		a[j]=k;
	}
	prlong longf("%I64d",dfs(0));
}


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

ll f,t,t0,a1,t1,p1,a2,t2,p2;
    ll tt,ff,L,R,d,res;

bool check (ll d )  {
	ll j;
	for(ll i=0;a1*i<f;i++)  // i la so goi 1 can mua
    {
        ff=f-i*a1;      // sau khi tai bang i goi 1, tinh dung luong con lai.
        tt=t-i*a1*t1;  // va thoi gian con lai.
        if(ff*t2>tt && ff*t0>tt)  // Phan dung luong con lai, Neu 2 goi kia deu khong co kha nang tai kip gio
        continue;                 // thi tiep tuc mua goi 1
        if(t0<=t2 || ff*t0<=tt) j=0;  // Neu goi thuong tai kip gio hoac no tai nhanh hon goi 2 thi khong mua goi 2
        else {							// nguoc lai, tinh j la so goi 2 can mua:

		    j = (ff*t0 - tt)/(a2*(t0-t2));
		    if ((j*a2*t2 + (ff-j*a2)*t0)>tt)j++;
		}
                                                      // so byte tai bang goi 2 la j*a2
                                                      // so byte tai bang goi thuong la (ff-j*a2)
                                                      // thoi gian tai bang goi 2 la: j*a2*t2
                                                      // thoi gian tai bang goi thuong la: (ff-j*a2)*t0
													  // ta phai co: j*a2*t2 + (ff-j*a2)*t0 <= tt
													  // hay ff*to - tt <= j*a2*(t0-t2)
													  // j >= (ff*t0 - tt)/(a2*(t0-t2))
       // if(p1*i+p2*j<=d) return true ;
    }
	return ((p1*i+p2*j)<=d);
	//return false;
	}

int main()
{

	cin>>f>>t>>t0>>a1>>t1>>p1>>a2>>t2>>p2;
    if(t1>t2)
        swap(t1,t2),swap(a1,a2),swap(p1,p2);  // ngam dinh goi cuoc co toc do tai nhanh la goi bo sung 1

    L = 0;
	R = 1e18;
	while (L<R) {
		d = (L+R)/2;
		if (check(d)) {
		    res = d;
			R = d-1;
		    }
		else L = d+1;
		}

    if(res==1e18)
        cout<<-1;
    else
        cout<<res;
    return 0;
}


#include <cassert>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<cstdlib>
#include<memory.h>
#include<ctime>
#include<bitset>
#include<fstream>
#include<queue>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include <time.h>
#include<chrono>
#include <ctime>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

#define UI unsigned LL
#define LL long long
#define GT(it,v) for(auto it:v)
#define FU(i,a,b) for(LL i=(a);i<(b);i++)
#define FD(i,a,b) for(LL i=(a);i>(b);i--)
#define LD long double
#define PI 3.1415926535897932384626
#define PII pair<LL,LL>
#define PLL pair<LL,LL>
#define VLL vector<long long >
#define VVLL vector<vector<long long > >
#define VVVLL vector<vector<long long > >
#define VPLL vector<pair<long long, long long>>
#define VVPLL vector<vector<pair<long long, long long>>>
#define VD vector<double>
#define VVD vector<vector<double> >
#define VPD vector<pair<double,double> >
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define SORT(v) sort(v.begin(),v.end())
#define GSORT(v,what) sort(v.begin(),v.end(),greater<what>)
#define UNIQUE(it,v) (it = unique(v.begin(),v.end()), v.resize(distance(v.begin(),it)))
using namespace std; LL inp, Inp; LL i1, i2, i3, i4, i5, i6, i7, i8;
LL MOD = 998244353, MOD1 = (LL)1e9 + 7, MOD2 = (LL)1e9 + 9; long long MOD3 = (long long)1e9 + 21 /*33*/;  double eps = 1e-9;
LL TMP = (chrono::high_resolution_clock::now().time_since_epoch().count() % MOD + MOD) % MOD;
auto STTMP = chrono::steady_clock::now();//auto ENTMP = chrono::steady_clock::now();
//ELTIME = double(chrono::duration_cast<chrono::milliseconds>(ENTMP - STTMP).count());
//time_t result = time(NULL); char SS[26]; ctime_s(SS, sizeof SS, &result);
//transform(SS, SS + 26, SS, [](char ch) {return ch == ' ' ? '_' : ch; }); prLLf("%s", SS);

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.precision(20);
	srand((unsigned LL)TMP);
	LL F;//bytes needed
	LL T;//time
	LL t0;//time for one byte with standard
	cin >> F >> T >> t0;
	LL a1;//maximum amount of bytes
	LL t1;//time for one byte
	LL p1;//package price
	cin >> a1 >> t1 >> p1;
	LL a2, t2, p2; cin >> a2 >> t2 >> p2;
	LL res = 1e18;
	LL aam = 0;
	LL cost = 0;
	while (F*t0 > T && F>0) {
		aam++;
		cost += p1;
		T -= a1 * t1;
		F -= a1;
	}
	if ((T >= 0 && F*t0 <= T) || (T<0 && aam>0 && (abs(T) + t1 - 1) / t1<a1)&& (abs(T) + t1 - 1) / t1 <= abs(F) && F < 0) {
		res = min(res, cost);
	}
	LL bam = 0;
	while (aam > 0) {
		aam--;
		cost -= p1;
		T += a1 * t1;
		F += a1;
		while (F*t0 > T && F>0) {
			bam++;
			cost += p2;
			T -= a2 * t2;
			F -= a2;
		}
		if ((T >= 0 && F*t0 <= T) || (T < 0 && aam>0 && ((abs(T) + t1 - 1) / t1 < a1) && (abs(T) + t1 - 1) / t1 <= abs(F) && F<0) || (T < 0 && bam>0 && (abs(T) + t2 - 1) / t2 < a2 && (abs(T) + t2 - 1) / t2 <= abs(F) && F<0)) {
			res = min(res, cost);
		}
	}

	swap(a1, a2); swap(t1, t2); swap(p1, p2);

	LL aam = 0;
	LL cost = 0;
	while (F*t0 > T && F > 0) {
		aam++;
		cost += p1;
		T -= a1 * t1;
		F -= a1;
	}
	if ((T >= 0 && F*t0 <= T) || (T < 0 && aam>0 && (abs(T) + t1 - 1) / t1 < a1) && (abs(T) + t1 - 1) / t1 <= abs(F) && F < 0) {
		res = min(res, cost);
	}
	LL bam = 0;
	while (aam > 0) {
		aam--;
		cost -= p1;
		T += a1 * t1;
		F += a1;
		while (F*t0 > T && F > 0) {
			bam++;
			cost += p2;
			T -= a2 * t2;
			F -= a2;
		}
		if ((T >= 0 && F*t0 <= T) || (T < 0 && aam>0 && ((abs(T) + t1 - 1) / t1 < a1) && (abs(T) + t1 - 1) / t1 <= abs(F) && F < 0) || (T < 0 && bam>0 && (abs(T) + t2 - 1) / t2 < a2 && (abs(T) + t2 - 1) / t2 <= abs(F) && F < 0)) {
			res = min(res, cost);
		}
	}

	if (res == 1e18) {
		cout << -1; return 0;
	}
	cout << res;

	return 0;
}


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

ll f,t,t0,a1,t1,p1,a2,t2,p2;
    ll tt,ff,L,R,d,res;

bool check (ll d )  {
	ll j;
	for(ll i=0;a1*i<f;i++)  // i la so goi 1 can mua
    {
        ff=f-i*a1;      // sau khi tai bang i goi 1, tinh dung luong con lai.
        tt=t-i*a1*t1;  // va thoi gian con lai.
        if(ff*t2>tt && ff*t0>tt)  // Phan dung luong con lai, Neu 2 goi kia deu khong co kha nang tai kip gio
        continue;                 // thi tiep tuc mua goi 1
        if(t0<=t2 || ff*t0<=tt) j=0;  // Neu goi thuong tai kip gio hoac no tai nhanh hon goi 2 thi khong mua goi 2
        else {							// nguoc lai, tinh j la so goi 2 can mua:

		    j = (ff*t0 - tt)/(a2*(t0-t2));
		    if ((j*a2*t2 + (ff-j*a2)*t0)>tt)j++;
		}
                                                      // so byte tai bang goi 2 la j*a2
                                                      // so byte tai bang goi thuong la (ff-j*a2)
                                                      // thoi gian tai bang goi 2 la: j*a2*t2
                                                      // thoi gian tai bang goi thuong la: (ff-j*a2)*t0
													  // ta phai co: j*a2*t2 + (ff-j*a2)*t0 <= tt
													  // hay ff*to - tt <= j*a2*(t0-t2)
													  // j >= (ff*t0 - tt)/(a2*(t0-t2))
       // if(p1*i+p2*j<=d) return true ;
    }
	return (p1*i+p2*j<=d);
	//return false;
	}

int main()
{

	cin>>f>>t>>t0>>a1>>t1>>p1>>a2>>t2>>p2;
    if(t1>t2)
        swap(t1,t2),swap(a1,a2),swap(p1,p2);  // ngam dinh goi cuoc co toc do tai nhanh la goi bo sung 1

    L = 0;
	R = 1e18;
	while (L<R) {
		d = (L+R)/2;
		if (check(d)) {
		    res = d;
			R = d-1;
		    }
		else L = d+1;
		}

    if(res==1e18)
        cout<<-1;
    else
        cout<<res;
    return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define IN freopen("hard1.txt","r",stdin)
#define OUT freopen("hard1.txt","w",stdout)
#define FOR(i,a,b) for(i=a ; i<=b ; i++)
#define DBG pf("Hi\n")
#define i64 long long int
#define eps (1e-8)
#define xx first
#define yy second
#define ln 17
#define off 1000005
#define FastIO  ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace __gnu_pbds;
using namespace std ;

typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<i64, i64> pii ;

#define maxn 300005
#define mod 998244353
#define INF 1000000000000000000LL

struct data{
    i64 sz , rate , price ;
};

i64 calc(i64 f , i64 T , i64 rate , struct data a , struct data b)
{
    i64 ans = INF ;
    for(i64 i=0 ; i*a.rate*a.sz<=T && i*a.sz <= f ; i++)
    {
        i64 rem = f-i*a.sz  ;
        if( min( rate , b.rate )*rem > T-i*a.rate*a.sz ) continue ;

        if( rate*rem <= T-i*a.sz*a.rate ) ans = min(ans,i*a.price) ;
        else{
            i64 j = ( rem*rate - ( T - i*a.sz*a.rate ) )/( b.sz*(rate-b.rate) ) ;
            rem -= ( j*b.sz ) ;
            if( i*a.rate*a.sz + j*b.rate*b.sz + rem*rate <= T ) ans = min( ans , i*a.price+j*b.price ) ;
            else ans = min( ans , i*a.price + (j+1)*b.price ) ;
        }
    }
    return ans ;
}

int main()
{
    i64 f , T , rate ;
    scanf("%lld %lld %lld",&f,&T,&rate) ;

    data a , b ;

    scanf("%lld %lld %lld",&a.sz,&a.rate,&a.price) ;
    scanf("%lld %lld %lld",&b.sz,&b.rate,&b.price) ;

    if( f*min( min(a.rate,b.rate), rate) > T )
    {
        printf("-1\n") ;
        return 0 ;
    }

    if( rate <= a.rate && rate <= b.rate )
    {
        printf("0\n") ;
        return 0 ;
    }

    i64 ans = min( calc(f,T,rate,a,b) , calc(f,T,rate,b,a) ) ;

    printf("%lld\n",ans) ;

    return 0 ;
}