Playing with Histograms
Difficulty: Hard

In statistics, a histogram is a graphical display of tabulated frequencies, shown as bars. It shows what proportion of cases fall into each of several categories. It is a polygon composed of a sequence of rectangles aligned at a common base line. In this problem all rectangles have a width of unit length, but their heights are distinct. Some permutation of the heights will give the maximum perimeter. Your task is to find the __maximum perimeter__ of the histogram and the __number of permutations__ that give the maximum perimeter.

For example, you can have a histogram with heights {1,2,3,4} in order (1st sample test case) which has a perimeter of 16 units. One of the permutations of this set of heights, i.e. {3,1,2,4} has the maximum perimeter of 20 units.  

**Input:**
Input consists of multiple test cases. Each test case describes a histogram and starts with an integer N, 2 ≤ N ≤ 15, denoting the number of rectangles it is composed of. Next line consists of N space separated positive integers representing the heights of the rectangles. All heights are distinct and less than or equal to 100. N=0 indicates the end of tests. There are at most 50 test cases.

**Output:**
For each test case, output the maximum possible perimeter of the histogram and the number of permutations that give maximum perimeter in a single line, separated by a single space.

**Sample Input:**  
4  
1 2 3 4  
3  
2 6 5  
0  

**Sample Output:**  
20 8  
24 2  




Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include <vector>
    #include <algorithm>
    #include <cstdio>
    #include <cassert>

    using namespace std;
    int perimeter(vector<int>);
    int ways(int);
    int fact(int);

    int main()
    {
	int n;
	while(1)
	{
		scanf("%d",&n);
		if(!n)
			break;
		vector<int> v(n);
		int i;
		for(i=0;i<n;++i)
			scanf("%d",&v[i]);
		printf("%d %d\n",perimeter(v),ways((int)v.size()));
	}
	return 0;
    }

    int perimeter(vector<int> v)
    {
	sort(v.begin(),v.end());
	int lo=0;
	int hi=v.size()-1;
	int ans=v[hi];
	
	int now;
	int prev=v[hi];
	--hi;
	bool f=true;
	while(lo<=hi)
	{
		if(f)
		{
			now=v[lo];
			++lo;
		}
		else
		{
			now=v[hi];
			--hi;
		}
		f=!f;
		ans+=abs(now-prev);
		prev=now;
	}
	ans+=prev;
	
	return ans+=2*v.size();
    }

    int ways(int n)
    {
	if(n%2)
		return fact(n/2)*fact(n/2+1);
	else
		return n*fact(n/2)*fact(n/2-1);
    }

    int fact(int n)
    {
	int ans=1;
	int i;
	for(i=2;i<=n;++i)
		ans*=i;
	return ans;
    }