#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200100;
const int M = 1e9+7;
map<int,int> cnts;

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
    //freopen("data.out", "w", stdout);
#endif
    int n;
    scanf("%d",&n);
    for(int i = 0,x; i < n; i++)
    {
        cin(x);
        cnts[x]++;
    }
    vector<vector<int>> val_by_cnt(n+1); //出现次数
    for(auto val_cnt : cnts)
    {
        val_by_cnt[val_cnt.second].push_back(val_cnt.first);
    }

    int gep[n+1];
    gep[n] = val_by_cnt[n].size();
    for(int i = n-1; i >= -1; i--)
    {
        gep[i] = gep[i+1] + val_by_cnt[i].size();
    }

    int tot = 0,best = 0,best_a,best_b;
    for(int a = 1; a <= n; a++)
    {
        tot += gep[a];      //一个数最多出现a次,所以有a行答案
        int b = tot/a;      //b列
        if(a <= b && best < a*b)        //求a行b列的最大值咯
        {
            best = a*b;
            best_a = a;
            best_b = b;
        }
    }

    cout<<best<<endl<<best_a<<' '<<best_b<<endl;
    vector<vector<int>> r(best_a,vector<int>(best_b));
    int x = 0, y = 0;   //开始填答案
    for(int i = n; i >= 1; i--)
    {
        for(auto val : val_by_cnt[i])
        {
            for(int j = 0; j < min(i,best_a); j++)
            {
                if(r[x][y] != 0)
                {
                    x = (x+1)%best_a;
                }
                if(r[x][y] == 0)
                {
                    r[x][y] = val;
                }
                x = (x+1)%best_a;
                y = (y+1)%best_b;
            }
        }
    }
    for(int i = 0; i < best_a; i++)
    {
        for(int j = 0; j < best_b; j++)
            cout<<r[i][j]<<' ';
        cout<<endl;
    }
    return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){

}

int n,d;
map<int, int > m;
vi v, pr, q;
vector<pii > s;

bool chk(ll r, ll c){
    if(r>c)swap(r,c);
    int x=upper_bound(all(q), r)-q.begin();
    ll sum=0;
    if(x>0) sum+=pr[x-1];
    sum+=r*(sz(q)-x);
    if(sum>=r*c) return true;
    return false;
}

void pri(int r, int c){
    int a[r][c]={0};
    ll sum=r*c, i=0, j=0;
    rep(l,sz(q))if(sum>0){
        rep(k,min(r,q[l]))if(sum>0){
            a[i][j] = s[l].snd;
            sum--;
            i=(i+1)%r, j=(j+1)%c;
            if(i==0 && j==0) i=(i+1)%r;
        }
    }
    rep(i,r){
        rep(j,c) cout<<a[i][j]<<' ';
        cout<<'\n';
    }
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    cin>>n;
    v.resize(n,0);
    rep(i,n)cin>>v[i], m[v[i]]++;
    trav(it, m) s.pb({it.snd, it.fst});
    sort(all(s));
    rep(i,sz(s)) pr.pb(s[i].fst), q.pb(s[i].fst), pr[i] += ((i==0) ? 0:pr[i-1]);

    d=sz(pr);

    ll mx=0, mr=0, mc=0;
    repA(i,1,d)if(i*d>mx){
        ll l=1, r=d;
        while(l<r){
            ll m=(l+r+1)/2;
            if(chk(i,m)) l=m;
            else r=m-1;
        }
        if(i*l>mx) mx=i*l, mr=i, mc=l;
    }
    cout<<mx<<endl;
    cout<<mr<<' '<<mc<<endl;
    pri(mr,mc);

	return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){

}

int n,d;
map<int, int > m;
vi v, pr, q;
vector<pii > s;

bool chk(ll r, ll c){
    if(r>c)swap(r,c);
    int x=upper_bound(all(q), r)-q.begin();
    ll sum=0;
    if(x>0) sum+=pr[x-1];
    sum+=r*(sz(q)-x);
    if(sum>=r*c) return true;
    return false;
}

void pri(int r, int c){
    int a[r][c]={0};
    ll sum=r*c, i=0, j=0;
    debug(q);
    debug(s);
    repD(l,sz(q)-1,0)if(sum>0){
        rep(k,min(r,q[l]))if(sum>0){
            a[i][j] = s[l].snd;
            debug(i,j,l,s[l].snd);
            sum--;
            i=(i+1)%r, j=(j+1)%c;
            if(i==0 && j==0) i=(i+1)%r;
        }
    }
    rep(i,r){
        rep(j,c) cout<<a[i][j]<<' ';
        cout<<'\n';
    }
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    cin>>n;
    v.resize(n,0);
    rep(i,n)cin>>v[i], m[v[i]]++;
    trav(it, m) s.pb({it.snd, it.fst});
    sort(all(s));
    rep(i,sz(s)) pr.pb(s[i].fst), q.pb(s[i].fst), pr[i] += ((i==0) ? 0:pr[i-1]);

    d=sz(pr);

    ll mx=0, mr=0, mc=0;
    repA(i,1,d)if(i*d>mx){
        ll l=1, r=d;
        while(l<r){
            ll m=(l+r+1)/2;
            if(chk(i,m)) l=m;
            else r=m-1;
        }
        if(i*l>mx) mx=i*l, mr=i, mc=l;
    }
    cout<<mx<<endl;
    cout<<mr<<' '<<mc<<endl;
    pri(mr,mc);

	return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){

}

int n,d;
map<int, int > m;
vi v, pr, q;
vector<pii > s;

bool chk(ll r, ll c){
    if(r>c)swap(r,c);
    int x=upper_bound(all(q), r)-q.begin();
    ll sum=0;
    if(x>0) sum+=pr[x-1];
    sum+=r*(sz(q)-x);
    if(sum>=r*c) return true;
    return false;
}

void pri(int r, int c){
    int a[r][c];
    rep(i,r)rep(j,c)a[i][j]=0;
    ll sum=r*c, i=0, j=0;
    repD(l,sz(q)-1,0)if(sum>0){
        rep(k,min(r,q[l]))if(sum>0){
            a[i][j] = s[l].snd;
            sum--;
            i=(i+1)%r, j=(j+1)%c;
            if(a[i][j]!=0) i=(i+1)%r;
        }
    }
    rep(i,r){
        rep(j,c) cout<<a[i][j]<<' ';
        cout<<'\n';
    }
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    cin>>n;
    v.resize(n,0);
    rep(i,n)cin>>v[i], m[v[i]]++;
    trav(it, m) s.pb({it.snd, it.fst});
    sort(all(s));
    rep(i,sz(s)) pr.pb(s[i].fst), q.pb(s[i].fst), pr[i] += ((i==0) ? 0:pr[i-1]);

    d=sz(pr);

    ll mx=0, mr=0, mc=0;
    repA(i,1,d)if(i*d>mx){
        ll l=1, r=d;
        while(l<r){
            ll m=(l+r+1)/2;
            if(chk(i,m)) l=m;
            else r=m-1;
        }
        if(i*l>mx) mx=i*l, mr=i, mc=l;
    }
    cout<<mx<<endl;
    cout<<mr<<' '<<mc<<endl;
    pri(mr,mc);

	return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){

}

int n,d;
map<int, int > m;
vi v, pr, q;
vector<pii > s;

bool chk(ll r, ll c){
    if(r>c)swap(r,c);
    int x=upper_bound(all(q), r)-q.begin();
    ll sum=0;
    if(x>0) sum+=pr[x-1];
    sum+=r*(sz(q)-x);
    if(sum>=r*c) return true;
    return false;
}

void pri(int r, int c){
    int a[r][c];
    ll sum=r*c, i=0, j=0;
    rep(l,sz(q))if(sum>0){
        rep(k,min(r,q[l]))if(sum>0){
            a[i][j] = s[l].snd;
            sum--;
            i=(i+1)%r, j=(j+1)%c;
        }
    }
    rep(i,r){
        rep(j,c) cout<<a[i][j]<<' ';
        cout<<'\n';
    }
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    cin>>n;
    v.resize(n,0);
    rep(i,n)cin>>v[i], m[v[i]]++;
    trav(it, m) s.pb({it.snd, it.fst});
    sort(all(s));
    rep(i,sz(s)) pr.pb(s[i].fst), q.pb(s[i].fst), pr[i] += ((i==0) ? 0:pr[i-1]);

    d=sz(pr);

    ll mx=0, mr=0, mc=0;
    repA(i,1,d)if(i*d>mx){
        ll l=1, r=d;
        while(l<r){
            ll m=(l+r+1)/2;
            if(chk(i,m)) l=m;
            else r=m-1;
        }
        if(i*l>mx) mx=i*l, mr=i, mc=l;
    }
    cout<<mx<<endl;
    cout<<mr<<' '<<mc<<endl;
    pri(mr,mc);

	return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){

}

void solve(){

}

int n,d;
map<int, int > m;
vi v, pr, q;
vector<pii > s;

bool chk(ll r, ll c){
    if(r>c)swap(r,c);
    int x=upper_bound(all(q), r)-q.begin();
    ll sum=0;
    if(x>0) sum+=pr[x-1];
    sum+=r*(sz(q)-x);
    if(sum>=r*c) return true;
    return false;
}

void pri(int r, int c){
    int a[r][c];
    ll sum=r*c, i=0, j=0;
    rep(l,sz(q))if(sum>0){
        rep(k,min(r,q[l]))if(sum>0){
            a[i][j] = s[l].snd;
            sum--;
            i=(i+1)%r, j=(j+1)%c;
        }
    }
    rep(i,r){
        rep(j,c) cout<<a[i][j]<<' ';
        cout<<'\n';
    }
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    cin>>n;
    v.resize(n,0);
    rep(i,n)cin>>v[i], m[v[i]]++;
    trav(it, m) s.pb({it.snd, it.fst});
    sort(all(s));
    rep(i,sz(s)) pr.pb(s[i].fst), q.pb(s[i].fst), pr[i] += ((i==0) ? 0:pr[i-1]);

    d=sz(pr);

    ll mx=0, mr=0, mc=0;
    repA(i,1,d)if(i*d>mx){
        ll l=1, r=d;
        while(l<r){
            ll m=(l+r+1)/2;
            if(chk(i,m)) l=m;
            else r=m-1;
        }
        if(i*l>mx) mx=i*l, mr=i, mc=l;
    }
    cout<<mr<<' '<<mc<<endl;
    pri(mr,mc);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define isws std::ios::sync_with_stdio(false)
#define _USE_MATH_DEFINES
#define ll long long
#define ull unsigned long long

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <math.h>
#include <cmath>
#include <string>
#include <bitset>

using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mt make_tuple
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
const ll mod = 1000000007;

const int N = 200000 + 7;

map<int, vector<int>> lul;

int main()
{
	isws;
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	vector<int> arr;
	map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		mp[x]++;
	}
	for (auto x : mp) {
		lul[x.second].push_back(x.first);
		arr.push_back(x.second);
	}
	sort(arr.begin(), arr.end());
	//reverse(arr.begin(), arr.end());
	vector<int> cum(arr.size() + 1);
	cum[0] = 0;
	for (int i = 0; i < arr.size(); i++) {
		cum[i + 1] = cum[i] + arr[i];
	}
	int ans = -1;
	int ac = 0, ar = 0;
	for (int c = 1; c <= n; c++) {
		int mr = n / c;
		//if (mr < c) break;
		int lo = 1, hi = mr;
		int idx1 = upper_bound(arr.begin(), arr.end(), c) - arr.begin();
		//cout << c << " xc " << idx1 << "\n";
		int rem = arr.size() - idx1;
		while (lo <= hi) {
			int r = (lo + hi) / 2;
			if (rem * c + cum[idx1] >= r * c) {
				if (r * c > ans) {
					ans = r * c;
					ac = c;
					ar = r;
				}
				lo = r + 1;
			}
			else {
				hi = r - 1;
			}
		}
	}
	cout << ans << "\n";
	cout << ar << " " << ac << "\n";
	int cx = 0, cy = 0;
	vector<vector<int>> fans(ar);
	for (int i = 0; i < ar; i++) fans[i].resize(ac);
	for (int i = arr.size() - 1; i >= 0; i--) {
		int csize = arr[i];
		int yy = lul[csize].back();
		lul[csize].pop_back();
		for (int j = 0; j < min(csize, ac); j++) {
			fans[cx][cy] = yy;
			cx = (cx + 1) % ar;
			cy = (cy + 1) % ac;
		}
	}
	for (int i = 0; i < ar; i++) {
		for (int j = 0; j < ac; j++) {
			cout << fans[i][j] << " ";
		}
		cout << "\n";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define isws std::ios::sync_with_stdio(false)
#define _USE_MATH_DEFINES
#define ll long long
#define ull unsigned long long

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <math.h>
#include <cmath>
#include <string>
#include <bitset>

using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mt make_tuple
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
const ll mod = 1000000007;

const int N = 200000 + 7;

map<int, vector<int>> lul;

int main()
{
	isws;
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	vector<int> arr;
	map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		mp[x]++;
	}
	for (auto x : mp) {
		lul[x.second].push_back(x.first);
		arr.push_back(x.second);
	}
	sort(arr.begin(), arr.end());
	//reverse(arr.begin(), arr.end());
	vector<int> cum(arr.size() + 1);
	cum[0] = 0;
	for (int i = 0; i < arr.size(); i++) {
		cum[i + 1] = cum[i] + arr[i];
	}
	int ans = -1;
	int ac = 0, ar = 0;
	for (int c = 1; c <= n; c++) {
		int mr = n / c;
		if (mr < c) break;
		int lo = c, hi = mr;
		int idx1 = upper_bound(arr.begin(), arr.end(), c) - arr.begin();
		//cout << c << " xc " << idx1 << "\n";
		int rem = arr.size() - idx1;
		while (lo <= hi) {
			int r = (lo + hi) / 2;
			if (rem * c + cum[idx1] >= r * c) {
				if (r * c > ans) {
					ans = r * c;
					ac = c;
					ar = r;
				}
				lo = r + 1;
			}
			else {
				hi = r - 1;
			}
		}
	}
	cout << ans << "\n";
	cout << ar << " " << ac << "\n";
	int cx = 0, cy = 0;
	int lcx = 0;
	vector<vector<int>> fans(ar);
	bool stop = false;
	for (int i = 0; i < ar; i++) fans[i].resize(ac);
	for (int i = arr.size() - 1; i >= 0; i--) {
		int csize = arr[i];
		int yy = lul[csize].back();
		lul[csize].pop_back();
		for (int j = 0; j < min(csize, ac); j++) {
			fans[cx][cy] = yy;
			cx++;
			cx %= ar;
			cy++;
			if (cy == ac) {
				cx = lcx + 1;
				lcx = cx;
				cy = 0;
				if (cx == ar) {
					stop = true;
					break;
				}
			}
		}
		if (stop) break;
	}
	for (int i = 0; i < ar; i++) {
		for (int j = 0; j < ac; j++) {
			cout << fans[i][j] << " ";
		}
		cout << "\n";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define isws std::ios::sync_with_stdio(false)
#define _USE_MATH_DEFINES
#define ll long long
#define ull unsigned long long

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <math.h>
#include <cmath>
#include <string>
#include <bitset>

using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mt make_tuple
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef pair<int, int> PII;
const ll mod = 1000000007;

const int N = 200000 + 7;

map<int, vector<int>> lul;

int main()
{
	isws;
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	vector<int> arr;
	map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		mp[x]++;
	}
	for (auto x : mp) {
		lul[x.second].push_back(x.first);
		arr.push_back(x.second);
	}
	sort(arr.begin(), arr.end());
	//reverse(arr.begin(), arr.end());
	vector<int> cum(arr.size() + 1);
	cum[0] = 0;
	for (int i = 0; i < arr.size(); i++) {
		cum[i + 1] = cum[i] + arr[i];
	}
	int ans = -1;
	int ac = 0, ar = 0;
	for (int c = 1; c <= n; c++) {
		int mr = n / c;
		if (mr < c) break;
		int lo = c, hi = mr;
		int idx1 = upper_bound(arr.begin(), arr.end(), c) - arr.begin();
		//cout << c << " xc " << idx1 << "\n";
		int rem = arr.size() - idx1;
		while (lo <= hi) {
			int r = (lo + hi) / 2;
			if (rem * c + cum[idx1] >= r * c) {
				if (r * c > ans) {
					ans = r * c;
					ac = c;
					ar = r;
				}
				lo = r + 1;
			}
			else {
				hi = r - 1;
			}
		}
	}
	cout << ans << "\n";
	cout << ar << " " << ac << "\n";
	int cx = 0, cy = 0;
	vector<vector<int>> fans(ar);
	for (int i = 0; i < ar; i++) fans[i].resize(ac);
	for (int i = arr.size() - 1; i >= 0; i--) {
		int csize = arr[i];
		int yy = lul[csize].back();
		lul[csize].pop_back();
		for (int j = 0; j < min(csize, ac); j++) {
			fans[cx][cy] = yy;
			cx = (cx + 1) % ar;
			cy = (cy + 1) % ac;
		}
	}
	for (int i = 0; i < ar; i++) {
		for (int j = 0; j < ac; j++) {
			cout << fans[i][j] << " ";
		}
		cout << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif

typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second
#define ii pair<int,int>
template <typename G>
struct triple{G F, S, T;
    triple(G x, G y, G z) : F(x), S(y), T(z){}
    triple() : F(0), S(0), T(0){}
};
#define iii triple<int>

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

#ifdef Adrian
    //freopen("test.txt", "r", stdin);
#endif

    int n;
    cin>>n;
    vector<int>v(n), w;
    for(auto &i:v)
    {
        cin>>i;
        w.push_back(i);
    }

    sort(w.begin(), w.end());
    w.resize(unique(w.begin(), w.end()) - w.begin());
    int ind = w.size();
    map<int,int> mp;
    for(int i=0; i<ind; i++)
        mp[w[i]] = i;

    vector<int> cant(ind);
    for(auto &i:v)
        ++cant[mp[i]];

    vector<ii> temp;
    for(int i=0; i<ind; i++)
        temp.push_back({cant[i], w[i]});

    auto val = [&](int r, int c)
    {
        int x = 0;
        for(auto &i:temp)
            x += min(i.F, min(r, c));
        return x >= (ll)r * c;
    };

    sort(temp.begin(), temp.end());
    int ansn = 0, ansm = 0;
    for(int i=1; i<=n; i++)
    {
        int p = 0, l = 1, r = n / i;
        while(l <= r)
        {
            int mid = (l + r) / 2;
            if(val(i, mid))
            {
                p = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        if(ansn * ansm < i * p)
        {
            ansn = i;
            ansm = p;
        }
    }

    vector<int> integ;
    for(auto &i:temp)
        for(int j=0; j<min(i.F, min(ansn, ansm)); j++)
            integ.push_back(i.S);
    vector<vector<int>> ans(ansn, vector<int>(ansm));
    int pt = 0;
    for(int c=0; c<ansm; c++)
    {
        int r = 0;
        for(int i=0; i<ansn; i++)
            ans[(r + i) % ansn][(c + i) % ansm] = integ[pt++];
    }

    cout<<ansn * ansm<<'\n';
    cout<<ansn<<" "<<ansm<<'\n';
    for(int i=0; i<ansn; i++)
        for(int j=0; j<ansm; j++)
            cout<<ans[i][j]<<" \n"[j == ansm - 1];

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif

typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second
#define ii pair<int,int>
template <typename G>
struct triple{G F, S, T;
    triple(G x, G y, G z) : F(x), S(y), T(z){}
    triple() : F(0), S(0), T(0){}
};
#define iii triple<int>

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

#ifdef Adrian
    //freopen("test.txt", "r", stdin);
#endif

    int n;
    cin>>n;
    vector<int>v(n), w;
    for(auto &i:v)
    {
        cin>>i;
        w.push_back(i);
    }

    sort(w.begin(), w.end());
    w.resize(unique(w.begin(), w.end()) - w.begin());
    int ind = w.size();
    map<int,int> mp;
    for(int i=0; i<ind; i++)
        mp[w[i]] = i;

    vector<int> cant(ind);
    for(auto &i:v)
        ++cant[mp[i]];

    vector<ii> temp;
    for(int i=0; i<ind; i++)
        temp.push_back({cant[i], w[i]});

    auto val = [&](int r, int c)
    {
        int x = 0;
        for(auto &i:temp)
            x += min(i.F, min(r, c));
        return x >= (ll)r * c;
    };

    sort(temp.rbegin(), temp.rend());
    int ansn = 0, ansm = 0;
    for(int i=1; i<=n; i++)
    {
        int p = 0, l = 1, r = n / i;
        while(l <= r)
        {
            int mid = (l + r) / 2;
            if(val(i, mid))
            {
                p = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        if(ansn * ansm < i * p)
        {
            ansn = i;
            ansm = p;
        }
    }

    vector<int> integ;
    for(auto &i:temp)
        for(int j=0; j<min(i.F, min(ansn, ansm)); j++)
            integ.push_back(i.S);
    vector<vector<int>> ans(ansn, vector<int>(ansm));
    int pt = 0;
    for(int c=0; c<ansm; c++)
    {
        int r = 0;
        for(int i=0; i<ansn; i++)
            ans[(r + i) % ansn][(c + i) % ansm] = integ[pt++];
    }

    cout<<ansn * ansm<<'\n';
    cout<<ansn<<" "<<ansm<<'\n';
    for(int i=0; i<ansn; i++)
        for(int j=0; j<ansm; j++)
            cout<<ans[i][j]<<" \n"[j == ansm - 1];

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif

typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
#define F first
#define S second
#define ii pair<int,int>
template <typename G>
struct triple{G F, S, T;
    triple(G x, G y, G z) : F(x), S(y), T(z){}
    triple() : F(0), S(0), T(0){}
};
#define iii triple<int>

struct BIT
{
    vector<int>v;
    BIT(int n) : v(n + 10){}
    void upd(int p, int val)
    {
        for(; p<v.size(); p += (p & (-p)))
            v[p] += val;
    }
    int query(int p)
    {
        int x = 0;
        for(; p>0; p-=(p & (-p)))
            x += v[p];
        return x;
    }
};

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

#ifdef Adrian
    //freopen("test.txt", "r", stdin);
#endif

    int n;
    cin>>n;
    vector<int>v(n), w;
    for(auto &i:v)
    {
        cin>>i;
        w.push_back(i);
    }

    sort(w.begin(), w.end());
    w.resize(unique(w.begin(), w.end()) - w.begin());
    int ind = w.size();
    map<int,int> mp;
    for(int i=0; i<ind; i++)
        mp[w[i]] = i;

    vector<int> cant(ind);
    for(auto &i:v)
        ++cant[mp[i]];

    vector<ii> temp;
    for(int i=0; i<ind; i++)
        temp.push_back({cant[i], w[i]});

    sort(temp.rbegin(), temp.rend());
    BIT abi(n);
    for(auto &i:temp)
    {
        abi.upd(1, 1);
        abi.upd(i.F + 1, -1);
    }
    vector<ll>val(n + 1);
    for(int i=1; i<=n; i++)
        val[i] = val[i - 1] + abi.query(i);

    int ansn = 0, ansm = 0;
    for(int i=1; i<=n; i++)
    {
        int p = 0, l = 1, r = n / i;
        while(l <= r)
        {
            int mid = (l + r) / 2;
            if(val[min(i, mid)] >= i * mid)
            {
                p = mid;
                l = mid + 1;
            } else
                r = mid - 1;
        }
        if(ansn * ansm < i * p)
        {
            ansn = i;
            ansm = p;
        }
    }

    vector<int> integ;
    for(auto &i:temp)
        for(int j=0; j<min(i.F, min(ansn, ansm)); j++)
            integ.push_back(i.S);
    vector<vector<int>> ans(ansn, vector<int>(ansm));
    int pt = 0;
    for(int c=0; c<ansm; c++)
    {
        int r = 0;
        for(int i=0; i<ansn; i++)
            ans[(r + i) % ansn][(c + i) % ansm] = integ[pt++];
    }

    cout<<ansn * ansm<<'\n';
    cout<<ansn<<" "<<ansm<<'\n';
    for(int i=0; i<ansn; i++)
        for(int j=0; j<ansm; j++)
            cout<<ans[i][j]<<" \n"[j == ansm - 1];

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX=5e5+5;
#define pb push_back
#define se second
#define fi first
#define sb __builtin_popcount
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fbo find_by_order
#define ook order_of_key
#define up upper_bound
#define lb lower_bound
#define pq priority_queue
#define pll pair<ll,ll>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll cum[MAX];
vector<pll>v;
int main()
{
	ll n,a;
	cin>>n;
	map<ll,ll>mp;
	for(ll i=0;i<n;i++){
		cin>>a;
		mp[a]++;
	}
	for(auto  u:mp){
		cum[1]++;
		cum[u.se+1]--;
	}
	ll row,col;
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	ll total=0;
	for(ll row1=5e3;row1>0;row1--){
		if(row1*row1<=cum[row1])
			if(total<row1*(cum[row1]/row1)){
				row=row1;
				total=row1*(cum[row1]/row1);
			}
	}
	for(pll u:mp){
		v.pb({min(row,u.se),u.fi});
	}
	col=total/row;
	ll mat[row][col];
	ll curr=0;
	sort(v.begin(),v.end(),greater<pll>());
	for(ll i=0;i<col;i++){
		ll j=0,cnt=0,i1=i;
		while(cnt<row){
			cnt++;
			if(!v[curr].fi)curr++;
			mat[j][i1%col]=v[curr].se;
			v[curr].fi--;
			j++;
			i1++;
		}
	}
	cout<<row*col<<'\n';
	cout<<row<<' '<<col<<'\n';
	for(ll i=0;i<row;i++){
		for(ll j=0;j<col;j++)
			cout<<mat[i][j]<<' ';
		cout<<'\n';
	}

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX=5e5+5;
#define pb push_back
#define se second
#define fi first
#define sb __builtin_popcount
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fbo find_by_order
#define ook order_of_key
#define up upper_bound
#define lb lower_bound
#define pq priority_queue
#define pll pair<ll,ll>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll cum[MAX];
vector<pll>v;
int main()
{
	ll n,a;
	cin>>n;
	map<ll,ll>mp;
	for(ll i=0;i<n;i++){
		cin>>a;
		mp[a]++;
	}
	for(auto  u:mp){
		cum[1]++;
		cum[u.se+1]--;
	}
	ll row,col;
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(row=5e3;row>=0;row--)
		if(row*row<=cum[row])
			break;
		ll total=0;
	for(pll u:mp){
		v.pb({min(row,u.se),u.fi});
		total+=min(row,u.se);
	}
	col=total/row;
	ll mat[row][col];
	ll curr=0;
	sort(v.begin(),v.end(),greater<pll>());
	for(ll i=0;i<col;i++){
		ll j=0,cnt=0,i1=i;
		while(cnt<row){
			cnt++;
			if(!v[curr].fi)curr++;
			mat[j][i1%col]=v[curr].se;
			v[curr].fi--;
			j++;
			i1++;
		}
	}
	cout<<row*col<<'\n';
	cout<<row<<' '<<col<<'\n';
	for(ll i=0;i<row;i++){
		for(ll j=0;j<col;j++)
			cout<<mat[i][j]<<' ';
		cout<<'\n';
	}

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX=5e5+5;
#define pb push_back
#define se second
#define fi first
#define sb __builtin_popcount
#define maxe max_element
#define mine min_element
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fbo find_by_order
#define ook order_of_key
#define up upper_bound
#define lb lower_bound
#define pq priority_queue
#define pll pair<ll,ll>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll cum[MAX];
vector<pll>v;
int main()
{
	ll n,a;
	cin>>n;
	map<ll,ll>mp;
	for(ll i=0;i<n;i++){
		cin>>a;
		mp[a]++;
	}
	for(auto  u:mp){
		cum[1]++;
		cum[u.se+1]--;
	}
	ll row,col;
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(ll i=1;i<MAX;i++)cum[i]+=cum[i-1];
	//for(ll i=0;i<5;i++)debug(cum[i]);
	for(row=5e3;row>=0;row--)
		if(row*row<=cum[row])
			break;
		ll total=0;
	for(pll u:mp){
		v.pb({min(row,u.se),u.fi});
		total+=min(row,u.se);
	}
	col=total/row;
	ll mat[row][col];
	ll curr=0;
	sort(v.begin(),v.end(),greater<pll>());
	for(ll i=0;i<col;i++){
		ll j=0,cnt=0,i1=i;
		while(cnt<row){
			cnt++;
			if(!v[curr].fi)curr++;
			mat[j][i1%col]=v[curr].se;
			v[curr].fi--;
			j++;
			i1++;
		}
	}
	cout<<row<<' '<<col<<'\n';
	for(ll i=0;i<row;i++){
		for(ll j=0;j<col;j++)
			cout<<mat[i][j]<<' ';
		cout<<'\n';
	}

}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (lli)(x).size()

using lli   = long long;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vll   = vector<pll>;
using vl    = vector<lli>;
using vi    = vector<int>;
using vvi   = vector<vector<int>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T>
void __p(std::deque<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1); cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
#else
#define endl '\n'
#define trace(...)
#endif

#define MAF 400010
lli tre[MAF], other[MAF];
lli get(int x) { return tre[x+1];}
void add(int l, int r) {
    tre[l+1]+=1; tre[r+2]-=1;
}
void upd()
{
    fol(i,1,MAF) tre[i]+=tre[i-1];
    fol(i,1,MAF) tre[i]+=tre[i-1];
}

void rotat(int arr[], int n, int a)
{
    fol(i,0,n) other[(i+a)%n]=arr[i];
    fol(i,0,n) arr[i]=other[i];
}

void tejas_919(int kkkk)
{
    lli n, m, k, q, a, b, u, v, temp=0, ans=0;
    cin>>n;
    vi arr;
    deque<int> vect;
    fol(i,0,n) cin>>u,arr.pb(u);
    sort(all(arr));
    trace(arr)
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        add(1,j-i);
        i=j-1;
    }
    trace(tre,n+4)
    upd();
    trace(tre,n+4)
    fol(i,1,n+1)
    {
        temp=get(i);
        temp/=i;
        if(temp<i) break;
        trace(i,temp)
        if((temp*i)>ans) { ans=temp*i; u=i; v=temp; }
    }
    cout<<ans<<endl<<u<<" "<<v<<endl;
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        fol(k,0,min((lli)j-i,get(u))) vect.pb(arr[i]);
        i=j-1;
    }
    int mat[u+1][v+1];
    fol(j,0,v) fol(i,0,u) {mat[i][j]=vect.front(); vect.ppf;}
    fol(i,1,u) rotat(mat[i],v,i);
    fol(i,0,u) {fol(j,0,v) cout<<mat[i][j]<<" "; cout<<endl;}
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (lli)(x).size()

using lli   = long long;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vll   = vector<pll>;
using vl    = vector<lli>;
using vi    = vector<int>;
using vvi   = vector<vector<int>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T>
void __p(std::deque<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1); cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
#else
#define endl '\n'
#define trace(...)
#endif

#define MAF 400010
lli tre[MAF], other[MAF];
lli get(int x) { return tre[x+1];}
void add(int l, int r) {
    tre[l+1]+=1; tre[r+2]-=1;
}
void upd()
{
    fol(i,1,MAF) tre[i]+=tre[i-1];
    fol(i,1,MAF) tre[i]+=tre[i-1];
}

void rotat(int arr[], int n, int a)
{
    fol(i,0,n) other[(i+a)%n]=arr[i];
    fol(i,0,n) arr[i]=other[i];
}

void tejas_919(int kkkk)
{
    lli n, m, k, q, a, b, u, v, temp=0, ans=0;
    cin>>n;
    int arr[n+5]={};
    fol(i,0,n) cin>>arr[i];
    sort(arr,arr+n);
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        add(1,j-i);
        i=j-1;
    }
    upd();
    trace(tre,n+5)
    fol(i,1,n+1)
    {
        temp=get(i);
        temp/=i;
        trace(i, temp)
        if(temp<i) break;
        if((temp*i)>ans) { ans=temp*i; u=i; v=temp; }
    }
    cout<<ans<<endl<<u<<" "<<v<<endl;
    deque<int> de;
    vll vect;
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        vect.pb({j-i,arr[i]});
        i=j-1;
    }
    sort(all(vect),greater<pll>());
    forr(x,vect) fol(i,0,min(x.ff,u)) de.pb(x.ss);
    int mat[u+1][v+1];
    fol(j,0,v) fol(i,0,u) {mat[i][j]=de.front(); de.ppf;}
    fol(i,1,u) rotat(mat[i],v,i);
    fol(i,0,u) {fol(j,0,v) cout<<mat[i][j]<<" "; cout<<endl;}
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (lli)(x).size()

using lli   = long long;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vll   = vector<pll>;
using vl    = vector<lli>;
using vi    = vector<int>;
using vvi   = vector<vector<int>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T>
void __p(std::deque<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1); cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
#else
#define endl '\n'
#define trace(...)
#endif

#define MAF 400010
lli tre[MAF], other[MAF];
lli get(int x) { return tre[x+1];}
void add(int l, int r) {
    tre[l+1]+=1; tre[r+2]-=1;
}
void upd()
{
    fol(i,1,MAF) tre[i]+=tre[i-1];
    fol(i,1,MAF) tre[i]+=tre[i-1];
}

void rotat(int arr[], int n, int a)
{
    fol(i,0,n) other[(i+a)%n]=arr[i];
    fol(i,0,n) arr[i]=other[i];
}

void tejas_919(int kkkk)
{
    lli n, m, k, q, a, b, u, v, temp=0, ans=0;
    cin>>n;
    vi arr;
    deque<int> vect;
    fol(i,0,n) cin>>u,arr.pb(u);
    sort(all(arr));
    trace(arr)
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        add(1,j-i);
        i=j-1;
    }
    upd();
    fol(i,1,n+1)
    {
        temp=get(i);
        temp/=i;
        if(temp<i) break;
        trace(i,temp)
        if((temp*i)>ans) { ans=temp*i; u=i; v=temp; }
    }
    cout<<ans<<endl<<u<<" "<<v<<endl;
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        fol(k,0,min((lli)j-i,u)) vect.pb(arr[i]);
        i=j-1;
    }
    int mat[u+1][v+1];
    fol(j,0,v) fol(i,0,u) {mat[i][j]=vect.front(); vect.ppf;}
    fol(i,1,u) rotat(mat[i],v,i);
    fol(i,0,u) {fol(j,0,v) cout<<mat[i][j]<<" "; cout<<endl;}
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define uid uniform_int_distribution
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpop(x) __builtin_popcountll((ulli)x)
#define blead(x) __builtin_clzll((ulli)x)
#define btrail(x) __builtin_ctzll((ulli)x)
const double PI = acos(-1);
template<class my>
using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define loop(i,a,b) for(int i=a;i>=b;i--)
#define forr(x,arr) for(auto& x:arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) (lli)(x).size()

using lli   = long long;
using ulli  = unsigned long long int;
using pll   = pair<lli, lli>;
using ttt   = pair<lli, pll>;
using vttt  = vector<ttt>;
using vll   = vector<pll>;
using vl    = vector<lli>;
using vi    = vector<int>;
using vvi   = vector<vector<int>>;
using cd    = complex<long double>;

#ifdef tejasp
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T>
void __p(std::deque<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1); cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
template<typename Arg1, typename Arg2>
void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){
    int i=0;
	for(; ;i++) if(names[i]==',') break;
	cout.write(names,i)<<" : { ";
	fol(i,0,arg2) __p(arg1[i]);
	cout << "} "<<endl;
}
#define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); }
#else
#define endl '\n'
#define trace(...)
#endif

#define MAF 400010
lli tre[MAF], other[MAF];
lli get(int x) { return tre[x+1];}
void add(int l, int r) {
    tre[l+1]+=1; tre[r+2]-=1;
}
void upd()
{
    fol(i,1,MAF) tre[i]+=tre[i-1];
    fol(i,1,MAF) tre[i]+=tre[i-1];
}

void rotat(int arr[], int n, int a)
{
    fol(i,0,n) other[(i+a)%n]=arr[i];
    fol(i,0,n) arr[i]=other[i];
}

void tejas_919(int kkkk)
{
    lli n, m, k, q, a, b, u, v, temp=0, ans=0;
    cin>>n;
    vi arr;
    deque<int> vect;
    fol(i,0,n) cin>>u,arr.pb(u);
    sort(all(arr));
    trace(arr)
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        add(1,j-i);
        i=j-1;
    }
    upd();
    fol(i,1,n+1)
    {
        temp=get(i);
        temp/=i;
        if(temp<i) break;
        trace(i,temp)
        if((temp*i)>ans) { ans=temp*i; u=i; v=temp; }
    }
    cout<<ans<<endl<<u<<" "<<v<<endl;
    fol(i,0,n)
    {
        int j=i;
        while(j<n&&arr[j]==arr[i]) j++;
        fol(k,0,min((lli)j-i,v)) vect.pb(arr[i]);
        i=j-1;
    }
    int mat[u+1][v+1];
    fol(j,0,v) fol(i,0,u) {mat[i][j]=vect.front(); vect.ppf;}
    fol(i,1,u) rotat(mat[i],v,i);
    fol(i,0,u) {fol(j,0,v) cout<<mat[i][j]<<" "; cout<<endl;}
}

int main()
{
    #ifndef tejasp
        ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #endif
    cout << fixed << setprecision(10);
    int t=1;
    //cin>>t;
    fol(i,0,t) { tejas_919(i+1); }
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back

using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;

int a[N];
vector <pair <int, int> > v;
int saved = 0;

int func(int w, int n) {
      int l = n / w, total = 0, res = 0;
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            total += min(amount, w);
      }
      l = total / w;
      if (l < w) swap(l, w);
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            res += min(amount, w);
      }
      l = res / w;
      saved = w;
      res = l * w;
      return res;
}

void print(int ind, int n) {
      int res = func(ind, n);
      cout << res << endl;
      int w = saved, l = res / saved;
      cout << w << " " << l << endl;
      for (int i = 0; i < v.size(); i ++) {
            v[i].fr = min(v[i].fr, w);
      }
      vector <int> an[w];
      for (int i = 0; i < w; i ++) an[i].resize(l);
      int pos = 0;

      int i = 0, j = 0;
      int cnt = v[pos].fr, num = v[pos].sc;
      while (res > 0) {
            an[i][j] = num;
            cnt --;
            if (cnt == 0) {
                  cnt = v[++ pos].fr;
                  num = v[pos].sc;
            }
            if (j + 1 == l && w == l) i ++;
            i ++, j ++;
            i %= w, j %= l;
            res --;
      }
      for (int i = 0; i < w; i ++) {
            for (int j = 0; j < l; j ++)
                  cout << an[i][j] << " ";
            cout << endl;
      }
}

int main() {
      int n;
      cin >> n;
      for (int i = 1; i <= n; i ++) {
            cin >> a[i];
      }
      sort(a + 1, a + n + 1);
      for (int i = 1; i <= n; i ++) {
            int j = 0;
            while (a[i] == a[i + j]) j ++;
            v.pb(mk(j, a[i]));
            i = i + j - 1;
      }
      sort(v.begin(), v.end());
      int ans = v.size(), ind = 1;
      for (int i = 2; i <= sqrt(n + 0.0); i ++) {
            int cnt = func(i, n);
            if (cnt > ans) {
                  ans = cnt;
                  ind = i;
            }
      }
      print(ind, n);
      return 0;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back

using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;

int a[N];
vector <pair <int, int> > v;
int saved = 0;

int func(int w, int n) {
      int l = n / w, total = 0, res = 0;
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            total += min(amount, w);
      }
      l = total / w;
      if (l < w) swap(l, w);
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            res += min(amount, w);
      }
      l = res / w;
      saved = w;
      res = l * w;
      return res;
}

void print(int ind, int n) {
      int res = func(ind, n);
      cout << res << endl;
      int w = saved, l = res / saved;
      cout << w << " " << l << endl;
      for (int i = 0; i < v.size(); i ++) {
            v[i].fr = min(v[i].fr, w);
      }
      vector <int> an[w];
      for (int i = 0; i < w; i ++) an[i].resize(l);
      int pos = v.size() - 1;
      int i = 0, j = 0;
      int cnt = v[pos].fr, num = v[pos].sc;
      while (res > 0) {
            //cout << i << " " << j << " " << num << " " << cnt << endl;
            an[i][j] = num;
            cnt --;
            if (cnt == 0) {
                  cnt = v[-- pos].fr;
                  num = v[pos].sc;
            }
            if (j + 1 == l && w == l) i ++;
            i ++, j ++;
            i %= w, j %= l;
            res --;
      }
      for (int i = 0; i < w; i ++) {
            for (int j = 0; j < l; j ++)
                  cout << an[i][j] << " ";
            cout << endl;
      }
}

int main() {
      int n;
      cin >> n;
      for (int i = 1; i <= n; i ++) {
            cin >> a[i];
      }
      sort(a + 1, a + n + 1);
      for (int i = 1; i <= n; i ++) {
            int j = 0;
            while (a[i] == a[i + j]) j ++;
            v.pb(mk(j, a[i]));
            i = i + j - 1;
      }
      sort(v.begin(), v.end());
      int ans = v.size(), ind = 1;
      for (int i = 2; i <= sqrt(n + 0.0); i ++) {
            int cnt = func(i, n);
            if (cnt > ans) {
                  ans = cnt;
                  ind = i;
            }
      }
      print(ind, n);
      return 0;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back

using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;

int a[N];
vector <pair <int, int> > v;
int saved = 0;

int func(int w, int n) {
      int l = n / w, total = 0, res = 0;
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            total += min(amount, w);
      }
      l = total / w;
      if (l < w) swap(l, w);
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            res += min(amount, w);
      }
      l = res / w;
      saved = w;
      res = l * w;
      return res;
}

void print(int ind, int n) {
      int res = func(ind, n);
      cout << res << endl;
      int w = saved, l = res / saved;
      cout << w << " " << l << endl;
      for (int i = 0; i < v.size(); i ++) {
            v[i].fr = min(v[i].fr, w);
      }
      vector <int> an[w];
      for (int i = 0; i < w; i ++) an[i].resize(l);
      int pos = v.size() - 1;
      int i = 0, j = 0;
      int cnt = v[pos].fr, num = v[pos].sc;
      while (res > 0) {
            //cout << i << " " << j << " " << num << " " << cnt << endl;
            an[i][j] = num;
            cnt --;
            if (cnt == 0) {
                  cnt = v[-- pos].fr;
                  num = v[pos].sc;
            }
            i ++, j ++;
            i %= w, j %= l;
            res --;
            while (res != 0 && an[i][j] > 0) i ++, i %= w;
      }
      for (int i = 0; i < w; i ++) {
            for (int j = 0; j < l; j ++)
                  cout << an[i][j] << " ";
            cout << endl;
      }
}

int main() {
      int n;
      cin >> n;
      for (int i = 1; i <= n; i ++) {
            cin >> a[i];
      }
      sort(a + 1, a + n + 1);
      for (int i = 1; i <= n; i ++) {
            int j = 0;
            while (a[i] == a[i + j]) j ++;
            v.pb(mk(j, a[i]));
            i = i + j - 1;
      }
      sort(v.begin(), v.end());
      int ans = v.size(), ind = 1;
      for (int i = 2; i <= sqrt(n + 0.0); i ++) {
            int cnt = func(i, n);
            if (cnt > ans) {
                  ans = cnt;
                  ind = i;
            }
      }
      print(ind, n);
      return 0;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back

using namespace std;
typedef long long ll;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;

int a[N];
vector <pair <int, int> > v;
int saved = 0;

int func(int w, int n) {
      int l = n / w, total = 0, res = 0;
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            total += min(amount, w);
      }
      l = total / w;
      if (l < w) swap(l, w);
      for (int i = 0; i < v.size(); i ++) {
            int amount = v[i].fr;
            res += min(amount, w);
      }
      l = res / w;
      saved = w;
      res = l * w;
      return res;
}

void print(int ind, int n) {
      int res = func(ind, n);
      cout << res << endl;
      int w = saved, l = res / saved;
      cout << w << " " << l << endl;
      for (int i = 0; i < v.size(); i ++) {
            v[i].fr = min(v[i].fr, w);
      }
      vector <int> an[w];
      for (int i = 0; i < w; i ++) an[i].resize(l);
      int pos = v.size() - 1;
      int i = 0, j = 0;
      int cnt = v[pos].fr, num = v[pos].sc;
      while (res > 0) {
            //cout << i << " " << j << " " << num << " " << cnt << endl;
            an[i][j] = num;
            cnt --;
            if (cnt == 0) {
                  cnt = v[-- pos].fr;
                  num = v[pos].sc;
            }
            i ++, j ++;
            i %= w, j %= l;
            res --;
            while (res != 0 && an[i][j] > 0) i ++, i %= w;
      }
      for (int i = 0; i < w; i ++) {
            for (int j = 0; j < l; j ++)
                  printf("%d ", an[i][j]);
            printf("\n");
      }
}

int main() {
      int n;
      cin >> n;
      for (int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
      }
      sort(a + 1, a + n + 1);
      for (int i = 1; i <= n; i ++) {
            int j = 0;
            while (a[i] == a[i + j]) j ++;
            v.pb(mk(j, a[i]));
            i = i + j - 1;
      }
      sort(v.begin(), v.end());
      int ans = v.size(), ind = 1;
      for (int i = 2; i <= sqrt(n + 0.0); i ++) {
            int cnt = func(i, n);
            if (cnt > ans) {
                  ans = cnt;
                  ind = i;
            }
      }
      print(ind, n);
      return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int ,int> pii;
typedef pair<ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define sll(n) scanf("%lld",&n)
#define sll2(x,y) scanf("%lld%lld",&x,&y)
#define sll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define debug(x) cerr<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define debug4(x,y,z,w) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<"\n"
ll power(ll x,ll y,ll z) {
	ll ret = 1;
	while(y > 0) {
		if(y & 1) ret = (ret*x)%z;
		x = (x*x)%z;
		y >>= 1;
	}
	return ret;
}
const ll N = 4e5 + 5;
ll A[N], dp[N];
vector<ll> V[N];
int main() {

	ll n;
	sll(n);
	unordered_map<ll,ll> has;
	REP(i,1,n) sll(A[i]), has[A[i]]++;
	for(auto u : has) V[u.S].pb(u.F);
	dp[n] = V[n].size();
	for(ll i = n - 1;i >= 1;--i) dp[i] = dp[i + 1] + V[i].size();
	ll tot = 0, bx, by, bs = 0;
	REP(i,1,n)  {
		tot += dp[i];
		ll j = tot / i;
		if(i <= j && bs < i * j) {
			bs = i * j;
			bx = i;
			by = j;
		}
	}
	printf("%lld\n%lld %lld\n",bs, bx, by);
	vector<vector<ll>> ans(bx + 1, vector<ll>(by + 1));
	ll x = 0,y = 0;
	for(ll i = n;i >= 1;--i) {
		for(auto u : V[i]) {
			for(ll j = 0;j < min(i,bx);++j) {
				if(ans[x][y]) x = (x + 1) % bx;
				if(!ans[x][y]) ans[x][y] = u;
				x = (x + 1) % bx;
				y = (y + 1) % by;
			}
		}
	}
	REP(i,0,bx - 1) {
		REP(j,0,by - 1) printf("%lld ",ans[i][j]);
		printf("\n");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					mat[x][y] = j.sc;
					x++;
					y++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i)+1;
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}

			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			std::map<int, int> np;
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					np[mat[k][l]]++;
			}
			assert(np==m);
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i)+1;
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}

			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i)+1;
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			std::map<int, int> np;
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					np[mat[k][l]]++;
			}
			assert(np==m);
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,n+1){
mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i)+1;
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}

			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i)+1;
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			rep(k,0,mxsquare[i]){

				set<int> s;

				rep(l,0,i/mxsquare[i]) s.insert(mat[k][l]);

				assert(sz(s)==i/mxsquare[i]);

			}

			rep(k,0,i/mxsquare[i]){

				set<int> s;

				rep(l,0,mxsquare[i]) s.insert(mat[l][k]);

				assert(sz(s)==mxsquare[i]);

			}

			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			mxsquare[i]=1;
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			rep(k,0,mxsquare[i]){
				set<int> s;
				rep(l,0,i/mxsquare[i]) s.insert(mat[k][l]);
				assert(sz(s)==i/mxsquare[i]);
			}
			rep(k,0,i/mxsquare[i]){
				set<int> s;
				rep(l,0,mxsquare[i]) s.insert(mat[l][k]);
				assert(sz(s)==mxsquare[i]);
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		assert(0);
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			rep(k,0,mxsquare[i]){
				set<int> s;
				rep(l,0,i/mxsquare[i]) s.insert(mat[k][l]);
				assert(sz(s)==i/mxsquare[i]);
			}
			rep(k,0,i/mxsquare[i]){
				set<int> s;
				rep(l,0,mxsquare[i]) s.insert(mat[l][k]);
				assert(sz(s)==mxsquare[i]);
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					mat[x][y] = j.sc;
					x++;
					y++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 400005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
bool prime[N];
void seive()
{
	memset(prime, true, sizeof(prime));
	prime[1] = false;
	for (int p=2; p*p<=N; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=N; i += p)
				prime[i] = false;
		}
	}
}
signed main()
{
	ios_base::sync_with_stdio(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		seive();
		int mxsquare[N];
		rep(i,1,N){
			if(prime[i]) continue;
			int sq = sqrt(i);
			for(int j=sq;; j--){
				if(i%j==0){
					mxsquare[i] = j;
					break;
				}
			}
		}
		int n;
		cin>>n;
		map<int,int> m;
		set<pair<int,int>> s;
		rep(i,0,n){
			int t;
			cin>>t;
			m[t]++;
		}
		for(auto j:m){
			s.insert({j.sc,j.fr});
		}
		for(int i=n;;i--){
			auto j = *--s.end();
			if(j.fr>mxsquare[i]){
				s.erase(--s.end());
				if(j.fr>1)
					s.insert({j.fr-1,j.sc});
				m[j.sc]--;
				if(j.fr==1) m.erase(j.sc);
				continue;
			}
			cout<<i<<endl<<mxsquare[i]<<" "<<i/mxsquare[i]<<endl;
			int mat[mxsquare[i]][i/mxsquare[i]];
			int x=0, y=0, cnt=0;
			pair<int,int> start;
			for(set<pair<int,int>>::reverse_iterator it = s.rbegin(); it!=s.rend(); it++){
				auto j = *it;
				rep(k,0,j.fr){
					// trace(x,y,j.sc);
					mat[x][y] = j.sc;
					if(cnt==0) start = {x,y};
					x++;
					y++;
					cnt++;
					if(x==mxsquare[i]) x=0;
					if(y==i/mxsquare[i]) y=0;
					if(cnt==mxsquare[i]){
						cnt = 0;
						x = start.fr;
						y = start.sc+1;
						if(y==i/mxsquare[i]) y=0;
					}
				}
			}
			rep(k,0,mxsquare[i]){
				rep(l,0,i/mxsquare[i])
					cout<<mat[k][l]<<" ";
				cout<<endl;
			}
			return 0;
		}
	}
}


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

map<int,int> mp;

int count_val(int i)
{
	int cnt=0;
	for(auto ele:mp)
	{
		cnt+=min(ele.second,i);
	}
	return cnt;
}
int main()
{
	int n;
	cin>>n;

	int temp;
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		mp[temp]++;
	}

	int ma=0;
	pair<int,int> r_c;

	for(int i=1;i<=sqrt(n);i++)
	{
		int cnt=count_val(i);

		if(i*i<=cnt)
		{
			int temp=(cnt/i)*i;
			if(temp>ma)
			{
				ma=temp;
				r_c={i,cnt/i};
			}
		}
	}

	int ans[r_c.first][r_c.second];
	memset(ans,0,sizeof(ans));

	vector<pair<int,int> > v;
	for(auto ele:mp)
	{
		v.push_back({min(ele.second,r_c.first),ele.first});
	}
	sort(v.begin(),v.end());

	int x=0,y=0;
	temp=ma;

	for(int i=0;i<v.size();i++)
	{
		while(v[i].first-- && temp--)
		{
			ans[x][y]=v[i].second;
			x++,y++;

			x%=r_c.first;
			y%=r_c.second;

			if(ans[x][y]!=0)
				x=(x+1)%r_c.first;
		}
	}

	cout<<ma<<endl;
	cout<<r_c.first<<' '<<r_c.second<<endl;

	for(int i=0;i<r_c.first;i++)
	{
		for(int j=0;j<r_c.second;j++)
			cout<<ans[i][j]<<' ';
		cout<<endl;
	}
	return 0;
}


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

map<int,int> mp;

int count_val(int i)
{
	int cnt=0;
	for(auto ele:mp)
	{
		cnt+=min(ele.second,i);
	}
	return cnt;
}
int main()
{
	int n;
	cin>>n;

	int temp;
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		mp[temp]++;
	}

	int ma=0;
	pair<int,int> r_c;

	for(int i=1;i<=sqrt(n);i++)
	{
		int cnt=count_val(i);

		if(i*i<=cnt)
		{
			int temp=(cnt/i)*i;
			if(temp>ma)
			{
				ma=temp;
				r_c={i,cnt/i};
			}
		}
	}

	int ans[r_c.first][r_c.second];
	memset(ans,0,sizeof(ans));

	vector<pair<int,int> > v;
	for(auto ele:mp)
	{
		v.push_back({min(ele.second,r_c.first),ele.first});
	}
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());

	int x=0,y=0;
	temp=ma;

	for(int i=0;i<v.size();i++)
	{
		while(v[i].first-- && temp--)
		{
			ans[x][y]=v[i].second;
			x++,y++;

			x%=r_c.first;
			y%=r_c.second;

			if(ans[x][y]!=0)
				x=(x+1)%r_c.first;
		}
	}

	cout<<ma<<endl;
	cout<<r_c.first<<' '<<r_c.second<<endl;

	for(int i=0;i<r_c.first;i++)
	{
		for(int j=0;j<r_c.second;j++)
			cout<<ans[i][j]<<' ';
		cout<<endl;
	}
	return 0;
}


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

map<int,int> mp;
vector<int> v;
vector<int> pre_sum;

int count_val(int i)
{
	int ind=lower_bound(v.begin(),v.end(),i)-v.begin();
	int n=v.size();

	if(ind==n)
	{
		return pre_sum[n-1];
	}
	else
	{
		int temp=0;
		if(ind)
			temp=pre_sum[ind-1];

		return temp + (n-ind)*i;
	}
}
int main()
{
	int n;
	cin>>n;

	int temp;
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		mp[temp]++;
	}

	for(auto ele:mp)
	{
		v.push_back(ele.second);
	}
	sort(v.begin(),v.end());
	pre_sum.push_back(v[0]);

	for(int i=1;i<v.size();i++)
	{
		pre_sum.push_back(v[i]+pre_sum[i-1]);
	}
	int ma=0;
	pair<int,int> r_c;

	for(int i=1;i<=sqrt(n);i++)
	{
		int cnt=count_val(i);

		if(i*i<=cnt)
		{
			int temp=(cnt/i)*i;
			if(temp>ma)
			{
				ma=temp;
				r_c={i,cnt/i};
			}
		}
	}

	int ans[r_c.first][r_c.second];
	memset(ans,0,sizeof(ans));

	vector<pair<int,int> > v;
	for(auto ele:mp)
	{
		v.push_back({min(ele.second,r_c.first),ele.first});
	}
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());

	int x=0,y=0;
	temp=ma;

	for(int i=0;i<v.size();i++)
	{
		while(v[i].first-- && temp--)
		{
			ans[x][y]=v[i].second;
			x++,y++;

			x%=r_c.first;
			y%=r_c.second;

			if(ans[x][y]!=0)
				x=(x+1)%r_c.first;
		}
	}

	cout<<ma<<endl;
	cout<<r_c.first<<' '<<r_c.second<<endl;

	for(int i=0;i<r_c.first;i++)
	{
		for(int j=0;j<r_c.second;j++)
			cout<<ans[i][j]<<' ';
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define f first
#define s second

vector<pair<int, int> >f;
int n, v[40002], solA, solB, ans;
map<int, int>mp;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  int poz = 0;
  for (auto it : mp) {
    f.push_back({it.s, it.f});
  }
  sort(f.begin(), f.end());
  int sum = 0;
  for (int a = 1; a <=n; a++) {
    while (poz < f.size() && f[poz].f < a) {
      sum += f[poz].f;
      poz++;
    }
    int b = (sum + a * (f.size()  - poz)) / a;
    if (a <= b && ans < a * b) {
      ans = a * b;
      solA = a;
      solB = b;
    }
  }
  vector<vector<int> >mat;
  mat.resize(solA);
  for (int i = 0; i < solA; i++)
    mat[i].resize(solB);
  int x = 0, y = 0;
  int aux = ans;
  for (int i = f.size() - 1; i >= 0; i--) {
    int t = 0;
    for (int j = 0; j < min(ans, min(f[i].f, solA)); j++) {
      mat[x][y] = f[i].s;
      x = (x + 1) % solA;
      y = (y + 1) % solB;
      if (mat[x][y] != 0) {
        x = (x + 1) % solA;
      }
      t++;
    }
    ans -= t;
  }
  cout << aux << "\n" << solA << " " << solB << "\n";
  for (int i = 0; i < solA; i++) {
    for(int j = 0; j < solB; j++)
      cout << mat[i][j] << " ";
    cout << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define f first
#define s second

vector<pair<int, int> >f;
int n, v[40002], solA, solB, ans;
map<int, int>mp;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  int poz = 0;
  for (auto it : mp) {
    f.push_back({it.s, it.f});
  }
  sort(f.begin(), f.end());
  int sum = 0;
  for (int a = 1; a <=n; a++) {
    while (poz < f.size() && f[poz].f < a) {
      sum += f[poz].f;
      poz++;
    }
    int b = (sum + a * (f.size()  - poz)) / a;
    if (a <= b && ans < a * b) {
      ans = a * b;
      solA = a;
      solB = b;
    }
  }
  vector<vector<int> >mat;
  mat.resize(solA);
  for (int i = 0; i < solA; i++)
    mat[i].resize(solB);
  int x = 0, y = 0;
  for (int i = 0; i < f.size(); i++) {
    for (int j = 0; j < min(f[i].f, solA); j++) {
      mat[x][y] = f[i].s;
      x = (x + 1) % solA;
      y = (y + 1) % solB;
      if (mat[x][y] != 0) {
        x = (x + 1) % solA;
      }
    }
  }
  cout << ans << "\n" << solA << " " << solB << "\n";
  for (int i = 0; i < solA; i++) {
    for(int j = 0; j < solB; j++)
      cout << mat[i][j] << " ";
    cout << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define f first
#define s second

vector<pair<int, int> >f;
int n, v[40002], solA, solB, ans;
map<int, int>mp;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  int poz = 0;
  for (auto it : mp) {
    f.push_back({it.s, it.f});
  }
  sort(f.begin(), f.end());
  int sum = 0;
  for (int a = 1; a <=n; a++) {
    while (f[poz].f < a) {
      sum += f[poz].f;
      poz++;
    }
    int b = (sum + a * (f.size()  - poz)) / a;
    if (a <= b && ans < a * b) {
      ans = a * b;
      solA = a;
      solB = b;
    }
  }
  vector<vector<int> >mat;
  mat.resize(solA);
  for (int i = 0; i < solA; i++)
    mat[i].resize(solB);
  int x = 0, y = 0;
  for (int i = 0; i < f.size(); i++) {
    for (int j = 0; j < min(f[i].f, solA); j++) {
      mat[x][y] = f[i].s;
      x = (x + 1) % solA;
      y = (y + 1) % solB;
    }
  }
  cout << ans << "\n" << solA << " " << solB << "\n";
  for (int i = 0; i < solA; i++) {
    for(int j = 0; j < solB; j++)
      cout << mat[i][j] << " ";
    cout << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define f first
#define s second

vector<pair<int, int> >f;
int n, v[400002], solA, solB, ans;
map<int, int>mp;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  int poz = 0;
  for (auto it : mp) {
    f.push_back({it.s, it.f});
  }
  sort(f.begin(), f.end());
  int sum = 0;
  for (int a = 1; a <=n; a++) {
    while (poz < f.size() && f[poz].f < a) {
      sum += f[poz].f;
      poz++;
    }
    int b = (sum + a * (f.size()  - poz)) / a;
    if (a <= b && ans < a * b) {
      ans = a * b;
      solA = a;
      solB = b;
    }
  }
  vector<vector<int> >mat;
  mat.resize(solA);
  for (int i = 0; i < solA; i++)
    mat[i].resize(solB);
  int x = 0, y = 0;
  int aux = ans;
  for (int i = f.size() - 1; i >= 0; i--) {
    int t = 0;
    for (int j = 0; j < min(ans, min(f[i].f, solA)); j++) {
      mat[x][y] = f[i].s;
      x = (x + 1) % solA;
      y = (y + 1) % solB;
      if (mat[x][y] != 0) {
        x = (x + 1) % solA;
      }
      t++;
    }
    ans -= t;
  }
  cout << aux << "\n" << solA << " " << solB << "\n";
  for (int i = 0; i < solA; i++) {
    for(int j = 0; j < solB; j++)
      cout << mat[i][j] << " ";
    cout << "\n";
  }
  return 0;
}