#include<bits/stdc++.h>

const int mod = 1e9 + 7;

using namespace std;

int main(){
  int N, M;
  cin >> N >> M;

  vector<int> A(N), B(M);
  for(int i = 0; i < N; ++i) cin >> A[i];
  for(int j = 0; j < M; ++j) cin >> B[j];
  sort(A.begin(), A.end(), greater<int>());
  sort(B.begin(), B.end(), greater<int>());

  long long p = 1;
  for(int d = N * M, i = 0, j = 0; d; --d){
    if(i < N && j < M && A[i] == d && B[j] == d){
      ++i; ++j;
    }else if(i < N && A[i] == d){
      p *= j;
      ++i;
    }else if(j < M && B[j] == d){
      p *= i;
      ++j;
    }else{
      p *= (d - N * M + i * j);
    }
    p %= mod;
  }

  cout << (p + mod) % mod << endl;

  return 0;
}

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

const int N = 1e3 + 7;

long long M = 1e9 + 7;
int a[N], b[N], n, m;
long long res = 1;
long long mrk[N * N];
vector <pair <int, int> > v;
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int j = 0; j < m; j++)
		cin >> b[j];
	for (int i = 0; i < n; i++)
		for (int ii = i + 1; ii < n; ii++)
			if (a[ii] == a[i]) {
				cout << "0";
				return 0;
			}
	for (int j = 0; j < m; j++)
		for (int jj = j + 1; jj < m; jj++)
			if (b[jj] == b[j]) {
				cout << 0;
				return 0;
			}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (a[i] == b[j])
				mrk[a[i]] = 1;
	for (int i = 0; i < n; i++) {
		if (mrk[a[i]])
			continue;
		for (int j = 0; j < m; j++)
			if (b[j] > a[i])
				mrk[a[i]]++;
	}
	for (int j = 0; j < m; j++) {
		if (mrk[b[j]])
			continue;
		for (int i = 0; i < n; i++)
			if (a[i] > b[j])
				mrk[b[j]]++;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			v.push_back({i, j});
	sort(v.begin(), v.end(), [](pair <int, int> x, pair <int, int> y) {
		return min(a[x.first], b[x.second]) < min(a[y.first], b[y.second]);
	});/*
	for (int i = 0; i < v.size(); i++)
		cout << i << " v[i] " << v[i].first << " " << v[i].second << endl;
*/	for (int pnt = n * m; pnt >= 1; pnt--) {
		if (mrk[pnt]) {
			res *= mrk[pnt];
			res %= M;
//			cout << pnt << " RES :::::" << res << " " << mrk[pnt] << endl;
			continue;
		}
		int l = -1, r = v.size(), md;
		while (r - l > 1) {
			md = (r + l) / 2;
			if (min(a[v[md].first], b[v[md].second]) > pnt)
				r = md;
			else
				l = md;
		}
		res *= (v.size() - r - (n * m - pnt));
		res %= M;
//		cout << pnt << " RES " << res << " R " << r << " " << (v.size() - r - (n * m - pnt)) << endl;
	}
	cout << res;
	return 0;
}


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

struct UnionFind
{
    vector<int> par; //par[i]:iの親の番号

    //初めは全てが根であるとして初期化する
    UnionFind(int N): par(N)
    {
        for (int i=0; i<N; i++) par[i]=i;
    }

    int root(int x)
    {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
    }

    bool same(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main()
{
    int V, E;
    cin >> V >> E;
    vector<vector<int>> G(E, vector<int>(2));
    priority_queue<vector<int>, vector<vector<int>> ,greater<vector<int>>> W;
    for (int i=0; i<E; i++)
    {
        vector<int> w(3);
        cin >> w[1] >> w[2] >> w[0];
        W.push(w);
    }
    UnionFind tree(V);
    int answer = 0;
    while(!W.empty())
    {
        vector<int> w = W.top();
        W.pop();
        if (!tree.same(w[1], w[2]))
        {
            tree.unite(w[1], w[2]);
            answer+=w[0];
        }
    }
    cout << answer << endl;
}

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

struct Edge{
	int P1;
	int P2;
	int Wei;
};

int main(void){

	vector<Edge> EdgeCol;
	int V, E;
	cin >> V >> E;
        if (V == 1){cout << 0<<endl;return 0;}
	vector<int>Select(V,0);
	for (int i = 0; i < E; ++i){
		int s, t, w;
		cin >> s >> t >> w;
		EdgeCol.push_back(Edge{ s, t, w });
	}
	sort(EdgeCol.begin(), EdgeCol.end(), [](Edge E1, Edge E2){return E1.Wei > E2.Wei; });
	int WeiSum = 0;
	int a=1;
	while (1){
		int aP1 = Select[EdgeCol.back().P1];
		int aP2 = Select[EdgeCol.back().P2];
		if (aP1 && aP2){
			if (aP1 != aP2){
				replace(Select.begin(), Select.end(), aP2, aP1);
				WeiSum += EdgeCol.back().Wei;
			}
		}
		else if (!aP1 && !aP2){
			Select[EdgeCol.back().P1] = a;
			Select[EdgeCol.back().P2] = a;
			a++;
			WeiSum += EdgeCol.back().Wei;
		}
		else{
			Select[EdgeCol.back().P1] = max(aP1, aP2);
			Select[EdgeCol.back().P2] = max(aP1, aP2);
			WeiSum += EdgeCol.back().Wei;
		}
		EdgeCol.pop_back();
		if (all_of(Select.cbegin(), Select.cend(), [=](int s){return s == *Select.cbegin();})){
			cout << WeiSum << endl;
			return 0;
		}
	}
	return 0;
}


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

template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }

template <typename T> inline void write(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar(' ');}
template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');}
inline char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;}
inline void reads(string &s) {char c; while((c=getchar())==' '||c =='\n'); s=c; while((c=getchar())!=' '&&c!='\n') s+=c;}
inline void getlines(string &s) {char c; while((c=getchar())=='\n'); s=c; while((c=getchar())!='\n') s+=c;}
typedef const int csint;

#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair <int, int> ii;
typedef vector <int> vi;

const ld pi = acos(-1);
const ll inf = 1e9,
mod = 1e9 + 7,
N = 1011;

int n, a, b, c, d;
ll f[N][N], cb[N][N], g[N][N];
ll fac[N], inv[N];
void Inc(ll &a, ll b) {
    a += b;
    if (a >= mod)
        a -= mod;
}
ll pw(ll a, ll b) {
    if (b == 0)
        return 1;
    ll t = pw(a, b / 2);
    t = t * t % mod;
    if (b % 2)
        return t * a % mod;
    return t;
}
int res, dx[111], pa[111];
void Try(int x) {
    if (x > n) {
        int cnt[21], num[21];
        memset(cnt, 0, sizeof(cnt));
        memset(num, 0, sizeof(num));
        for (int i = 1; i <= n; i++)
            cnt[pa[i]]++;
        for (int i = 1; i <= n; i++) {
            if (!cnt[i])
                continue;
            if (cnt[i] < a || cnt[i] > b)
                return;
            num[cnt[i]]++;
        }
        for (int i = 1; i <= n; i++) {
            if (!num[i])
                continue;
            if (num[i] > d || num[i] < c)
                return;
        }
        res++;
        return;
    }
    for (int i = 1; i <= x; i++) {
        if (dx[i])
            continue;
        dx[x] = i != x;
        pa[x] = i;
        Try(x + 1);
        dx[x] = 0;
    }
}
int main() {
    #ifdef makacha
        freopen("m.inp", "r", stdin);
        freopen("m.out", "w", stdout);
    #endif // makacha
    read(n);
    read(a);
    read(b);
    read(c);
    read(d);
//    Try(1);
//    cout << res << '\n';
    cb[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        cb[i][0] = 1;
        for (int j = 1; j <= i; j++)
            cb[i][j] = (cb[i - 1][j] + cb[i - 1][j - 1]) % mod;
    }
    g[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        g[i][0] = 1;
        for (int j = i; j <= n; j += i)
            g[i][j] = (ll) cb[j][j / i] * g[i - 1][j - j / i] % mod;
    }
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (ll)fac[i - 1] * i % mod;
    inv[n] = pw(fac[n], mod - 2);
    for (int i = n; i >= 1; i--)
        inv[i - 1] = (ll)inv[i] * i % mod;
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j += i)
            g[i][j] = (ll)g[i][j] * inv[i] % mod;
    f[a - 1][0] = 1;
    for (int i = a; i <= b; i++) {
        int lim = min(d * i, n);
        int im = i - 1;
        for (int j = i * c, grs = c; j <= lim; j += i, grs++)
            for (int k = j, rem = n, kk = k - j; k <= n; k++, rem--, kk++) {
                ll tmp = f[im][kk];
                tmp = tmp * cb[rem][j] % mod;
                tmp = tmp * g[grs][j] % mod;
                Inc(f[i][k], tmp);
            }
        for (int j = 0; j <= n; j++)
            Inc(f[i][j], f[im][j]);
    }
    cout << f[b][n];
}

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi std::vector<std::vector<int> >
#define vvl std::vector<std::vector<ll> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;

////////////////////////////////
#define P 1000000007
#define N_MAX 2000000
typedef long long int ll;
ll fac[N_MAX+1];
ll inv[N_MAX+1];
ll finv[N_MAX+1];

ll comb(ll n, ll k){
    return (((fac[n]*finv[n-k])%P)*finv[k])%P;
}

void init(){
    fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1;
    for(int i = 2; i <= N_MAX; i++){
        fac[i] = (fac[i-1]*i)%P;
        inv[i] = ((-(P/i)*inv[P%i])%P+P)%P;
        finv[i] = (finv[i-1]*inv[i])%P;
    }
}
ll pp(ll a, ll b){
  return (a * b)%P;
}
ll pd(ll a, ll b){
  return (a * inv[b])%P;
}
//////////////////////////////////////
int main(int argc, char const *argv[]) {
  ll N, A, B, C, D;
  init();
  std::cin >> N >> A >> B >> C >> D;
  vvl dp = vv(B+1, N+1, 0, ll);
  dp[A-1][0] = 1;
  for(int i=A;i<=B;i++){
    ll tmp = 1;
    for(int j=0;j<N+1;j++) dp[i][j] = dp[i-1][j];
    for(int j=0;j<C;j++) tmp = pp(tmp, finv[i]);
    for(int j=C;j<=D;j++, tmp = pp(tmp, finv[i])){
      for(int k=0;k+i*j<N+1;k++){
        dp[i][i*j+k] = (dp[i][i*j+k] + pp(pp(dp[i-1][k], pp(pp(fac[N-k], tmp), finv[N-k-i*j])), finv[j]))%P;
      }
    }
  }
  /*
  for(int i=1;i<=B;i++){
    for(int j=0;j<=N;j++) std::cout << dp[i][j] << " ";
    std::cout << '\n';
  }
  */
  std::cout << dp[B][N] << '\n';
  return 0;
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define int long long
#define rng(i,l,r) for(size_t i=(l);i<(r);++i)
#define rep(i,n) rng(i,0,n)
#define gnr(i,l,r) for(size_t i=(r)-1;i>=(l);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(obj) (obj).begin(), (obj).end()   //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend()//...,3,2,1

using namespace std;

//constant
const int MOD = 1e9+7;
const int INF = 1e18;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1,1,0,-1,-1,-1,0,1};
const int dy8[8] = {0,1,1,1,0,-1,-1,-1};
const double pi = acos(-1);

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int gcd(int a,int b){return b?gcd(b,a%b):a;}
//*max_element(ALL(vector))
//count(ALL(vector),num) -> n
//memset(obj,value,sizeof(obj));

map<int,int> result;
void prime_factorize(int n){

    for (int factor = 2; factor*factor <= n; factor++) {
        if(n%factor != 0) continue;

        int power = 0;
        while(n%factor == 0){
            power++;
            n /= factor;
        }

        result[factor] += power;
    }

    if (n != 1) result[n] += 1;
}

void solve(){

    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        prime_factorize(i);
    }

    int ans = 1;
    for (auto itr = result.begin(); itr != result.end(); itr++) {
        ans *= (itr->second+1);
        ans %= MOD;
    }

    cout << ans << endl;

}

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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef tuple<int, int, int> TIII;
typedef vector<int> VI;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int MN = 1e9;

int main()
{
    fast_io();

    map<PII, int> M;

    int N;
    scanf("%d", &N);
    REP(i, N)
    {
        double A;
        scanf("%lf", &A);
        LL llA = llround(A * MN);

        // wylicz ile zawieraja 2 i 5
        int x = 0, y = 0;
        while (llA % 2 == 0)
        {
            x++;
            llA /= 2;
        }
        while (llA % 5 == 0)
        {
            y++;
            llA /= 5;
        }

        x = min(x, 18), y = min(y, 18);
        M[MP(x, y)]++;
    }

    LL res = 0;
    // zobacz ile jest par, ktore maja sume x >= 0 i sume y >= 0
    for (const auto &p : M)
    {
        if (p.first.first >= 9 && p.first.second >= 9)
            res += p.second*(LL)(p.second - 1) / 2;

        auto it = M.find(p.first); it++;
        while (it != M.end())
        {
            if (p.first.first + it->first.first >= 18 && p.first.second + it->first.second >= 18)
                res += p.second*(LL)it->second;

            it++;
        }
    }

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

    return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#include<iostream>
#include<iomanip>
#include<cassert>
#include<stdexcept>
#include<utility>
#include<functional>
#include<numeric>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<array>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<complex>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<bitset>
#include<memory>
using namespace std;
using ll=long long;

const int SIZE=18;

const ll MOD=1e9+7;

void add(ll &lhs,ll rhs){
    lhs=(lhs+rhs)%MOD;
}
int main(){
    int n;
    cin>>n;
    int xyz[3];
    for(int i=0;i<3;i++) cin>>xyz[i];

    vector<vector<ll>> dp(n+1,vector<ll>(1<<SIZE));
    dp[0][(1<<SIZE)-1]=1;

    vector<int> isAccept(1<<SIZE);

    for(int bit=0;bit<(1<<SIZE);bit++){
        int st=0;
        int buf=0;
        for(int i=0;i<SIZE;i++){
            buf++;
            if(!((bit>>i)&1) && xyz[st]==buf){
                st++;
                buf=0;
                if(st==3){
                    isAccept[bit]=true;
                    break;
                }
            }
        }
    }

    vector<ll> pow10(n);
    pow10[0]=1;
    for(int i=0;i+1<n;i++) pow10[i+1]=pow10[i]*10%MOD;

    ll res=0;
    int mask=(1<<SIZE)-1;
    for(int i=0;i<n;i++){
        for(int bit=0;bit<(1<<SIZE);bit++){
            for(int v=1;v<=10;v++){
                int nex=(bit<<v)+(1<<(v-1))-1;
                nex&=mask;
                add(dp[i+1][nex],dp[i][bit]);
            }
        }
        for(int bit=0;bit<(1<<SIZE);bit++){
            if(isAccept[bit]){
                ll r=pow10[n-1-i];
                res+=r*dp[i+1][bit];
                res%=MOD;
                dp[i+1][bit]=0;
            }
        }
    }

    cout<<res<<endl;

    return 0;
}

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#include<list>
#include<unordered_map>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end())
// #define int ll
using namespace std;
const int INF (1 << 30);
const ll LLINF (1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;
const double pi = acos(-1);
const double eps = 1e-9;

ll C[3000][3000];
void com(){
    C[0][0]=1;
    for(ll i=1;i<2010;i++){
        for(ll j=0;j<i+1;j++){
            if(j==0||j==i) C[i][j]=1;
            else{
                C[i][j]=C[i-1][j-1]+C[i-1][j];
                C[i][j]%=1000000007;
            }
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    com();
    ll n,x,y,z,ans=1;
    ll dp[50][1<<19];
    cin>>n>>x>>y>>z;
    rep(i,n){
        ans*=10;
        ans%=MOD;
    }
    ll ng = 0;
    ng |= 1<<z;
    ng |= 1<<(y+z);
    ng |= 1<<(x+y+z);
    ll mask = 1<<(x+y+z+1); mask--;
    rep(i,42)rep(j,(1<<19)) dp[i][j]=0;
    dp[0][1]=1;
    rep(i,n){
        rep(bit,mask+1){
            rep2(j,1,11){
                ll ns = ((bit<<j)&mask)+1;
                if((ns&ng) == ng) continue;
                dp[i+1][ns] += dp[i][bit];
                dp[i+1][ns] %= MOD;
            }
        }
    }
    rep(i,mask){
        ans-=dp[n][i];
        ans%=MOD;
    }
    cout<<(ans+MOD)%MOD<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz(x) (int)x.size()
#define int long long
typedef pair<int,int>pii;
typedef vector<int> vi;
typedef vector<vi> vii;
const int mxN=2e5+5,M=1e9+7;
const int LINF = 0x3f3f3f3f3f3f3f3f;
//did u check when n=1?
//is mxN correct?
set<int>s;
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    for(int i=0;i<3;i++){
        int x;cin>>x;
        s.insert(x);
    }
    if(sz(s)!=2)cout<<"No";
    else cout<<"Yes";
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int N,n,ans=0;
  cin>>N>>n;
  if(n){
    puts("-1");
    return 0;
  }
  for(int i=1;i<N;i++){
    int a;
    cin>>a;
    if(n>=a)ans+=a;
    else if(n+1==a)ans++;
    else{
      puts("-1");
      return 0;
    }
    n=a;
  }
  cout<<ans<<endl;
}


#include "bits/stdc++.h"
#define ll long long
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef pair<ll, ll> pairs;
typedef pair<ll, pairs> tpl;
ll c[4];
double dp[301][301][301];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, a;
    cin >> n;
    for (ll i = 0; i < n; i++)
        cin >> a, c[a]++;
    dp[0][0][0] = 0;
    for (ll i = 0; i <= n; i++)
    {
        for (ll j = 0; j <= n; j++)
        {
            for (ll k = 0; k <= n; k++)
            {
                double z = n - (i + j + k), val = 1;
                if (z == n)
                    continue;
                if (i + j + k > n)
                    continue;
                if (i > 0)
                    val += (1.0 * i / n) * dp[i - 1][j + 1][k];
                if (j > 0)
                    val += (1.0 * j / n) * dp[i][j - 1][k + 1];
                if (k > 0)
                    val += (1.0 * k / n) * dp[i][j][k - 1];
                if (z > 0)
                    val /= (1.0 - 1.0 * z / n);
                dp[i][j][k] = val;
            }
        }
    }

    cout << setprecision(9) << fixed << dp[c[3]][c[2]][c[1]];
}

#include <bits/stdc++.h>
using namespace std;
#define long long ll
#define f(i,n) for(int i=0;i<n;i++)
const int maxn=305;
double dp[maxn][maxn][maxn];
double recur(int k, int b, int c,int n){
	if(dp[k][b][c]>=0)return dp[k][b][c];
	if(k==0&&b==0&&c==0)return 0.0;
	double res=0.0;
	// dp[k][b][c]=0.0;
	if(k>0)res+=(k)*recur(k-1,b,c,n);
	if(b>0)res+=(b)*recur(k+1,b-1,c,n);
	if(c>0)res+=(c)*recur(k,b+1,c-1,n);
	// dp[k][b][c]/=(n-k-c-b);
	res+=n;
	res*=1.0/(k+b+c);
	return dp[k][b][c]=res;
}
int main() {

	cout<<fixed<<setprecision(10);
	int n;
	cin>>n;
	vector<int>a(4,0);
	f(i,n){int x;
	cin>>x;
	a[x]++;
	}
	memset(dp, -1, sizeof(dp));
	cout<<fixed<<setprecision(9)<<recur(a[1],a[2],a[3],n)<<endl;

	return 0;
}


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

int main(){
    string s;
    int ans, t;

    cin >> s;
    ans = 1000;
    for ( int i = 2;i < s.size(); i++ ) {
        t = (s[i-2] - '0') * 100 + (s[i-1] - '0') * 10 + s[i] - '0';

        ans = min(ans, abs(t-753));
    }
    cout << ans << endl;

    return (0);
}

#include<bits/stdc++.h>
int main() {
	int arr[26]{};
	std::string name;
	std::cin >> name;
	int n=name.size();
	bool yes = 1;
	for (int x = 0; x < n; x += 2) {
		if (name.substr(x, 2) != "hi") {
			yes = false;
		}
	}
	std::cout << (yes == 1 ? "Yes" : "No");

}


#include <bits/stdc++.h>

#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main()
{
    int m, min, max;
    while (cin >> m >> min >> max && m) {
        vector<int> v;
        loop(m, i) {
            int j; cin >> j;
            v.push_back(j);
        }

        int gap = 0;
        int ans = 0;
        for (int i = min; i <= max; i++) {
            if (gap <= v[i-1] - v[i]) {
                gap = v[i-1] - v[i];
                ans = i;
            }
        }
        cout << ans << endl;
    }
}

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

#define pb 		push_back
#define mp 		make_pair
#define prior   priority_queue
#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define PI 		3.1415926535897932384626433832795
#define ll      long long
#define ld		long double
#define ret     return
#define NUM 	1000001
const int SIZE = 2e5+4;
void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}
void input(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
}
void swap(int& a, int& b){
	int t=a;
	a = b;
	b = t;
}

void solveTest(){
	int A,B,M;
	cin>>A>>B>>M;
	int a[A+1],b[B+1];
	int minA = INT_MAX;
	int minB = INT_MAX;
	for(int i=1;i<=A;i++){
		cin>>a[i];
		if(a[i]<minA){
			minA=a[i];
		}
	}
	for(int i=1;i<=B;i++){
		cin>>b[i];
		if(b[i]<minB){
			minB = b[i];
		}
	}
	int minprice = INT_MAX;
	for(int i=0;i<M;i++){
		int x,y,c;
		cin>>x>>y>>c;
		if((a[x]+b[y]-c)<minprice){
			minprice = a[x]+b[y]-c;
		}
	}
	cout<<min(minA+minB,minprice)<<endl;
	return ;
}

int main(){
	fastio();
	//input();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();

	}

	return 0;
}


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

int main(){
    long int n,x;
    cin >> n >> x;
    vector<long int> a(n);
    long int sum=0;
    int i;

    for(i=0; i<n; i++){
        cin >> a.at(i);
        sum+=a.at(i);
    }
    sort(a.begin(),a.end());

    if(x>sum) cout << n-1 << endl;
    else if(x==sum) cout << n << endl;
    else{
        for(i=0; i<n; i++){
            sum-=a.at(n-1-i);
            if(x>=sum) break;
        }
        cout << n-1-i << endl;
    }
}

///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>

#define                           int                         long long
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const int inf=1e18;
const int mod=1e9+7;
const int M=100009;

inline int bigmod(int B,int P){int R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}
inline int ad(int x,int y){int ret=(x%mod+y%mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline int sub(int x,int y){int ret=((x%mod)-(y%mod)+mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline int gun(int x,int y){int ret=((x%mod)*(y%mod))%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}

 main()

{
    fast
    int n,k,x,mx=0;
    cin>>n>>k;
    int g=0;
    f(i,1,n)
    {
        cin>>x;
        mx=max(mx,x);
        g=__gcd(x,g);
    }
    if(k%g==0 && k<=mx)
    {
        cout<<"POSSIBLE"<<endl;
    }
    else
    {
        cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

string s;
int res = 0;
int cnt[3] = {0};

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    cin >> s;
    s += 'J';
    int mode = 1;

    rep(i, s.size()){
      if(mode == 1){
        if(s[i] == 'J')++cnt[0];
        else if(s[i] == 'O'){
          mode = 2; ++cnt[1];
        }else cnt[0] = 0;
      }else if(mode == 2){
        if(s[i] == 'J'){
          mode = 1;
          rep(j, 2)cnt[j] = 0;
          ++cnt[0];
        }else if(s[i] == 'O')++cnt[1];
        else{
          mode = 3; ++cnt[2];
        }
      }else{
        if(s[i] == 'J'){
          if(cnt[0] >= cnt[1] && cnt[1] <= cnt[2])res = max(res, cnt[1]);
          mode = 1;
          rep(j, 3)cnt[j] = 0;
          ++cnt[0];
        }else if(s[i] == 'O'){
          if(cnt[0] >= cnt[1] && cnt[1] <= cnt[2])res = max(res, cnt[1]);
          mode = 1;
          rep(j, 3)cnt[j] = 0;
        }else ++cnt[2];
      }
      //printf("%d %d %d %d %d\n", i, mode, cnt[0], cnt[1], cnt[2]);
    }
    pri(res);

/*/

//*/ break;
  }
  return 0;
}

#include<iostream>
using namespace std;

int main(){
  string s;
  cin >> s;
  int n = s.size();

  int ans = 0;
  for(int i=0;i<n;i++){
    if(s[i] == 'O'){
      int j = 0;
      while( (i+j<n) && (s[i+j] == 'O') )j++;

      int k = 0;
      while( (0<=i-1-k) && (k<j) && (s[i-1-k] == 'J'))k++;

      int l = 0;
      while( (i+j+l<n) && (l<j) && (s[i+j+l] == 'I'))l++;

      if(j==k && j==l)ans = max(ans,j);

      i += j+l-1;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) {
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P){if(P==0) return 1;
  if(P%2==0){ll t = jou(N, P/2);return t*t % mod;
  }return (N * jou(N, P-1)) % mod;}  //jou(n,mod-2)で逆元
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる

int main(){
  ll n;
  cin>>n;

  vector<ll> p(n);
  rep(i,n){
    cin>>p[i];
  }

  ll ans=1;
  vector<ll> q={0,0,0};
  rep(i,n){
    int cnt=0;
    int memo;
    rep(j,3){
      if(p[i]==q[j]) {
        cnt++;
        memo=j;
      }
    }
    if(cnt==0){
      cout << 0 << endl;
      return 0;
    }
    ans*=cnt;
    ans%=mod;
    q[memo]++;
  }

  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

const int mod = 1e9+7;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    vector<int> cnt(n);
    ll ans = 1;
    rep(i,n){
        if (a[i] == 0){
            ans *= 3 - cnt[a[i]];
        }else{
            ans *= (cnt[a[i]-1] - cnt[a[i]]);
        }
        cnt[a[i]]++;
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int64_t res = 0;
  vector table(20, vector(20, 0));
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    auto pos = s.find(".");
    if (pos == string::npos) {
      s += string(9, '0');
    } else {
      s.erase(s.begin() + pos);
      s += string(9 - ((int)s.size() - pos), '0');
    }
    int64_t x = static_cast<int64_t>(stoll(s));
    pair<int, int> a = make_pair(0, 0);
    while (x % 2 == 0) {
      x /= 2;
      a.first++;
    }
    while (x % 5 == 0) {
      x /= 5;
      a.second++;
    }
    a.first = min(a.first, 18);
    a.second = min(a.second, 18);
    for (int j = 18 - a.first; j < 20; j++) {
      for (int k = 18 - a.second; k < 20; k++) {
        res += table[j][k];
      }
    }
    table[a.first][a.second]++;
  }
  cout << res << '\n';
  return 0;
}

//
//  main.cpp
//  ALDS1_4_A
//
//  Created by ptyuan on 2018/10/22.
//  Copyright © 2018 YY. All rights reserved.
//

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

int linearSearch(int *array, int n, int key) {
    *(array + n ) = key;
    int i = 0;
    while (array[i] != key) {
        i++;
    }
    return i == n ? -1 : i;
}

int main(int argc, const char * argv[]) {
    int n = 0;
    int S[10000 + 1] = {0};
    int q = 0;
    int sum = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", S + i);
    }

    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        int key = 0;
        scanf("%d", &key);
        if (-1 != linearSearch(S, n, key)) {
            sum++;
        }
    }
    printf("%d\n", sum);

    return 0;
}


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

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

    vector<int> p(n);
    int cnt = 0, min = 200001;
    for (int i = 0; i < n; i++) {
        cin >> p.at(i);
        if (p.at(i) <= min) cnt++;
        if (p.at(i) < min) min = p.at(i);
    }

    cout << cnt << endl;

    return 0;
}

//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber

#include <bits/stdc++.h>

using namespace std;

// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;

// constants...
const double PI = acos(-1);
const int Mod = 1e9+7; // 998244353;
const int MXS = 2e5+5;
const int MXI = 1e9+5;
const ll MXL = 1e15+5;
const int INF = 1e5+5;

// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

// chess moves...
// int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right
// int dy[] = {+1, -1, +0, +0};
// int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
// int dx[] = {-2, -2, -1, -1,  1,  1,  2,  2}; ///Knight's Move
// int dy[] = {-1,  1, -2,  2, -2,  2, -1,  1};

// structs...
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

// functions...
ll gcd(ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll lcm(ll a, ll b)
{
    return (a/gcd(a, b)*b);
}
ll comb(ll a, ll b, ll M)
{
    ll x = max(a-b, b), ans=1;

    for(ll K=a, L=1; K>=x+1; K--, L++){
        ans *= K;
        ans /= L;
    }

    return ans % M;
}

void task()
{
    //code here...

    int n, ans, mn = MXI;

    cin >> n;
    int ara[n];

    for(int K=0; K<n; K++) cin >> ara[K];
    ans = n;

    for(int K=0; K<n; K++){
		mn = min(mn, ara[K]);

		if(mn<ara[K]) ans--;
	}

	cout << ans << "\n";
}

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

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    task();

    return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

#define N 262144
ll dp[2][2*N+5];
int n,q,a,b;

#define INM 100000000000000000LL
//0-index
void update(int id,int k,ll a)
{
	k += N-1;
	dp[id][k] = a;
	while(k > 0)
	{
		k = (k-1)/2;
		dp[id][k] = min(dp[id][k*2+1],dp[id][k*2+2]);
	}
}

ll query(int id,int a,int b,int k=0,ll l=0,ll r=N)
{
	if(r <= a || b <= l)return INM;
	if(a <= l && r <= b)return dp[id][k];
	else
	{
		ll vr = query(id,a,b,k*2+1,l,(l+r)/2);
		ll vl = query(id,a,b,k*2+2,(l+r)/2,r);
		return min(vr,vl);
	}
}

int main()
{
	cin >> n >> q >> a >> b;
	REP(i,2)REP(j,N)update(i,j,INM);

	vector<int> que;que.PB(a);

	REP(i,q)
	{
		int tmp;cin >> tmp;que.PB(tmp);
	}

	update(0,b,b);
	update(1,b,n-b);

	//cout  << query(1,0,n+1) << endl;

	ll sum = 0;
	for(int i = 1;i <= q;i++)
	{
		ll tmp = abs(que[i-1]-que[i]);
		sum += tmp;

		ll mi = min(query(1,0,que[i]+1)-(n-que[i]),query(0,que[i],n+1)-que[i]);
		//cout << query(1,0,que[i]+1)-que[i]+1<< ' ' << query(0,que[i],n+1)-que[i] << endl;
		update(0,que[i-1],mi-tmp+que[i-1]);
		update(1,que[i-1],mi-tmp+n-que[i-1]);
	}

	ll ans = INM;

	REP(i,n+1)
	{
		ans = min(ans,dp[0][i+N-1]-i);
		ans = min(ans,dp[1][i+N-1]-(n-i));
	}

	cout << ans + sum << endl;

	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;

ll n,arr[200010],a,b,c,ans=1e18,s1,s2,s3,s4;

ll sum( ll l , ll r ){ return arr[r]-arr[l-1]; }

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for(ll i=1; i<=n; i++) cin >> arr[i] , arr[i]+=arr[i-1];

    a = 1;
    c = 3;
    for(b=2; b<n-1; b++){

        while( a+1 < b && abs( sum( 1 , a+1 )-sum( a+2 , b ) ) <= abs( sum( 1 , a ) - sum( a+1 , b ) ) ) a++;
        while( c <= b ) c++;
        while( c+1 < n && abs( sum( b+1 , c+1 )-sum( c+2 , n ) ) <= abs( sum( b+1 , c ) - sum(c+1,n)) ) c++;

        s1 = sum( 1 , a );
        s2 = sum( a+1 , b );
        s3 = sum( b+1 , c );
        s4 = sum( c+1 , n );

        //cout << a << " " << b << " " << c << " " << s1 << " " << s2 << " " << s3 << " " << s4 << "\n";

        ans = min( ans , max( max( s1 , s2 ) , max( s3 , s4 ) ) - min( min( s1 , s2 ) , min( s3 , s4 ) ) );

    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
//#define PI 3.14159265358979

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
	for (int i = 0; i < A.size(); i++)
		os << A[i] << " ";
	os << endl;
	return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
	int N = A.size();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < A[i].size(); j++)
			os << A[i][j] << " ";
		os << endl;
	}
	return os;
}
template < typename T, typename U >
ostream &operator<<(ostream &os, const pair< T, U > &p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}

typedef pair< int, int > pii;
typedef long long ll;

struct edge {
	int from, to, d, c, i;
	edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
		from = _from;
		to = _to;
		d = _d;
		c = _c;
	}
	bool operator<(const edge &rhs) const {
		return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
	}
};
struct aabb {
	int x1, y1, x2, y2;
	aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
	int to, cap, rev, cost;
	flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

int pow2(int n) { return 1LL << n; }
template < typename T, typename U >
bool chmin(T &x, const U &y) {
	if (x > y) {
		x = y;
		return true;
	}
	return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}

template < typename A, size_t N, typename T >
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}
struct initializer {
	initializer() {
		cout << fixed << setprecision(20);
	}
};
initializer _____;

int N, M, K, T, Q, H, W;
signed main() {
	cin >> T;
	while (T--) {
		cin >> N;
		vector< int > A(N);
		rep(i, N) cin >> A[i];
		string S;
		cin >> S;
		vector< int > es;
		int ans = 0;
		for (int i = N - 1; i >= 0; i--) {
			int v = A[i];
			sort(es.rbegin(), es.rend());
			for (auto e : es) {
				chmin(v, v ^ e);
			}
			if (S[i] == '0') {
				if (v != 0) {
					es.push_back(v);
				}
			} else {
				if (v) {
					ans = 1;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#include <cstdlib>
#include <math.h>
#include<cstdio>
#include<cstring>
#define FOR(I, A, B) for (ll I = (A); I <= (B); I++)
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fo(i,n) for(ll i=0;i<n;i++)
#define sz(a) ll((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define dbg4(x,y,z,q) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << ", " << #q << " = " << q << endl
#define scan(char_array) scanf("%[^\n]s",&char_array);
struct three{
	ll a,b,c;

};

int main(){
	ll n,q;
	IOS
	cin>>n>>q;
	std::vector<three> v;
	ll aa,bb,cc;
	auto compare=[&](three a,three b){
		return a.a<b.a;

	};
	fo(i,n){
		three tmp;
		cin>>aa>>bb>>cc;
		tmp.a=cc;
		ll hi,lo;
		if(bb-1>=cc){
			hi=bb-1-cc;

		}
		else hi=-1;
		lo=max((ll)0,aa-cc);
		tmp.b=lo;
		tmp.c=hi;
		v.pb(tmp);

	}
	map<ll,ll> d;
	sort(all(v),compare);
	set<ll> st;
	fo(i,q){
		cin>>aa;
		d[aa]=-1;

		st.insert(aa);
	}
	for(auto kk:v){
		//dbg3(kk.a,kk.b,kk.c);
		std::vector<ll> out;
		auto it=st.lower_bound(kk.b);
		while(it!=st.end()){
			ll our=*it;
			if(our>=kk.b&&our<=kk.c){
				out.pb(our);

			}
			else break;
			it++;
		}
		for(auto pp:out){
			d[pp]=kk.a;

			st.erase(pp);

		}
	}
	for(auto ii:d){
		cout<<ii.second<<endl;
	}

}


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)

constexpr ll MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll a;

  vector<ll> one(60);
  rep(i, 0, n) {
    cin >> a;
    rep(j, 0, 60) if (a >> j & 1) one[j]++;
  }

  ll ans = 0;
  rep(i, 0, 60) ans =
    (ans + (1ll << i) % MOD * one[i] % MOD * (n - one[i]) % MOD) % MOD;

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

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define reps(i,a,b) for(int i= a; i < b; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int n;
int cnt = 62;
vector<ll> vec;
ll mod = 1e9+7;

ll solve() {
  vector<vector<int>> sum_zero(n+1, vector<int>(cnt, 0));
  vector<vector<int>> sum_one(n+1, vector<int>(cnt, 0));

  ll ans = 0;
  reps(i, 1, n+1) {
    rep(j,cnt) {
      if (vec.at(i-1) >> j & 1ll) {
        sum_one.at(i).at(j) = sum_one.at(i-1).at(j) + 1;
        sum_zero.at(i).at(j) = sum_zero.at(i-1).at(j);
      } else {
        sum_one.at(i).at(j) = sum_one.at(i-1).at(j);
        sum_zero.at(i).at(j) = sum_zero.at(i-1).at(j) + 1;
      }
    }
  }
  rep(i, n-1) {
    ll t = 1;
    rep(j, cnt) {
      ll v;
      if (vec.at(i) >> j & 1ll) {
        v = (sum_zero.at(n).at(j) - sum_zero.at(i+1).at(j));
      } else {
        v = (sum_one.at(n).at(j) - sum_one.at(i+1).at(j));
      }
      ans += v * t;
      ans %= mod;
      t *= 2;
      t %= mod;
    }
  }
  return ans;
}

int main()
{
  cin >> n;
  vec.resize(n);
  rep(i, n) cin >> vec.at(i);
  cout << solve() << endl;

  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+500;
int w[N],n;
int main()
{
    scanf("%d",&n);int ans=0;
    for(int i=1;i<=2*n;i++)scanf("%d",w+i);
    sort(w+1,w+n*2+1);
    for(int i=1;i<=2*n;i+=2)ans+=w[i];
    cout<<ans<<endl;
}

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

int main(){
  int n,ans;
  ans=0;
  cin >> n;
  vector<string> s(3);

  for(int i=0;i<3;i++){
	cin >> s[i];
  }
  for(int i=0;i<n;i++){
    if(s[0][i]==s[1][i] && s[0][i]==s[2][i]);
    else if(s[0][i]==s[1][i] || s[0][i]==s[2][i] || s[1][i]==s[2][i])ans++;
    else ans+=2;
  }
  cout << ans;
  return 0;
}


#include<iostream>
using namespace std;

int main(){
    int N,x;
    cin >> N >> x;
    int a[N];
    for(int i=0; i<N; i++){
        cin >> a[i];
    }

    long long total = 0;
    for(int pairIndex=0; pairIndex<N-1; pairIndex++){
        int first = pairIndex;
        int second = pairIndex+1;
        if(a[first] > x){
            int diff = a[first] - x;
            a[first] -= diff;
            total += diff;
        }
        if(a[first] + a[second] > x){
            int diff = a[first] + a[second] - x;
            a[second] -= diff;
            total += diff;
        }
    }

    cout << total << '\n';
}

#include "bits/stdc++.h"

using ll = long long;
using namespace std;

void go(int x) {
   cout << x << endl;
   exit(0);
}

void solve() {
   int x, y;
   cin >> x >> y;
   if(x == 0 || y == 0) {
      if(x == 0) {
         if(y >= 0) go(y - x);
         else go(abs(y) + 1);
      } else if(y == 0) {
         if(x < 0) go(abs(x));
         else go(abs(x) + 1);
      }
   }
   if(x < y) {
      if(x * 1LL * y >= 0) {
         go(y - x);
      } else {
         go(1 + abs(abs(y) - abs(x)));
      }
   } else if(x == y) {
      go(0);
   } else {
      //x > y;
      if(x * 1LL * y >= 0) {
         go(2 + abs(abs(x) - abs(y)));
      } else {
         go(1 + abs(abs(x) - abs(y)));
      }

   }
}

int main() {
   cin.tie(0);
   cin.sync_with_stdio(0);

   int testcase = 1;
   // cin >> testcase;

   for(int i = 0; i < testcase; i++) {
      solve();
   }

   return 0;
}


//
//  main.c
//  ITP1_5_D
//
//  Created by ??±??°?????? on 2015/10/11.
//  Copyright ?? 2015??´ HamadaShogo. All rights reserved.
//

#include <stdio.h>

void call(int n);

int main(int argc, const char * argv[]) {
    int num;

    scanf("%d", &num);
    call(num);

    return 0;
}

void call(int n){
    int i;
    int x;

    for (i=1; i<=n; i++) {
        x = i;
        if (x%3 == 0) {
            printf(" %d", i);
        }
        else{
            while (x){
                if (x%10 == 3) {
                    printf(" %d", i);
                    break;
                }
                x /= 10;
            }
        }

    }
    puts("");
}

#include <bits/stdc++.h>

#define int long long
#define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define tc(T) int T; cin>>T; while(T--)
#define q(q) int q; cin>>q; while(q--)
#define pi 3.1415926535897932384626433832
const int pinf=((int)9e18);
const int ninf=((int)-9e18);
const int mod=1000000007;

using namespace std;

int32_t main()
{
    fast_io();

    //FILE *fptr1=freopen("in.txt","r",stdin);
    //FILE *fptr2=freopen("out.txt","w",stdout);

    int r;
    cin>>r;

    double ans=2LL*pi*r;

    printf("%0.12f",ans);

    return 0;
}


#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

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

   int n, k;
   cin >> n >> k;
   vector<int> a(n);
   rep(i, 0, n) cin >> a[i];

   int ans = 0;
   rep(l, 0, k + 1) rep(r, 0, k - l + 1) {
      if (l + r > n) continue;
      int d = k - l - r;
      int now = 0;
      vector<int> s;
      rep(i, 0, l) {
         now += a[i];
         s.push_back(a[i]);
      }
      rep(i, n - r, n) {
         now += a[i];
         s.push_back(a[i]);
      }
      sort(s.begin(), s.end());
      rep(i, 0, d) {
         if (i >= s.size()) break;
         if (s[i] >= 0) break;
         now -= s[i];
      }
      ans = max(ans, now);
   }
   cout << ans << endl;
   return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP2(i, a, b) for(int i = a;i <= b;i++)
#define REPR(i, a, b) for(int i = a; i >= b; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define PI 3.14159265358979323846
#define vi vector<int>
#define vll vector<ll>
#define vi2 vector<vector<int>>
#define eb emplace_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define sz(x) int(x.size())
using namespace std;
using P = pair<ll,ll>;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
#define INT(name) int name;cin >> name;
#define VEC(type,name,n) vector<type> name(n);REP(i,n) cin >> name[i];

template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll solve(int N,int K,vector<ll> A){
  deque<ll> deq2;
  REP(i,N){
    deq2.push_back(A[i]);
  }

  ll MAX = 0;
  REP(i,K+1){
    if(i>=N){
      vector<ll> vec=A;
      if(vec.size()>0){
        sort(ALL(vec));
        ll sum = 0;
        REP(k,vec.size()){
          sum+=vec[k];
        }
        REP(k,min(sz(vec),K-i)){
          if(vec[k]<0){
            sum-=vec[k];
          }
        }
        MAX=max(MAX,sum);
      }
    }else{
      REP(j,i+1){
        vector<ll> vec;
        deque<ll> deq=deq2;
        REP(k,j){
          vec.push_back(deq.front());
          deq.pop_front();
        }
        REP(k,i-j){
          vec.push_back(deq.back());
          deq.pop_back();
        }
        if(vec.size()>0){
          sort(ALL(vec));
          // REP(k,vec.size()){
          //   cout << vec[k] << " ";
          // }
          // cout << endl;
          ll sum = 0;
          REP(k,vec.size()){
            sum+=vec[k];
          }
          REP(k,min(sz(vec),K-i)){
            if(vec[k]<0){
              sum-=vec[k];
            }
          }
          MAX=max(MAX,sum);
          // cout << MAX << endl;
          // cout << i << "a" << j << endl;
        }
      }
    }
  }

  return MAX;
}

ll dfs(deque<ll> deq,vector<ll> A,int cnt,int K){
  ll ret = 0;
  REP(i,sz(A)){
    ret+=A[i];
  }
  if(cnt==K){
    return ret;
  }
  cnt++;
  ll MAX = ret;
  REP(i,sz(A)){
    vector<ll> B=A;
    deque<ll> deq2=deq;
    deq2.push_back(B[i]);
    B.erase(B.begin()+i);
    MAX=max(MAX,dfs(deq2,B,cnt,K));
    deque<ll> deq3=deq;
    vector<ll> C=A;
    deq3.push_front(C[i]);
    C.erase(C.begin()+i);
    MAX=max(MAX,dfs(deq3,C,cnt,K));
  }
  if(deq.size()>0){
    deque<ll> deq4=deq;
    vector<ll> D=A;
    D.push_back(deq4.back());
    deq4.pop_back();
    MAX=max(MAX,dfs(deq4,D,cnt,K));
    deque<ll> deq5=deq;
    vector<ll> E=A;
    E.push_back(deq5.front());
    deq5.pop_front();
    MAX=max(MAX,dfs(deq5,E,cnt,K));
  }

  return MAX;
}

ll solve2(int N,int K,vector<ll> A){
  deque<ll> deq2;
  REP(i,N){
    deq2.push_back(A[i]);
  }

  vector<ll> EMPTY;
  return max(0ll,dfs(deq2,EMPTY,0,K));
}

int main()
{
  INT(N);
  INT(K);
  VEC(ll,A,N);
  cout << solve(N,K,A) << endl;
  // cout << solve2(N,K,A) << endl;
  random_device seed_gen;
  mt19937_64 engine(seed_gen());
  while(0){
    ll N=engine()%(ll)10+1;
    ll K=engine()%(ll)3+1;
    vector<ll> A;
    REP(i,N){
      ll x=engine()%(ll)2e7+1-(ll)1e7;
      A.push_back(x);
    }
    cout << "checking input : " << N << " " << K << endl;
    REP(i,N){
      cout << A[i] << " ";
    }
    cout << endl;
    ll sol1=solve(N,K,A);
    ll sol2=solve2(N,K,A);
    if(sol1==sol2){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
      cout << "solver1 " << sol1 << endl;
      cout << "solver2 " << sol2 << endl;
      break;
    }
  }
}


// AtCoder Beginner Contest 167 - Problem F: Bracketing Sequencing (https://atcoder.jp/contests/abc167/tasks/abc167_f)
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

string solve(int N, const vector<string>& xs)
{
    vector<int> cs;
    vector<ii> ds, es;

    int open = 0, close = 0;

    for (int i = 0; i < N; ++i)
    {
        int a = 0, b = 0;

        for (auto c : xs[i])
        {
            open += (c == '(' ? 1 : 0);
            close += (c == ')' ? 1 : 0);

            b += (c == '(' ? 1 : -1);

            if (b < 0)
            {
                a--;
                b = 0;
            }
        }

        if (b + a >= 0)
            ds.push_back(ii(-a, i));
        else
            es.push_back(ii(b, i));
    }

    if (open != close)
        return "No";

    sort(ds.begin(), ds.end());
    sort(es.begin(), es.end(), greater<ii>());

    int s = 0;

    for (auto p : ds)
    {
        auto i = p.second;

        for (auto c : xs[i])
        {
            s += (c == '(' ? 1 : -1);

            if (s < 0)
                return "No";
        }
    }

    for (auto p : es)
    {
        auto i = p.second;

        for (auto c : xs[i])
        {
            s += (c == '(' ? 1 : -1);

            if (s < 0)
                return "No";
        }
    }

    return "Yes";
}

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

    int N;
    cin >> N;

    vector<string> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i];

    auto ans = solve(N, xs);

    cout << ans << '\n';

    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

bool cmp(pii x, pii y) {
    return 1ll * x.first * y.second < 1ll * x.second * y.first;
}

int main() {
    ios::sync_with_stdio(false);
    int n, m = 0;
    pii a[1000006] = {};
    string s;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s;
        int x = 0, y = 0;
        for (int j = 0; j < s.length(); j++) {
            if (s[j] == '(') x++;
            else x--;
            y = min(y, x);
        }
        a[i] = {-y, x};
    }
    sort(a, a + n, cmp);
    //for (int i = 0; i < n; i++) cout << a[i].first << ' ' << a[i].second << '\n';
    for (int i = 0; i < n; i++) {
        if (m < a[i].first) {
            cout << "No";
            return 0;
        }
        m += a[i].second;
    }
    if (m) cout << "No";
    else cout << "Yes";
}


#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 1e5 + 10;

int n, m, res[N], deg[N];
vector <int> adj[N];

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    FOR(i, 1, m + n - 1){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        deg[v]++;
    }
    int root = 0;
    FOR(i, 1, n)
        if (deg[i] == 0)
            root = i;
    queue <int> Q;
    Q.push(root);
    while (!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int v : adj[u])
            if (--deg[v] == 0){
                Q.push(v);
                res[v] = u;
            }
    }
    FOR(i, 1, n)
        cout << res[i] << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template <class T>
string to_string(T s);
template <class S, class T>
string to_string(pair<S, T> p);
string to_string(string s);

template <class T>
string to_string(T v) {
  if (v.empty()) return "{}";
  string ret = "{";
  auto itr = v.begin(), next = ++v.begin();
  for (; next != v.end(); ++itr, ++next) ret += to_string(*itr) + ",";
  ret += to_string(*itr) + "}";
  return ret;
}
template <class S, class T>
string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}
string to_string(string s) { return s; }

void debug() { cerr << endl; }
template <class Head, class... Tail>
void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head head, Tail... tail) {
  cout << to_string(head) << " ";
  print(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  string separator = " ";
  template <class T>
  inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus) x = -x;
  }
  inline void read(string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\n' && c != EOF);
  }
  template <class T>
  inline void read(vector<T> &v) {
    for (auto &x : v) read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &... tail) {
    read(head);
    read(tail...);
  }
  template <class T>
  inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0) putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(string x) {
    for (char c : x) putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]);
  }
  template <class T>
  inline void write(vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  void set_separator(string s) { separator = s; }
} io;

int main() {
  int N, M;
  io.read(N, M);
  vector<vector<int>> G(N), RG(N);
  REP(i, N + M - 1) {
    int a, b;
    io.read(a, b);
    G[a - 1].push_back(b - 1);
    RG[b - 1].push_back(a - 1);
  }
  int root = -1;
  REP(i, N) if (RG[i].empty()) root = i;
  vector<int> ans(N, -1);
  function<void(int)> dfs = [&](int u) {
    for (auto v : G[u]) {
      if (ans[v] == -1) {
        bool f = 1;
        for (int k : RG[v])
          if (ans[k] == -1 && k != root) f = 0;
        if (f) {
          ans[v] = u;
          dfs(v);
        }
      }
    }
  };
  dfs(root);
  REP(i, N) {
    io.write(ans[i] + 1);
    io.write("\n");
  }
  return 0;
}


#include <iostream>
#include <vector>

long int cnt = 0;

void swap(std::vector<int>& arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

void merge(std::vector<int>& arr, int start, int mid, int end) {
    int length = end -start;
    int left = start, right = mid;
    std::vector<int> tmp(length);
    for (int i=0; i<length; i++) {
        if ( !(right < end) ) {
            tmp[i] = arr[left];
            left++;
        } else if ( !(left < mid) ) {
            tmp[i] = arr[right];
            right++;
        } else if ( arr[left] < arr[right] ) {
            tmp[i] = arr[left];
            left++;
        } else {
            tmp[i] = arr[right];
            right++;
            cnt+= mid - left;
        }
    }

    for (int i=0; i<length; i++) {
        arr[start+i] = tmp[i];
    }
}

void merge_sort(std::vector<int>& arr, int start, int end) {
    if ( end - start > 1 ) {
        if ( end - start == 2 ) {
            if ( arr[start] > arr[start+1] ) {
                swap(arr, start, start+1);
                cnt++;
            }
        } else {
            int mid = ( start + end ) / 2;
            merge_sort(arr, start, mid);
            merge_sort(arr, mid, end);
            merge(arr, start, mid, end);
        }
    }
}

int main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i=0; i<n; i++) {
        std::cin >> a[i];
    }

    merge_sort(a, 0, n);

    std::cout << cnt << std::endl;
}

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

int main(){
	int e;
	cin >> e;
	while(e!=0){
		int Min = 1000000;
		for(int i=0; i<e+1; i++){
			int z = i;
			if(z*z*z > e){
				break;
			}
			int y = 0;
			int x = 0;
			while( (y+1)*(y+1) <= e-z*z*z ){
				y++;
			}
			x = e-z*z*z-y*y;
			if(Min > x+y+z){
				Min = x+y+z;
			}
		}
		cout << Min << endl;
		cin >> e;
	}
}


/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <stack>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#include <functional>
#define   mod  998244353
#define   int  long long
#define   ld   long double
#define   pb   push_back
#define   vi   vector<int>
#define   co(x)  cout << x << '\n'
#define   dbg(x)  cerr << #x << " = " << x << '\n'
#define   bitcount(x)  (int)__builtin_popcount(x)
#define   sz(x)  (int)x.size()
#define   all(a) a.begin(),a.end()
#define   ff   first
#define   ss   second
#define   inf  1000000007;
#define   pii  pair<int,int>
#define   lcm(a,b) (a*b)/__gcd(a,b)
using namespace std;

int  I[100005],L[100005];

inline void solve(){
    int n; cin>>n;
    int  a[n+1], lis = 0 ;
    I[0] = -inf ;
    for (int i=1;i<=n;i++){
    	cin >> a[i] ;
    	I[i] = inf ;
    	L[i] = 1 ;
    }
    reverse (a+1,a+n+1);
    for (int i=1;i<=n;i++) {
    	int  l = 0, r = lis ;
    	while (l <= r){
    		int  mid = ( l + r ) >> 1 ;
    		if (I[mid] <= a[i]) l = mid + 1 ;
    		else r = mid - 1 ;
    	}
    	I[l] = a[i] ;
    	if (l > lis) lis = l ;
    }
    co (lis) ;
}

signed main()
{
    int n=1; // cin>>n;
    while (n--) solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main()
{
    int n; cin >> n;
    vector<int> b(n), dp(n+1, -1);
    rep(i, n) cin >> b[i];
    rep(i, n){
        int p = lower_bound(dp.rbegin(), dp.rend(), b[i]) - dp.rbegin() - 1;
        p = n - p;
        // pr(p);
        if(p==-1) dp[0] = b[i];
        else dp[p] = b[i];
        // prvec(dp);
    }

    rep(i, n+1){
        if(dp[i]==-1) {pr(i); break;}
    }

    vector<int> a = {8,5,2};
    // pr(3 - 1 - ((upper_bound(a.rbegin(), a.rend(), 1) - a.rbegin()) - 1));
    return 0;}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

template <typename Tp> inline void getint(Tp &num){
	register int ch, neg = 0;
	while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
	num = ch & 15;
	while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
	if(neg) num = -num;
}

int N, K;

int main(){
	getint(N), getint(K);
	return printf("%d\n", N - K + 1), 0;
}

// A - Rated for Me
#include <bits/stdc++.h>
using namespace std;

int main(){
	int r; cin>>r;
	if(r < 1200) puts("ABC");
	else if(2800 <= r) puts("AGC");
	else puts("ARC");
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl
#define show2(a, n, m) rep(i, n) { rep(j, m) {cout << a[i][j] << ' ';} cout << endl;}
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int main() {
//	cout << fixed << setprecision(15);
	int n;
	cin >> n;
	cout << n / 3 << endl;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
const int INF=1001001001;

int main() {
  int64_t N;
  cin>>N;
  int x=N;
  string t;
  while(x){
   int r=x%(-2);
   if(r<0){r+=2;}
   x-=r;
   x/=(-2);
   t+=(char)('0'+r);
  }
  reverse(t.begin(),t.end());
  if(t==""){t='0';}
  cout<<t<<endl;
  return 0;
}


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

int main(){

	long long int n , id , price , num, cnt;
	while(cin >> n , n!=0){
		vector<long long int> p(4001,0);
		vector<int> a;
		while(n--){
			cin>>id>>price>>num;
			if(find(a.begin(),a.end(),id)==a.end()) a.push_back(id);
			p[id]+=price*num;
			}
		cnt=0;
		for(int i=0;i<a.size();i++){
			if(p[a[i]]>=1000000){
				cout << a[i] << endl;
				cnt++;
			}
		}
		if (cnt==0) cout << "NA" << endl;
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
#include<numeric>
using namespace std;

int main(void) {

  int n;
  while(cin >> n){
    if( n == 0 ) break;

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

    vector<int> dp(n+5, 0);
    for(int i=1; i<=n; i++){
      dp[i] = max( a[i-1], dp[i-1] + a[i-1] );
    }
    int ans = dp[1];
    for(int i=1; i<=n; i++){
      ans = max(ans, dp[i]);
    }
    cout << ans << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const double PI = acos(-1.0);

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

	int n;
	cin >> n;

	for(int i = 1; i <= n; i++) {
		bool flag = false;
		if(i % 3 == 0) {
			flag = true;
		}
		int j = i;
		while(1) {
			int r = j % 10;
			j /= 10;
			if(r == 3) {
				flag = true;
				break;
			}
			if(j == 0) break;
		}
		if(flag) cout << " " << i;
	}

	cout << "\n";

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void readin(T &x) {
    x = 0;
    T fh = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
    x *= fh;
}
inline void d_read(double &x) {
    x = 0.0;
    int fh = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
    if (ch == '.') {
        double num = 1.0;
        ch = getchar();
        for (; isdigit(ch); ch = getchar()) x = x + (num /= 10) * (ch ^ 48);
    }
    x *= fh;
}
template <typename T>
inline void wt(T x) {
    if (x > 9) wt(x / 10);
    putchar(x % 10 + 48);
}
template <typename T>
inline void writeln(T x, char c) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    wt(x);
    putchar(c);
}
const int N = 2e5 + 5;
int a[N << 2], t[N << 2], tg[N << 2];
inline void up(int p) {
    a[p] = min(a[p << 1], a[p << 1 | 1]);
    t[p] = min(t[p << 1], t[p << 1 | 1]);
}
inline void work(int p, int l, int r, int k) {
    a[p] = k;
    t[p] = k + l;
    tg[p] = k;
}
inline void build(int p, int l, int r) {
    tg[p] = -1e9;
    if (l == r) {
        a[p] = -l;
        t[p] = a[p] + l;
    }
    else {
        int mid = l + r >> 1;
        build(p << 1, l, mid);
        build(p << 1 | 1, mid + 1, r);
        up(p);
    }
}
inline void down(int p, int l, int r) {
    if (tg[p] != -1e9) {
        int mid = l + r >> 1;
        a[p << 1] = a[p << 1 | 1] = tg[p];
        tg[p << 1] = tg[p << 1 | 1] = tg[p];
        t[p << 1] = tg[p] + l;
        t[p << 1 | 1] = tg[p] + mid + 1;
        tg[p] = -1e9;
    }
}
int sum;
inline void ask(int p, int l, int r, int pos) {
    if (l == pos && pos == r) {
        sum = a[p];
    }
    else {
        down(p, l, r);
        int mid = l + r >> 1;
        if (pos <= mid) ask(p << 1, l, mid, pos);
        else ask(p << 1 | 1, mid + 1, r, pos);
    }
}
inline void modify(int p, int l, int r, int ql, int qr, int k) {
    if (l >= ql && r <= qr) {
        a[p] = k;
        t[p] = a[p] + l;
        tg[p] = k;
    }
    else {
        down(p, l, r);
        int mid = l + r >> 1;
        if (ql <= mid) modify(p << 1, l, mid, ql, qr, k);
        if (mid < qr) modify(p << 1 | 1, mid + 1, r, ql, qr, k);
        up(p);
    }
}
int n, m;
int main() {
    readin(n); readin(m);
    int x, y;
    build(1, 1, m);
    for (int i = 1; i <= n; i ++) {
        readin(x); readin(y);
        sum = 1e8;
        if (x > 1) ask(1, 1, m, x - 1);
        modify(1, 1, m, x, y, sum);
        if (t[1] + i >= 1e8) puts("-1");
        else writeln(t[1] + i, '\n');
    }
    return 0;
}


// Written by newbiechd
#include <cstring>
#include <iostream>
#include <string>

const int maxN = 13;
int n;
char col[maxN][maxN];
bool vis[26];
void dfs(int x, int y, char c) {
  if (x > n) {
    int val = 0;
    memset(vis, 0, sizeof(vis));
    for (int j = 1; j <= n; ++j) vis[col[1][j] - 'a'] = 1;
    for (char t = 'a'; t <= c; ++t) val += vis[t - 'a'];
    if (val != 3)
      return ;
    for (int i = 2; i <= n; ++i) {
      memset(vis, 0, sizeof(vis));
      int tmp = 0;
      for (int j = 1; j <= n; ++j) vis[col[i][j] - 'a'] = 1;
      for (char t = 'a'; t <= c; ++t) tmp += vis[t - 'a'];
      if (tmp != val)
        return ;
    }
    for (int j = 1; j <= n; ++j) {
      memset(vis, 0, sizeof(vis));
      int tmp = 0;
      for (int i = 1; i <= n; ++i) vis[col[i][j] - 'a'] = 1;
      for (char t = 'a'; t <= c; ++t) tmp += vis[t - 'a'];
      if (tmp != val)
        return ;
    }
    for (int i = 1; i <= n; ++i)
      printf("%s\n", col[i] + 1);
    putchar('\n'), fflush(stdout);
    return ;
  }
  if (y > n) {
    dfs(x + 1, 1, c);
    return ;
  }
  if (col[x][y - 1] == '.') {
    col[x][y] = col[x][y - 1] = c;
    dfs(x, y + 1, c + 1);
    col[x][y] = col[x][y - 1] = '.';
  }
  if (col[x - 1][y] == '.') {
    col[x][y] = col[x - 1][y] = c;
    dfs(x, y + 1, c + 1);
    col[x][y] = col[x - 1][y] = '.';
  }
  dfs(x, y + 1, c);
}

const std::string ans[][7] = {
  {
    "aabc",
    "ddbc",
    "efgg",
    "efhh"
  },
  {
    "aabbd",
    "ccf.d",
    "eefgg",
    "..hij",
    "..hij"
  },
  {
    "aabbcc",
    "ddeeff",
    "ghi...",
    "ghi...",
    "...jkl",
    "...jkl"
  },
  {
    "aabbcc.",
    "ddee..h",
    "ffgg..h",
    "....ijk",
    "....ijk",
    "....lmn",
    "....lmn"
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cin >> n;
  /*
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      col[i][j] = '.';
  dfs(1, 1, 'a');
  */

  if (n < 3) {
    printf("-1\n");
    return 0;
  }
  if (n == 3) {
    std::cout << "aa.\n..a\n..a\n";
    return 0;
  }

  int k = (n & 3) + 4;
  for (int i = 0; i < n - k; ++i) {
    std::string str(n, '.');
    str.replace(i / 4 * 4, 4, ans[0][i % 4]);
    std::cout << str << '\n';
  }
  for (int i = n - k; i < n; ++i) {
    std::string str(n, '.');
    str.replace(n - k, k, ans[k - 4][i + k - n]);
    std::cout << str << '\n';
  }
  return 0;
}

#include<bits/stdc++.h>

using namespace std;

const int N = 123456;

int n, a[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int type = 1;
  while (1) {
    int even = 0;
    for (int i = 1; i <= n; ++i) {
      if (!(a[i] & 1)) {
        ++even;
      }
    }
    if (even & 1) {
      cout << (type == 1 ? "First" : "Second") << '\n';
      exit(0);
    } else if (!(even & 1) && n - even >= 2) {
      cout << (type == 1 ? "Second" : "First") << '\n';
      exit(0);
    } else {
      for (int i = 1; i <= n; ++i) {
        if (a[i] & 1) {
          if (a[i] == 1) {
            cout << (type == 1 ? "Second" : "First") << '\n';
            exit(0);
          } else {
            --a[i];
          }
        }
      }
      int gcd = 0;
      for (int i = 1; i <= n; ++i) {
        gcd = __gcd(gcd, a[i]);
      }
      for (int i = 1; i <= n; ++i) {
        a[i] /= gcd;
      }
    }
    type = !type;
  }
  return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#define int long long
using namespace std;
const int INF = 1000000000000;
const int mod = 1000000007;
int cnt[345678];
signed main() {
	int n; cin >> n;
	cout << (n - 2) * 180 << endl;
	return 0;
}

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

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

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T>
void fin(T a)
{
  cout << a << endl;
  exit(0);
}

//桁数
ll GetDigit(ll num)
{
  return log10(num) + 1;
}

//約数全列挙
vector<ll> enum_div(ll n)
{
  vector<ll> ret;
  for (ll i = 1; i * i <= n; ++i)
  {
    if (n % i == 0)
    {
      ret.push_back(i);
      if (i * i != n)
      {
        ret.push_back(n / i);
      }
    }
  }
  sort(begin(ret), end(ret));
  return ret;
}

//累乗(xのn乗%mod)
ll modpow(ll x, ll n, ll mod = MAX)
{
  ll res = 1;
  while (n > 0)
  {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

//階乗
ll factorial(ll n)
{
  if (n > 0)
    return n * factorial(n - 1);
  else
    return 1;
}

ll modinv(ll a, ll mod)
{
  ll b = mod, u = 1, v = 0;
  while (b)
  {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll gcd(ll a, ll b)
{
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b)
{
  return a * b / gcd(a, b);
}

void Main()
{
  str s;
  cin >> s;
  set<char> flag;
  rep(i, s.size())
  {
    flag.insert(s[i]);
  }
  for (char i = 'a'; i <= 'z'; i++)
  {
    if (!flag.count(i))
      fin(s + i);
  }
  if (s == "zyxwvutsrqponmlkjihgfedcba")
    fin(-1);
  vc SS;
  SS.pb(s[s.size() - 1]);
  s.pop_back();
  while(1)
  {
    sort(all(SS));
    for(ll i = 0;i < SS.size();i++)
    {
      if(s[s.size() - 1] < SS[i])
      {
        s.pop_back();
        cout << s << SS[i] << endl;
        return;
      }
    }
    SS.pb(s[s.size() - 1]);
    s.pop_back();
  }
}

//前処理
int main()
{
  //ios::sync_with_stdio(false);
  //cin.tie(0);
  cout << fixed << setprecision(20); //高精度少数表示
  //cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
const double pi=3.14159265358979323846;
int ctoi(const char c) {
    if ('0' <= c && c <= '9') return (c - '0');
    return -1;
}
vector<int> input(int n) {
    vector<int> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec.at(i);
    }
    return vec;
}
void output(vector<int> vec) {
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    return;
}
vector<vector<int>> input(int n, int m) {
    vector<vector<int>> table(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> table. at(i).at(j);
        }
    }
    return table;
}
void output(vector<vector<int>> table) {
    for (int i = 0; i < table.size(); i++) {
        for (int j = 0; j < table.at(0).size(); j++) {
            cout << table.at(i).at(j) << " ";
        }
        cout << endl;
    }
}
long long perm(int n, int r) {
    if (n < r) {
        cout << "error" << endl;
        return 0;
    }
    long long perm = 1;
    for (int i = n; i > n - r; i--) {
        perm *= i;
    }
    return perm;
}
long long comb(int n, int r) {
    if (n < r) {
        cout << "error" << endl;
        return 0;
    }
    long long comb = perm(n,r);
    for (int i = r; i > 0; i--) {
        comb /= i;
    }
    return comb;
}
long long homo(int n, int r) {
    return comb(n + r - 1, n - 1);
}
long long fact(int n) {
    long long fact = 1;
    for (int i = n; i > 0; i--) {
        fact *= i;
    }
    return fact;
}
int gcd(int a, int b){
    if (a % b == 0){
        return(b);
    }else{
        return(gcd(b, a % b));
    }
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
bool isprime(int n){
    if (n < 2) return false;
    else if (n == 2) return true;
    else if (n % 2 == 0) return false;

    for (int i = 3; i <= sqrt(n); i += 2){
        if (n % i == 0){
            return false;
        }
    }
    return true;
}
void sayYes(bool f) {
    if (f) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
void sayYES(bool f) {
    if (f) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}

int main() {
  int a,b;
  cin >> a >> b;
  int ans=0;
  if(4-a>0){
    ans+=(4-a)*1e5;
  }
  if(4-b>0){
    ans+=(4-b)*1e5;
  }
  if(a==1&&b==1){
    ans+=4e5;
  }
  cout << ans << endl;
}

#include <iostream>
  #include <complex>
  #include <vector>
  #include <string>
  #include <algorithm>
  #include <cstdio>
  #include <numeric>
  #include <cstring>
  #include <ctime>
  #include <cstdlib>
  #include <set>
  #include <map>
  #include <unordered_map>
  #include <unordered_set>
  #include <list>
  #include <cmath>
  #include <bitset>
  #include <cassert>
  #include <queue>
  #include <stack>
  #include <deque>
 #include <iomanip>
#include <fstream>

  using namespace std;

//abcdefghijklmnopqrstuvwxyz
//ABCDEFGHIJKLMNOPQRSTUVWXYZ

  typedef long long ll;
  typedef long double lld;
  typedef unsigned long long ull;
  // ll INF = INT_MAX;
  // ll mod = 1000000007;
  // lld PI = 3.141592653;

// const int N = 2e5 + 5;

  int main()
  {
      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
      // ifstream cin("input.txt");

     int n,k;
     cin>>n>>k;
     vector<int> a(n);
     for (int i = 0;i<n;i++) cin>>a[i];
      vector<ll> pref(n);

    pref[0] = a[0];
    for (int i = 1;i<n;i++) pref[i] = pref[i-1] + a[i];
       lld ans = lld((k+pref[k-1]))/2;
    for (int i = k;i<n;i++){
      ll sum = pref[i] - pref[i-k];
      ans = max(ans, (lld(k)+sum)/2);
    }
cout<<setprecision(20)<<ans<<"\n";
}


#include <cstdio>
using namespace std;

typedef long long int LLI;

LLI d;

int main() {
  while (1) {
    if (scanf("%lld", &d) != 1) break;

    LLI ans = 0;
    for (LLI i=0; i<600; i+=d) {
      ans += i*i*d;
    }
    printf("%lld\n", ans);
  }
}

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int f(int);
int integral(int);

int main(void) {
	int a, b;

	while(cin >> a >> b) {
		cout << integral(a) << endl;
		cout << integral(b) << endl;
	}
	return 0;
}
int integral(int d){
	int n = 600 / d;
	int sum = 0;

	for (int i = 1; i < n; i++) {
		sum = sum + f(d * i) * d;
	}

	return sum;
}

int f(int a) {
	return a * a;
}


#include<stdio.h>
int main(){
	int A, B;
	int mark =0;
	scanf("%d %d", &A, &B);
	char in[A+B+1];
	scanf("%s", in);
	int ctr = 0;
	for(int i = 0; i < A+B+1 ; i++){
		if(in[i] == '-' && i == A){
			mark = 1;
		}else if(in[i] >= '0' && in[i] <= '9' ){
			++ctr;
		}
	}
	if( mark == 1 && ctr == A+B){
		printf("Yes\n");
	}else{
		printf("No\n");
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

vector<pair<long long, long long>> primeFactorize(long long N) {
    vector<pair<long long, long long>> res;
    for (long long i = 2; i * i <= N; ++i) {
        if (N % i != 0)
            continue;
        long long powInd = 0;
        while (N % i == 0) {
            powInd++;
            N /= i;
        }
        res.push_back({i, powInd});
    }
    if (N != 1)
        res.push_back({N, 1});
    return res;
}

signed main() {
    cin >> A >> B;
    N = gcd(A, B);
    auto v = primeFactorize(N);
    dump(v);
    cout << v.size() + 1 << endl;
}


#include <algorithm>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int n, m;
int p[1003];
void solve() {
    sort(p, p + n);
    reverse(p, p+n);
    int ans = 0;

    rep(i, n) ans += p[i];
    for (int i = 0; i + m - 1 < n; i += m) {
        ans -= p[i + m - 1];
    }
    cout << ans << endl;
}
int main() {
    while (cin >> n >> m) {
        rep(i, n) cin >> p[i];
        if (n == 0 && m == 0) break;
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<LL> Vl;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vl> VVl;
typedef pair<LL, LL> Pll;
typedef pair<DD, DD> Pdd;
typedef vector<Pll> Vpl;
typedef vector<Pdd> Vpd;
typedef queue<LL> Ql;
typedef stack<LL> Sl;
typedef deque<LL> Dl;

LL _set(LL N, LL pos) {return N = N | (1 << pos);}
LL _reset(LL N, LL pos) {return N = N & ~(1 << pos);}
bool _check(LL N, LL pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

LL dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
LL dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int a[mx5], c[mx5];

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

    int ans = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        c[i - 1] = abs(a[i] - a[i - 1]);
        ans += c[i - 1];
    }

    c[n] = abs(a[n] - 0);
    ans += c[n];

    for(int i = 1; i <= n; i++){
        if(i == n)
            cout << ans - (c[i - 1] + c[i]) + abs(a[i - 1] - 0) << '\n';
        else
            cout << ans - (c[i - 1] + c[i]) + abs(a[i - 1] - a[i + 1]) << '\n';
    }
    return 0;
}


#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;

const long long int mod = 1000000007;
const long long int INF = 1e18;
const long double PI=3.14159265358979323;

long long int N,W,H;
string S[200];

int main()
{
    cout << fixed << setprecision(18);
    cin>>N>>H>>W;
    cout<<(N-H+1)*(N-W+1)<<endl;

}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

map<ll,int> prf(ll n){
  map<ll,int> ret;
  for(ll i=2;i*i<=n;i++){
    while(n%i==0){
     ret[i]++;
      n/=i;
    }
  }
  if(n!=1){
    ret[n]=1;
  }
  return ret;
}

int main(){cout<<fixed<<setprecision(10);
		   int n,h,w;
           cin>>n>>h>>w;
           n++;
           cout<<(n-w)*(n-h)<<endl;
}


#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

void solve(){
	int n;
	cin>>n;
	string s;
	cin>>s;
	int q;
	cin>>q;
	vector<ll>cnt_m(n+1);
	rep(i,n){
		cnt_m[i+1]=cnt_m[i]+ll(s[i]=='M');
	}
	while(q--){
		int k;
		cin>>k;
		vector<vector<ll>>dp(2,vector<ll>(4));
		dp[0][0]=1;
		rep(i,k){
			rep(j,4){
				dp[(i+1)%2][j]+=dp[i%2][j];
			}
			if(s[i]=='D'){
				dp[(i+1)%2][1]+=dp[i%2][0];
			}
			else if(s[i]=='M'){
				dp[(i+1)%2][2]+=dp[i%2][1];
			}
			else if(s[i]=='C'){
				dp[(i+1)%2][3]+=dp[i%2][2];
			}
			rep(j,4)dp[i%2][j]=0;
		}
		for(int i=k;i<n;++i){
			if(s[i-k]=='D'){
				dp[i%2][2]-=(cnt_m[i]-cnt_m[i+1-k]);
				dp[i%2][1]--;
			}
			rep(j,4){
				dp[(i+1)%2][j]+=dp[i%2][j];
			}
			if(s[i]=='D'){
				dp[(i+1)%2][1]+=dp[i%2][0];
			}
			else if(s[i]=='M'){
				dp[(i+1)%2][2]+=dp[i%2][1];
			}
			else if(s[i]=='C'){
				dp[(i+1)%2][3]+=dp[i%2][2];
			}
			rep(j,4)dp[i%2][j]=0;
		}
		cout<<dp[n%2][3]<<"\n";
	}

}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}

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

int main(){
    int h, w;
    cin >> h >> w;
    vector<vector<int>> campas(h, vector<int>(w));
    for (int i=0; i<h; i++){
        string s;
        cin >> s;
        for (int j=0; j<w; j++){
            if (s[j] == '#') campas[i][j] = 1;
            else campas[i][j] = 0;
        }
    }
    for (int i=0; i<h; i++){
        for (int j=0; j<w; j++){
            if (campas[i][j] == 0) continue;
            if ((0 <= i+1) && (i+1 < h)) {if (campas[i+1][j] > 0) campas[i][j] += 1;}
            if ((0 <= i-1) && (i-1 < h)) {if (campas[i-1][j] > 0) campas[i][j] += 1;}
            if ((0 <= j+1) && (j+1 < w)) {if (campas[i][j+1] > 0) campas[i][j] += 1;}
            if ((0 <= j-1) && (j-1 < w)) {if (campas[i][j-1] > 0) campas[i][j] += 1;}
        }
    }
    string ans="Yes";
    for (int i=0; i<h; i++){
        for (int j=0; j<w; j++){
            if (campas[i][j] == 1) ans = "No";
        }
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit
//	freopen("testcase.wdt", "r", stdin);
	// 2004/01/01 is Thursday
	const string week[] = {
	"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	const int days[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	string str = "";

	while (1){
		int month = 0;
		int day = 0;
		cin >> month >> day;
		if (month == 0 && day == 0){
			break;
		} // end if
		int num = 0;
		for (int i = 0; i < month; ++i){
			num += days[i];
		} // end for
		num += day;
		cout << week [( num - 1 + 3 ) % 7 ] << endl;
	} // end while

	return 0;
}

#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <vector>

using namespace std;

void b_power_socket(void) {
	int a, b;
	cin >> a >> b;

	int sum = 1;
	int cnt = 0;
	while (sum < b) {
		sum += (a - 1);
		cnt++;
	}

	cout << cnt << endl;
}
int main()
{
    b_power_socket();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); i++)
using ll = long long;
using P = pair<int, int>;
int main()
{
    int n;
    cin >> n;
    string m = to_string(n);
    int cnt = 0;
    rep(i,m.size()){
        cnt += m.at(i) - '0';
    }
    if(n % cnt == 0)cout << "Yes" << endl;
    else cout << "No" << endl;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
int num(int N){
int a = 0 , b = 1, c;
for(int i = 0 ; i < 9 ; i++){
a += N / b % 10;
b *= 10;
}
c = N % a;
return c;
}
int main(){
  int N;
  cin >> N;
      if(num(N) == 0){
        cout << "Yes" <<endl;
      }
      else if(num(N) != 0){
        cout << "No" << endl;
      }
}