#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <tuple>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <stdexcept>
#include <type_traits>
#include <utility>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T x, T y){ if(x<y) swap(x,y); while(y>0) { T f = x%y; x = y; y = f; } return x; }
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
	if (!b) {d = a; x = 1; y = 0;}
	else {exgcd(b, a % b, d, y, x); y -= x * (a / b);}
}

ll inv(ll a, ll m) {
	ll x, y, d; exgcd(a, m, d, x, y);
	return d == 1 ? ((x%m) + m) % m : -1;
}

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

const int maxn = 200200;

int n;
int a[maxn];

namespace Int128Impl {
	typedef long long int64;
	typedef int       int32;

	// This class is the base class for the Doubler, it provides the
	// helper routines like
	//    int SubDouble(int128_t *A,const int128_t &B,const int borrow);
	//    int128_t DivideDouble(int128_t *A,const int128_t &B);
	//     int shiftleft(int128_t *Value,const int Carry_prm);
	//
	// Which are used by DoubleInt_t to double the size of the passed class.
	// By itself this class is fairly useful if you happen to need something
	// slightly larger than a 64-bit int (large(er) bitfields come to mind).
	//
	typedef class int128_t
	{
		public:
			// construction/casting
			int128_t()                    :Hi(0),Lo(0) {}
			int128_t(const int128_t &orig):Hi(orig.Hi),Lo(orig.Lo) {}
			int128_t(const int64    &orig):Hi(0),Lo(orig) {}
			// assignment
			int128_t &operator= (const int128_t &rhs) {Hi=rhs.Hi;Lo=rhs.Lo;return *this;}
			// compariston
			bool     operator==(const int128_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return true; return false;}
			bool     operator!=(const int128_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return false; return true;}
			bool     operator>=(const int128_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>=rhs.Lo))) return true; return false;}
			bool     operator<=(const int128_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<=rhs.Lo))) return true; return false;}
			bool     operator> (const int128_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>rhs.Lo ))) return true; return false;}
			bool     operator< (const int128_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<rhs.Lo ))) return true; return false;}
			// operations (these are exported for user use)
			int128_t &operator++() { AddDouble(this,1,0); return *this;}
			int128_t &operator--() { SubDouble(this,1,0); return *this;}

			int128_t &operator>>=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftright(this,0); return *this;}
			int128_t &operator<<=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftleft(this,0); return *this;}
			int128_t &operator-=( const int128_t &rhs) { SubDouble(this,rhs,0); return *this;}
			int128_t &operator+=( const int128_t &rhs) { AddDouble(this,rhs,0); return *this;}
			int128_t &operator*=( const int128_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			int128_t &operator/=( const int128_t &rhs) { DivideDouble(this,rhs); return *this;}
			int128_t &operator%=( const int128_t &rhs) { *this=DivideDouble(this,rhs); return *this;}

			int128_t &operator&=( const int64 &rhs) { this->Lo&=rhs; return *this;}
			int128_t &operator|=( const int64 &rhs) { this->Lo|=rhs; return *this;}
			int128_t &operator^=( const int64 &rhs) { this->Lo^=rhs; return *this;}

			int128_t &operator&=( const int128_t &rhs)  { this->Lo&=rhs.Lo; this->Hi&=rhs.Hi; return *this;}
			int128_t &operator|=( const int128_t &rhs)  { this->Lo|=rhs.Lo; this->Hi|=rhs.Hi; return *this;}
			int128_t &operator^=( const int128_t &rhs)  { this->Lo^=rhs.Lo; this->Hi^=rhs.Hi; return *this;}

	//          int128_t &operator~=( const int64 &rhs) { this->Lo~=rhs; return *this;}

			int128_t operator+(   const int128_t &rhs) { int128_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			int128_t operator-(   const int128_t &rhs) { int128_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			int128_t operator/(   const int128_t &rhs) { int128_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			int128_t operator%(   const int128_t &rhs) { int128_t tmp=*this; tmp=DivideDouble(&tmp,rhs); return tmp;}
			int128_t operator*(   const int128_t &rhs) { int128_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			int128_t operator&(   const int64    &rhs) { int128_t tmp=*this; tmp&=rhs; return tmp;}
			int128_t operator|(   const int64    &rhs) { int128_t tmp=*this; tmp|=rhs; return tmp;}
			int128_t operator^(   const int64    &rhs) { int128_t tmp=*this; tmp^=rhs; return tmp;}

			int128_t operator>>(  const int      &rhs) { int128_t tmp=*this; tmp>>=rhs; return tmp;}
			int128_t operator<<(  const int      &rhs) { int128_t tmp=*this; tmp<<=rhs; return tmp;}

			// input/output routines
			string AsString(const char *format) const;
			char GetLowByte() {return Lo&0xFF;}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(int128_t *A,const int128_t &B,const int borrow);
			static int AddDouble(int128_t *A,const int128_t &B,const int carry);
			static int128_t DivideDouble(int128_t *A,const int128_t &B);
			static int128_t MultiplyDouble(int128_t *A,const int128_t &B);
			static int shiftleft(int128_t *Value,const int Carry_prm);
			static int shiftright(int128_t *Value,const int Carry_prm);
	//  private:
			int64 Hi;
			int64 Lo;
			static const int size; //clean up the memory allocation slightly by moving this out of band..
	} int128;
	const int int128::size=128;

	/*bool operator==(const int128 &a,const int128 &b)
	{
		if ((a.Hi==b.Hi) && (a.Lo==b.Lo)) return true;
		return false;
	}*/

	//
	//
	// Assembly implementations of some functions used
	// by the int128 class.. These are just lightweight
	// inline assembly wrappers for x86 instructions
	//
	//

	static inline int64 Multiply64(int64 *A, int64 *B)
	{
		int64 ret; //unessisary the ret should be in RAX
//		asm ("mul %3    \n\t"
//			:"=a" (*A), "=d" (ret)
//			: "a" (*A), "q" (*B)
//		 );
		return ret;
	}

	// take a long word B:A and divide by C, result in A and remainder is returned
	// This is provided for completeness but isn't used because the
	// divide double is implemented as shifts/subtracts like the doubler is
	static inline int64 Divide64(int64 *A, int64 *B,int64 *C)
	{
		int64 ret;
		if (*C==0)
		{
			throw "Divide by zero";
		}
		if (*B>*C) //if the part in the high 64-bits is larger than the divisor then the
		{          //cannot fit in RAX.
			throw "Underflow";
		}
		asm ("div %4    \n\t"
			 :"=a" (*A), "=d" (ret)
			 : "a" (*A), "d" (*B), "q" (*C)
			 :
		 );
		return ret;
	}

	//returns carry and sum in A
	static inline int Add64(int64 *A,const int64 *B,const int64 carry)
	{
		char ret_carry =0;
		asm (
			 "add %3, %0  \n\t"
			 "adc %4, %0  \n\t"
			 "adc %1, %1  \n\t"
	//       "setc %1     \n\t" //appears slower than the add
	//       "cmovcq %3, %1   \n\t"
			 :"=q" (*A), "=q" (ret_carry)
			 :"0" (*A), "q" (carry), "q" (*B), "1" (ret_carry)
			 : "cc"
		);
		return (int)ret_carry;
	}

	// returns if it borroed one...
	static inline int Sub64(int64 *A, const int64 *B,const int borrow)
	{
		int ret_borrow=0;
		asm (
			"clc                \n\t"
			 "cmp $0,%[bor]      \n\t"
			 "je  0f             \n\t"
			 "stc                \n\t"
			 "0:                 \n\t"
			 "sbb %[src], %[dst] \n\t"
			 "adc %[ret_bor], %[ret_bor]\n\t"
			 : [dst] "=q" (*A), [ret_bor] "=r" (ret_borrow)
			 :        "0" (*A),           "1"  (ret_borrow), [src] "q" (*B), [bor] "q" (borrow)
			 : "cc"
			);
		return ret_borrow;
	}

	//
	//
	//          The int128_t methods
	//
	//
	//

	// A-=B; returns borrow
	inline int int128_t::SubDouble(int128_t *A,const int128_t &B,const int borrow)
	{
		int ret_borrow;
		ret_borrow=Sub64(&A->Lo,&B.Lo,borrow);
		ret_borrow=Sub64(&A->Hi,&B.Hi,ret_borrow);
		return ret_borrow;
	}

	inline int int128_t::AddDouble(int128_t *A,const int128_t &B,const int carry)
	{
		int carry_ret=0;
		carry_ret=Add64(&A->Lo,&B.Lo,carry);
		carry_ret=Add64(&A->Hi,&B.Hi,carry_ret);
		return carry_ret;
	}

	// Ha, this is a 256bit multiply, it takes two 128 bit sources and creates a 128bit destination and 128bit overflow..
	// this general code path can be abstracted into a template to generate any arbitraty length multiply as long as
	// we have a 64bit base class... For template testing we could create a 32-bit base class and compare the results
	// with a 64-bit result
	inline int128_t int128_t::MultiplyDouble(int128_t *A,const int128_t &B)
	{
		int128 ret;
		int64  tmp=0;
		int64  col3=0;

		// a 128 bit multiply works like when you were in grade school except that instead of the max value per column being
		// a 9 the max value is 2^64.

		//   ab
		//*  cd
		//------
		//   bd
		//  ad
		//  bc
		//+ac
		//-------
		// wxyz
		int64 a=A->Hi;
		int64 b=A->Lo;
		int64 c=B.Hi;
		int64 d=B.Lo;
		int64 w=0;
		int64 x=0;
		int64 y=0;
		int64 z=0;
		int   carry=0;
		int   carry2=0;
		int64 xp=0;

		y=Multiply64(&b,&d);
		z=b; b=A->Lo;
		x=Multiply64(&a,&d);
		carry=Add64(&y,&a,0);// y+=a;
		a=A->Hi;
		xp=Multiply64(&b,&c);
		carry=Add64(&x,&xp,carry); //x+=xp+carry;
		carry2=Add64(&y,&b,0);   //y+=b;
		w=Multiply64(&a,&c);
		carry2=Add64(&x,&a,carry2);//x+=a;
		// final w fixup
		int64 longcarry=carry2;
		Add64(&w,&longcarry,carry2);

		A->Lo=z;
		A->Hi=y;
		ret.Lo=x;
		ret.Hi=w;

		return ret;
	}

	// Sort of satanic because of all the carry nonsense being propagated in and out of the flags
	// for an arbitrary length integer the fastest way to do this is to put all the rcr's in a loop
	// or unroll them instead of putting all the carry flag checking everywhere
	int int128_t::shiftright(int128 *Value,const int Carry_prm)
	{
		int Carry_ret=0;
		if (Carry_prm)
		{
			asm ("stc\n\t"
				 "rcr $1,%0 \n\t"
				 "rcr $1,%1 \n\t"
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)    //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);
		}
		else
		{
			asm ("clc\n\t"
				 "rcr $1,%0 \n\t"
				 "rcr $1,%1 \n\t"
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)   //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);

		}
		return Carry_ret;
	}

	// This is the left shift version of the above right shift
	int int128_t::shiftleft(int128 *Value,const int Carry_prm)
	{
		char Carry_ret=0;
		if (Carry_prm)
		{
			asm ("stc\n\t"
				 "rcl $1,%1 \n\t"
				 "rcl $1,%0 \n\t"
	//           "setc %2     \n\t" //appears slower than the add
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)    //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);
		}
		else
		{
			asm ("clc\n\t"
				 "rcl $1,%1 \n\t"
				 "rcl $1,%0 \n\t"
	//           "setc %2     \n\t" //appears slower than the add
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)   //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);

		}
		return Carry_ret;
	}

	// from what I understand there are really only a couple of algorithms
	// useful for really long integer divides. My original plan was to
	// do some form of broken up radix 2^64 divide, but that doesn't actually
	// work without making sure the dividend is less than 2^64, if thats the case
	// we could make a really fast arbitrarily long dividend division. I'm afraid I don't
	// want to make that restriction so, I'm back to the old school, shift and subtract method.
	// A=A/B Ret=Remainder
	int128_t int128_t::DivideDouble(int128_t *A,const int128_t &B)
	{
		int128 quotient=*A;
		int128 remainder;//==0

		if ((int128_t)(B)==0) //TODO: fix this const mess
		{
			throw "division by zero";
		}

		for (int x=0;x<A->size;x++)
		{
			int hibit=shiftleft(&quotient,0);
			shiftleft(&remainder,hibit);
			if (remainder>=B)
			{
				SubDouble(&remainder,B,0); //remainder-=*B;
				quotient.Lo|=1; //quotient|=1;
			}
		}

		*A=quotient;
		return remainder;
	}

	// this takes the 64-bit value and prints it out given the formatter
	// this is about as far from efficient as can be
	string AsString(const int64 &Value,const char *format)
	{
		char temp[256];
		string ret;
		switch (format[1])
		{
			case 'd':
				sprintf(temp,"%lld",Value);
				ret=temp;
				break;
//			case 'b':
//				sprintf(temp,"%llb",Value);
//				ret=temp;
//				break;
			case 'X':
				sprintf(temp,"%llX",Value);
				ret=temp;
				break;
			case 'x':
				sprintf(temp,"%llx",Value);
				ret=temp;
				break;
		}
		return ret;
	}

	//string int128_t::AsString(const int128_t &Value,char *format)
	string int128_t::AsString(const char *format) const
	{
		char buff[256];
		char* temp = buff;
		string ret;
		switch (format[1])
		{
			case 'd':
			break;
			case 'b':
				break;
			case 'X':
			case 'x':
			{
				int128_t tmp;
				tmp=*this;
				temp[(tmp.size>>2)]=0;
				for (int x=(tmp.size>>2)-1;x>=0;x--)
				{
					temp[x]=(tmp.Lo)&0xF;
					if (temp[x]>0x9)
					{
						temp[x]+='A'-10;
					}
					else
					{
						temp[x]+='0';
					}
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
				}
			}
			break;
		}
		printf("-- %s\n", temp);
		ret=temp;
		return ret;
	}

	template<class BaseIntT> class DoubleInt_t
	{
		public:
			// construction/casting
			DoubleInt_t()                    :Hi(0),Lo(0),size(Hi.size*2) {}
			DoubleInt_t(const DoubleInt_t &orig):Hi(orig.Hi),Lo(orig.Lo),size(Hi.size*2) {}
			DoubleInt_t(const BaseIntT    &orig):Hi(0),Lo(orig),size(Hi.size*2) {}
			DoubleInt_t(const int64       &orig):Hi(0),Lo(orig),size(Hi.size*2) {}
			// assignment
			DoubleInt_t &operator= (const DoubleInt_t &rhs) {Hi=rhs.Hi;Lo=rhs.Lo;return *this;}
			// compariston
			bool     operator==(const DoubleInt_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return true; return false;}
			bool     operator!=(const DoubleInt_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return false; return true;}
			bool     operator>=(const DoubleInt_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>=rhs.Lo))) return true; return false;}
			bool     operator<=(const DoubleInt_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<=rhs.Lo))) return true; return false;}
			bool     operator> (const DoubleInt_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>rhs.Lo ))) return true; return false;}
			bool     operator< (const DoubleInt_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<rhs.Lo ))) return true; return false;}
			// operations (these are exported for user use)
			DoubleInt_t &operator>>=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftright(this,0); return *this;}
			DoubleInt_t &operator<<=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftleft(this,0); return *this;}
			DoubleInt_t &operator-=( const DoubleInt_t &rhs) { SubDouble(this,rhs,0); return *this;}
			DoubleInt_t &operator+=( const DoubleInt_t &rhs) { AddDouble(this,rhs,0); return *this;}
			DoubleInt_t &operator*=( const DoubleInt_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			DoubleInt_t &operator/=( const DoubleInt_t &rhs) { DivideDouble(this,rhs); return *this;}
			DoubleInt_t &operator%=( const DoubleInt_t &rhs) { *this=DivideDouble(this,rhs); return *this;}

			DoubleInt_t &operator&=( const int64 &rhs) { this->Lo&=rhs; return *this;}
			DoubleInt_t &operator|=( const int64 &rhs) { this->Lo|=rhs; return *this;}
			DoubleInt_t &operator^=( const int64 &rhs) { this->Lo^=rhs; return *this;}

			DoubleInt_t &operator&=( const DoubleInt_t &rhs) { this->Lo&=rhs.Lo; this->Hi&=rhs.Hi; return *this;}
			DoubleInt_t &operator|=( const DoubleInt_t &rhs) { this->Lo|=rhs.Lo; this->Hi|=rhs.Hi; return *this;}
			DoubleInt_t &operator^=( const DoubleInt_t &rhs) { this->Lo^=rhs.Lo; this->Hi^=rhs.Hi; return *this;}

			DoubleInt_t operator+(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			DoubleInt_t operator-(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			DoubleInt_t operator/(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			DoubleInt_t operator%(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; tmp=DivideDouble(&tmp,rhs); return tmp;}
			DoubleInt_t operator*(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			DoubleInt_t operator&(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo&=rhs; return tmp;}
			DoubleInt_t operator|(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo|=rhs; return tmp;}
			DoubleInt_t operator^(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo^=rhs; return tmp;}

			DoubleInt_t operator>>(  const int      &rhs) { DoubleInt_t tmp=*this; tmp>>=rhs; return tmp;}
			DoubleInt_t operator<<(  const int      &rhs) { DoubleInt_t tmp=*this; tmp<<=rhs; return tmp;}

			// consider overridding printf until then use AsString
			string AsString(const char *format);
			void   FromString(const char *Source_prm);
			char   GetLowByte() {return Lo.GetLowByte();}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(DoubleInt_t *A,const DoubleInt_t &B,const int borrow);
			static int AddDouble(DoubleInt_t *A,const DoubleInt_t &B,const int carry);
			static DoubleInt_t DivideDouble(DoubleInt_t *A,const DoubleInt_t &B);
			static DoubleInt_t MultiplyDouble(DoubleInt_t *A,const DoubleInt_t &B);
			static int shiftleft(DoubleInt_t *Value,const int Carry_prm);
			static int shiftright(DoubleInt_t *Value,const int Carry_prm);
	//  private:
			BaseIntT Hi;
			BaseIntT Lo;
			const int size;
	};

	// throw exceptions on overflow/underflow etc..
	template<class BaseInnT> class OverflowException_t
	{

	};

	template<class BaseIntT, class ExponentT> class Floating_t
	{
	};

	// normal number systems don't support -0 so we don't either...
	// this class just takes an unsigned base and adds a sign to it,
	// it does this without changing the bit encoding of the value, so its
	// not exactly efficient for certain operations, on the other hand we don't then have
	// to worry about sign extension or anything like that...
	template<class BaseIntT> class SignedInt_t
	{
		public:
			// construction/casting
			SignedInt_t()                    :Value(0),Negative(0) {}
			SignedInt_t(const SignedInt_t &orig):Value(orig.Value),Negative(orig.Negative) {}
			SignedInt_t(const BaseIntT    &orig):Value(orig),Negative(0) {}
			SignedInt_t(const int64       &orig):Value(orig),Negative(0) { if (orig<0) { Negative=1; Value^=BaseIntT(-1); Value+=BaseIntT(1);}} //ugly!
			// assignment
			SignedInt_t &operator= (const SignedInt_t &rhs) {Value=rhs.Value; Negative=rhs.Negative; return *this;}
			// compariston (some of thse operators could use a little teaking for efficiency)
			bool     operator==(const SignedInt_t &rhs) { if ((Value==rhs.Value) && (Negative==rhs.Negative)) return true; return false;}
			bool     operator!=(const SignedInt_t &rhs) { if ((Value==rhs.Value) && (Negative==rhs.Negative)) return false; return true;}
			bool     operator>=(const SignedInt_t &rhs) { if ((Negative==1) && (rhs.Negative==1))   {   if (Value<=rhs.Value)   {   return true;    }   return false;   }
														  else if ((Negative==0) && (rhs.Negative==0))  {   if (Value>=rhs.Value)   {   return true;    }   return false; }
														  else if (Negative==1) {   return false;   }   else    {   return true;    }   return false; }
			bool     operator<=(const SignedInt_t &rhs) { if (*this==rhs) return true; else if (*this>=rhs) return false; else return true;}
			bool     operator> (const SignedInt_t &rhs) { if (*this==rhs) return false; else return (*this>=rhs);}
			bool     operator< (const SignedInt_t &rhs) { if (*this==rhs) return false; else return !(*this>=rhs);}
			// operations (these are exported for user use), many of these operations are "wierd" because they don't affect the sign aka shift and bit instructions maintain signage
			SignedInt_t &operator>>=(const int      rhs)  { Value>>=rhs; return *this;}
			SignedInt_t &operator<<=(const int      rhs)  { Value<<=rhs; return *this;}
			SignedInt_t &operator-=( const SignedInt_t &rhs) { SubDouble(this,rhs,0);  return *this;}
			SignedInt_t &operator+=( const SignedInt_t &rhs) { AddDouble(this,rhs,0); return *this;}
			SignedInt_t &operator*=( const SignedInt_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			SignedInt_t &operator/=( const SignedInt_t &rhs) { DivideDouble(this,rhs); return *this;}

			SignedInt_t &operator&=( const int64 &rhs) { Value&=rhs; return *this;}
			SignedInt_t &operator|=( const int64 &rhs) { Value|=rhs; return *this;}
			SignedInt_t &operator^=( const int64 &rhs) { Value^=rhs; return *this;}

			SignedInt_t &operator&=( const SignedInt_t &rhs) { Value&=rhs.Value; return *this;}
			SignedInt_t &operator|=( const SignedInt_t &rhs) { Value|=rhs.Value; return *this;}
			SignedInt_t &operator^=( const SignedInt_t &rhs) { Value^=rhs.Value; return *this;}

			SignedInt_t operator+(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			SignedInt_t operator-(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			SignedInt_t operator/(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			SignedInt_t operator*(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			SignedInt_t operator&(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value&=rhs; return tmp;}
			SignedInt_t operator|(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value|=rhs; return tmp;}
			SignedInt_t operator^(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value^=rhs; return tmp;}

			SignedInt_t operator>>(  const int      &rhs) { SignedInt_t tmp=*this; tmp>>=rhs; return tmp;}
			SignedInt_t operator<<(  const int      &rhs) { SignedInt_t tmp=*this; tmp<<=rhs; return tmp;}

			// the following pretty much the same as above, in both cases we are in temp hell

			// consider overridding printf until then use AsString
			//string AsString(const SignedInt_t &Value,char *format);
			string AsString(const char *format) { string ret=Value.AsString(format); if (Negative) ret.insert(0,"-"); return ret;}
			void   FromString(const char *Source_prm);
			char   GetLowByte() {return Value.GetLowByte();}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(SignedInt_t *A,const SignedInt_t &B,const int borrow);
			static int AddDouble(SignedInt_t *A,const SignedInt_t &B,const int carry);
			static SignedInt_t DivideDouble(SignedInt_t *A,const SignedInt_t &B);
			static SignedInt_t MultiplyDouble(SignedInt_t *A,const SignedInt_t &B);
			static int shiftleft(SignedInt_t *Value_prm,const int Carry_prm) { return shiftleft(Value_prm->Value,Carry_prm);}
			static int shiftright(SignedInt_t *Value_prm,const int Carry_prm) { return shiftright(Value_prm->Value,Carry_prm);}

	//  private:
			BaseIntT Value;
			int Negative;
	};

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::SubDouble(DoubleInt_t *A,const DoubleInt_t &B,const int borrow)
	{
		int ret_borrow;
		ret_borrow=BaseIntT::SubDouble(&A->Lo,B.Lo,borrow);
		ret_borrow=BaseIntT::SubDouble(&A->Hi,B.Hi,ret_borrow);
		return ret_borrow;
	}

	//
	//
	//          The DoubleInt_t methods
	//
	//
	//

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::AddDouble(DoubleInt_t *A,const DoubleInt_t &B,const int carry)
	{
		int carry_ret=0;
		carry_ret=BaseIntT::AddDouble(&A->Lo,B.Lo,carry);
		carry_ret=BaseIntT::AddDouble(&A->Hi,B.Hi,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> DoubleInt_t<BaseIntT> DoubleInt_t<BaseIntT>::MultiplyDouble(DoubleInt_t *A,const DoubleInt_t &B)
	{
		DoubleInt_t ret;
		BaseIntT  tmp=0;
		BaseIntT  col3=0;

		// a 128 bit multiply works like when you were in grade school except that instead of the max value per column being
		// a 9 the max value is 2^64.

		//   ab
		//*  cd
		//------
		//   bd
		//  ad
		//  bc
		//+ac
		//-------
		// wxyz
		BaseIntT a=A->Hi;
		BaseIntT b=A->Lo;
		BaseIntT c=B.Hi;
		BaseIntT d=B.Lo;
		BaseIntT w=0;
		BaseIntT x=0;
		BaseIntT y=0;
		BaseIntT z=0;
		int   carry=0;
		int   carry2=0;
		BaseIntT xp=0;

		y=BaseIntT::MultiplyDouble(&b,d);
		z=b; b=A->Lo;
		x=BaseIntT::MultiplyDouble(&a,d);
		carry=BaseIntT::AddDouble(&y,a,0);//    y+=a;
		a=A->Hi;
		xp=BaseIntT::MultiplyDouble(&b,c);
		carry=BaseIntT::AddDouble(&x,xp,carry); //x+=xp+carry;
		carry2=BaseIntT::AddDouble(&y,b,0);   //y+=b;
		w=BaseIntT::MultiplyDouble(&a,c);
		carry2=BaseIntT::AddDouble(&x,a,carry2);//x+=a;
		// final w fixup
		BaseIntT longcarry=carry2;
		BaseIntT::AddDouble(&w,longcarry,carry2);

		A->Lo=z;
		A->Hi=y;
		ret.Lo=x;
		ret.Hi=w;

		return ret;
	}

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::shiftright(DoubleInt_t *Value,const int Carry_prm)
	{
		int carry_ret;
		carry_ret=BaseIntT::shiftright(&Value->Hi,Carry_prm);
		carry_ret=BaseIntT::shiftright(&Value->Lo,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::shiftleft(DoubleInt_t *Value,const int Carry_prm)
	{
		int carry_ret;
		carry_ret=BaseIntT::shiftleft(&Value->Lo,Carry_prm);
		carry_ret=BaseIntT::shiftleft(&Value->Hi,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> DoubleInt_t<BaseIntT> DoubleInt_t<BaseIntT>::DivideDouble(DoubleInt_t *A,const DoubleInt_t &B)
	{
		DoubleInt_t quotient=*A;
		DoubleInt_t remainder;//==0

		if ((DoubleInt_t)(B)==0) //TODO: fix this const mess
		{
			throw "division by zero";
		}

		for (int x=0;x<A->size;x++)
		{
			int hibit=shiftleft(&quotient,0);
			shiftleft(&remainder,hibit);
			if (remainder>=B)
			{
				SubDouble(&remainder,B,0); //remainder-=*B;
				quotient.Lo|=1; //quotient|=1;
			}
		}

		*A=quotient;
		return remainder;
	}

	template<class BaseIntT> string DoubleInt_t<BaseIntT>::AsString(const char *format)
	{
		string ret;
		switch (format[1])
		{
			case 'd':
			{
				char temp[2];
				DoubleInt_t tmp;
				int x=0;
				tmp=*this;
				if (tmp==0)
				{
					ret="0";
				}
				else
				{
					ret="";
					temp[1]='\0';

					while (tmp!=0)
					{
						DoubleInt_t remainder=DivideDouble(&tmp,DoubleInt_t(10)); //TODO: convert to something a little faster...
						temp[0]=remainder.GetLowByte()+'0';
						ret.insert(0,string(temp));
					}
				}
			}
			break;
			case 'b':
			{
				DoubleInt_t tmp;
				int x=0;
				tmp=*this;
				char temp[tmp.size+2];

				for (int x=0;x<tmp.size;x++)
				{
					temp[x]=tmp.GetLowByte()&0x01+'0';
					shiftright(&tmp,0);
				}
				temp[tmp.size]='\0';
				ret=temp;
			}
			break;
			case 'X':
			case 'x':
			{
				DoubleInt_t tmp;
				tmp=*this;
				char *temp=new char[(tmp.size>>2)+2];
				temp[(tmp.size>>2)]=0;
				for (int x=(tmp.size>>2)-1;x>=0;x--)
				{
					temp[x]=(tmp.Lo).GetLowByte()&0xF;
					if (temp[x]>0x9)
					{
						temp[x]+='A'-10;
					}
					else
					{
						temp[x]+='0';
					}
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
				}
				ret=temp;
				delete[] temp;
			}
			break;
		}
		return ret;
	}

	// takes the value as a base 10 or base 16 string and converts it to the big integer type
	template<class BaseIntT> void DoubleInt_t<BaseIntT>::FromString(const char *Source_prm)
	{
		int start=0;
		int base=10;
		Hi=Lo=0;
		while (Source_prm[start]!='\0')
		{
			if (Source_prm[start]=='0')
			{
				if (Source_prm[start+1]=='x')
				{
					base=16;
					start+=2;
				}
				break;
			}
			if ((Source_prm[start]>='0') && (Source_prm[start]<='9'))
			{
				break;
			}
			start++;
		}
		// ok we found the beginning of the string..
		if (base==10)
		{
			DoubleInt_t multconst(10);
			while ((Source_prm[start]<='9') && (Source_prm[start]>='0'))
			{
				char tmp=Source_prm[start]-'0';
				MultiplyDouble(this,multconst);
				AddDouble(this,DoubleInt_t(tmp),0);
				start++;
			}
		}
		else
		{
			char upper=toupper(Source_prm[start]);
			while (((upper<='9') && (upper>='0')) || ((upper>='A') && (upper<='F')))
			{
				char tmp;
				if (upper<='9')
				{
					tmp=upper-'0';
				}
				else
				{
					tmp=upper-'A';
				}
				shiftleft(this,0);
				shiftleft(this,0);
				shiftleft(this,0);
				shiftleft(this,0);
				Lo|=int64(tmp);
				start++;
				upper=toupper(Source_prm[start]);
			}
		}
	}

	template<class BaseIntT> int SignedInt_t<BaseIntT>::AddDouble(SignedInt_t *A,const SignedInt_t &B,const int carry)
	{
		if (carry!=0)
		{
			throw "non zero carry passed to SignedInt add";
		}
		if (A->Negative==B.Negative) //same sign.. just add them, don't modify the sign
		{
			return BaseIntT::AddDouble(&A->Value,B.Value,carry);
		}
		else
		{
			// ok now here is the fun with non two complement numbers

			// first detect which one has the larger magnitude
			if (A->Value>B.Value)
			{
				//A will maintain its sign, but subtract B from A
				if (BaseIntT::SubDouble(&A->Value,B.Value,0)!=0)
				{
					throw "borrow nessisary during add!"; //probably a bug...
				}
				return 0; //there shouldn't be a carry in this case
			}
			else
			{
				if (A->Value==B.Value)
				{
					//rare shortcut....
					A->Value=0;
					A->Negative=0;
					return 0;
				}
				else // (B.Value>A.Value)
				{
					//A will have B's Sign..

					// this is a lot sucky...
					BaseIntT tmp=A->Value;
					A->Value=B.Value;
					if (BaseIntT::SubDouble(&A->Value,tmp,0)!=0)
					{
						throw "borrow nessisary during add!"; //probably a bug...
					}
					A->Negative=B.Negative;
					return 0; //there shouldn't be a carry in this case
				}
			}
		}
	}

	// flip the sign of A and add them...
	template<class BaseIntT> int SignedInt_t<BaseIntT>::SubDouble(SignedInt_t *A,const SignedInt_t &B,const int borrow)
	{
		A->Negative^=1; //negate the sign with xor <chuckle>
		return AddDouble(A,B,0);
	}

	//TODO make a finalizer class which sits between this class and the double template and throws overflow exceptions...
	template<class BaseIntT> SignedInt_t<BaseIntT> SignedInt_t<BaseIntT>::MultiplyDouble(SignedInt_t *A,const SignedInt_t &B)
	{
		BaseIntT::MultiplyDouble(&A->Value,B.Value);
		if (A->Value==0)
		{
			A->Negative=0;
		}
		else
		{
			A->Negative^=B.Negative; //flip sign as nessiary
		}
		return *A;
	}

	template<class BaseIntT> SignedInt_t<BaseIntT> SignedInt_t<BaseIntT>::DivideDouble(SignedInt_t *A,const SignedInt_t &B)
	{
		SignedInt_t tmp;
		tmp.Value=BaseIntT::DivideDouble(&A->Value,B.Value);
		if (A->Value==0)
		{
			A->Negative=0;
		}
		else
		{
			A->Negative^=B.Negative; //flip sign as nessiary
		}
		return tmp.Value;
	}

	template<class BaseIntT> void SignedInt_t<BaseIntT>::FromString(const char *Source_prm)
	{
		int start=0;
		Negative=0;

		while (Source_prm[start]!='\0')
		{
			if (Source_prm[start]=='0')
			{
				if (Source_prm[start+1]=='x')
				{
					// hex start
					Value.FromString(&Source_prm[start]);
					break;
				}
			}
			if ((Source_prm[start]>='0') && (Source_prm[start]<='9'))
			{
				// normal decimal start
				Value.FromString(&Source_prm[start]);
				break;
			}
			if (Source_prm[start]=='-')
			{
				Negative=1;
			}
			start++;
		}
		if (Source_prm[start]=='\0')
		{
			Value=0;
		}
	}

	template<typename T>
	std::ostream & operator<<(std::ostream & stream, const SignedInt_t<T> & rhs){
		if (stream.flags() & stream.oct){
			stream << "ERR";
		}
		else if (stream.flags() & stream.dec){
			stream << rhs.AsString("%d") << " : " << rhs.AsString("%x");
		}
		else if (stream.flags() & stream.hex){
			stream << rhs.AsString("%x");
		}
		return stream;
	}
};

typedef Int128Impl::SignedInt_t<Int128Impl::DoubleInt_t<Int128Impl::int128_t> > bigint;

int main(int argc, char *argv[]) {
	std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
	#if DEBUG
	freopen("/tmp/in.txt", "r", stdin);
	while(cin>>n)
	#else
	cin >> n;
	#endif
	{
		map<ll,bigint> bs, b1;
		bigint r = 0;
		bigint nbr = 0;
		bigint ts = 0;
		for(int i=0;i<n;i++) {
			ll x; cin >> x;
			bigint nbr0 = b1[x+1] + b1[x] + b1[x-1];
			bigint s0 = bs[x+1] + bs[x] + bs[x-1];

			r += (nbr - nbr0) * x - (ts-s0);

			bs[x] += x;
			b1[x] += 1;
			ts += x;
			nbr += 1;
		}

		cout << r.AsString("%d") << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <tuple>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <stdexcept>
#include <type_traits>
#include <utility>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T x, T y){ if(x<y) swap(x,y); while(y>0) { T f = x%y; x = y; y = f; } return x; }
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
	if (!b) {d = a; x = 1; y = 0;}
	else {exgcd(b, a % b, d, y, x); y -= x * (a / b);}
}

ll inv(ll a, ll m) {
	ll x, y, d; exgcd(a, m, d, x, y);
	return d == 1 ? ((x%m) + m) % m : -1;
}

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

const int maxn = 200200;

int n;
int a[maxn];

namespace Int128Impl {
	class uint128_t{
		private:
			uint64_t UPPER, LOWER;

		public:
			// Constructors
			uint128_t();
			uint128_t(const uint128_t & rhs);
			uint128_t(uint128_t && rhs);

			template <typename T> uint128_t(const T & rhs)
				: UPPER(0), LOWER(rhs)
			{
				static_assert(std::is_integral <T>::value, "Input argument type must be an integer.");
			}

			template <typename S, typename T> uint128_t(const S & upper_rhs, const T & lower_rhs)
				: UPPER(upper_rhs), LOWER(lower_rhs)
			{
				static_assert(std::is_integral <S>::value &&
							  std::is_integral <T>::value
							  , "Input argument types must be integers.");
			}

			//  RHS input args only

			// Assignment Operator
			uint128_t & operator=(const uint128_t & rhs);
			uint128_t & operator=(uint128_t && rhs);

			template <typename T> uint128_t & operator=(const T & rhs){
				static_assert(std::is_integral <T>::value, "Input argument type must be an integer.");
				UPPER = 0;
				LOWER = rhs;
				return *this;
			}

			// Typecast Operators
			operator bool() const;
			operator uint8_t() const;
			operator uint16_t() const;
			operator uint32_t() const;
			operator uint64_t() const;

			// Bitwise Operators
			uint128_t operator&(const uint128_t & rhs) const;

			template <typename T> uint128_t operator&(const T & rhs) const{
				return uint128_t(0, LOWER & (uint64_t) rhs);
			}

			uint128_t & operator&=(const uint128_t & rhs);

			template <typename T> uint128_t & operator&=(const T & rhs){
				UPPER = 0;
				LOWER &= rhs;
				return *this;
			}

			uint128_t operator|(const uint128_t & rhs) const;

			template <typename T> uint128_t operator|(const T & rhs) const{
				return uint128_t(UPPER, LOWER | (uint64_t) rhs);
			}

			uint128_t & operator|=(const uint128_t & rhs);

			template <typename T> uint128_t & operator|=(const T & rhs){
				LOWER |= (uint64_t) rhs;
				return *this;
			}

			uint128_t operator^(const uint128_t & rhs) const;

			template <typename T> uint128_t operator^(const T & rhs) const{
				return uint128_t(UPPER, LOWER ^ (uint64_t) rhs);
			}

			uint128_t & operator^=(const uint128_t & rhs);

			template <typename T> uint128_t & operator^=(const T & rhs){
				LOWER ^= (uint64_t) rhs;
				return *this;
			}

			uint128_t operator~() const;

			// Bit Shift Operators
			uint128_t operator<<(const uint128_t & rhs) const;

			template <typename T> uint128_t operator<<(const T & rhs) const{
				return *this << uint128_t(rhs);
			}

			uint128_t & operator<<=(const uint128_t & rhs);

			template <typename T> uint128_t & operator<<=(const T & rhs){
				*this = *this << uint128_t(rhs);
				return *this;
			}

			uint128_t operator>>(const uint128_t & rhs) const;

			template <typename T> uint128_t operator>>(const T & rhs) const{
				return *this >> uint128_t(rhs);
			}

			uint128_t & operator>>=(const uint128_t & rhs);

			template <typename T> uint128_t & operator>>=(const T & rhs){
				*this = *this >> uint128_t(rhs);
				return *this;
			}

			// Logical Operators
			bool operator!() const;
			bool operator&&(const uint128_t & rhs) const;
			bool operator||(const uint128_t & rhs) const;

			template <typename T> bool operator&&(const T & rhs){
				return static_cast <bool> (*this && rhs);
			}

			template <typename T> bool operator||(const T & rhs){
				return static_cast <bool> (*this || rhs);
			}

			// Comparison Operators
			bool operator==(const uint128_t & rhs) const;

			template <typename T> bool operator==(const T & rhs) const{
				return (!UPPER && (LOWER == (uint64_t) rhs));
			}

			bool operator!=(const uint128_t & rhs) const;

			template <typename T> bool operator!=(const T & rhs) const{
				return (UPPER | (LOWER != (uint64_t) rhs));
			}

			bool operator>(const uint128_t & rhs) const;

			template <typename T> bool operator>(const T & rhs) const{
				return (UPPER || (LOWER > (uint64_t) rhs));
			}

			bool operator<(const uint128_t & rhs) const;

			template <typename T> bool operator<(const T & rhs) const{
				return (!UPPER)?(LOWER < (uint64_t) rhs):false;
			}

			bool operator>=(const uint128_t & rhs) const;

			template <typename T> bool operator>=(const T & rhs) const{
				return ((*this > rhs) | (*this == rhs));
			}

			bool operator<=(const uint128_t & rhs) const;

			template <typename T> bool operator<=(const T & rhs) const{
				return ((*this < rhs) | (*this == rhs));
			}

			// Arithmetic Operators
			uint128_t operator+(const uint128_t & rhs) const;

			template <typename T> uint128_t operator+(const T & rhs) const{
				return uint128_t(UPPER + ((LOWER + (uint64_t) rhs) < LOWER), LOWER + (uint64_t) rhs);
			}

			uint128_t & operator+=(const uint128_t & rhs);

			template <typename T> uint128_t & operator+=(const T & rhs){
				UPPER = UPPER + ((LOWER + rhs) < LOWER);
				LOWER = LOWER + rhs;
				return *this;
			}

			uint128_t operator-(const uint128_t & rhs) const;

			template <typename T> uint128_t operator-(const T & rhs) const{
				return uint128_t((uint64_t) (UPPER - ((LOWER - rhs) > LOWER)), (uint64_t) (LOWER - rhs));
			}

			uint128_t & operator-=(const uint128_t & rhs);

			template <typename T> uint128_t & operator-=(const T & rhs){
				*this = *this - rhs;
				return *this;
			}

			uint128_t operator*(const uint128_t & rhs) const;

			template <typename T> uint128_t operator*(const T & rhs) const{
				return *this * uint128_t(rhs);
			}

			uint128_t & operator*=(const uint128_t & rhs);

			template <typename T> uint128_t & operator*=(const T & rhs){
				*this = *this * uint128_t(rhs);
				return *this;
			}

		private:
			std::pair <uint128_t, uint128_t> divmod(const uint128_t & lhs, const uint128_t & rhs) const;

		public:
			uint128_t operator/(const uint128_t & rhs) const;

			template <typename T> uint128_t operator/(const T & rhs) const{
				return *this / uint128_t(rhs);
			}

			uint128_t & operator/=(const uint128_t & rhs);

			template <typename T> uint128_t & operator/=(const T & rhs){
				*this = *this / uint128_t(rhs);
				return *this;
			}

			uint128_t operator%(const uint128_t & rhs) const;

			template <typename T> uint128_t operator%(const T & rhs) const{
				return *this % uint128_t(rhs);
			}

			uint128_t & operator%=(const uint128_t & rhs);

			template <typename T> uint128_t & operator%=(const T & rhs){
				*this = *this % uint128_t(rhs);
				return *this;
			}

			// Increment Operator
			uint128_t & operator++();
			uint128_t operator++(int);

			// Decrement Operator
			uint128_t & operator--();
			uint128_t operator--(int);

			// Nothing done since promotion doesn't work here
			uint128_t operator+() const;

			// two's complement
			uint128_t operator-() const;

			// Get private values
			const uint64_t & upper() const;
			const uint64_t & lower() const;

			// Get bitsize of value
			uint8_t bits() const;

			// Get string representation of value
			std::string str(uint8_t base = 10, const unsigned int & len = 0) const;
	};

	// useful values
	extern const uint128_t uint128_0;
	extern const uint128_t uint128_1;

	// lhs type T as first arguemnt
	// If the output is not a bool, casts to type T

	// Bitwise Operators
	template <typename T> uint128_t operator&(const T & lhs, const uint128_t & rhs){
		return rhs & lhs;
	}

	template <typename T> T & operator&=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (rhs & lhs);
	}

	template <typename T> uint128_t operator|(const T & lhs, const uint128_t & rhs){
		return rhs | lhs;
	}

	template <typename T> T & operator|=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (rhs | lhs);
	}

	template <typename T> uint128_t operator^(const T & lhs, const uint128_t & rhs){
		return rhs ^ lhs;
	}

	template <typename T> T & operator^=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (rhs ^ lhs);
	}

	// Bitshift operators
	uint128_t operator<<(const bool     & lhs, const uint128_t & rhs);
	uint128_t operator<<(const uint8_t  & lhs, const uint128_t & rhs);
	uint128_t operator<<(const uint16_t & lhs, const uint128_t & rhs);
	uint128_t operator<<(const uint32_t & lhs, const uint128_t & rhs);
	uint128_t operator<<(const uint64_t & lhs, const uint128_t & rhs);
	uint128_t operator<<(const int8_t   & lhs, const uint128_t & rhs);
	uint128_t operator<<(const int16_t  & lhs, const uint128_t & rhs);
	uint128_t operator<<(const int32_t  & lhs, const uint128_t & rhs);
	uint128_t operator<<(const int64_t  & lhs, const uint128_t & rhs);

	template <typename T> T & operator<<=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (uint128_t(lhs) << rhs);
	}

	uint128_t operator>>(const bool     & lhs, const uint128_t & rhs);
	uint128_t operator>>(const uint8_t  & lhs, const uint128_t & rhs);
	uint128_t operator>>(const uint16_t & lhs, const uint128_t & rhs);
	uint128_t operator>>(const uint32_t & lhs, const uint128_t & rhs);
	uint128_t operator>>(const uint64_t & lhs, const uint128_t & rhs);
	uint128_t operator>>(const int8_t   & lhs, const uint128_t & rhs);
	uint128_t operator>>(const int16_t  & lhs, const uint128_t & rhs);
	uint128_t operator>>(const int32_t  & lhs, const uint128_t & rhs);
	uint128_t operator>>(const int64_t  & lhs, const uint128_t & rhs);

	template <typename T> T & operator>>=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (uint128_t(lhs) >> rhs);
	}

	// Comparison Operators
	template <typename T> bool operator==(const T & lhs, const uint128_t & rhs){
		return (!rhs.upper() && ((uint64_t) lhs == rhs.lower()));
	}

	template <typename T> bool operator!=(const T & lhs, const uint128_t & rhs){
		return (rhs.upper() | ((uint64_t) lhs != rhs.lower()));
	}

	template <typename T> bool operator>(const T & lhs, const uint128_t & rhs){
		return (!rhs.upper()) && ((uint64_t) lhs > rhs.lower());
	}

	template <typename T> bool operator<(const T & lhs, const uint128_t & rhs){
		if (rhs.upper()){
			return true;
		}
		return ((uint64_t) lhs < rhs.lower());
	}

	template <typename T> bool operator>=(const T & lhs, const uint128_t & rhs){
		if (rhs.upper()){
			return false;
		}
		return ((uint64_t) lhs >= rhs.lower());
	}

	template <typename T> bool operator<=(const T & lhs, const uint128_t & rhs){
		if (rhs.upper()){
			return true;
		}
		return ((uint64_t) lhs <= rhs.lower());
	}

	// Arithmetic Operators
	template <typename T> uint128_t operator+(const T & lhs, const uint128_t & rhs){
		return rhs + lhs;
	}

	template <typename T> T & operator+=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (rhs + lhs);
	}

	template <typename T> uint128_t operator-(const T & lhs, const uint128_t & rhs){
		return -(rhs - lhs);
	}

	template <typename T> T & operator-=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (-(rhs - lhs));
	}

	template <typename T> uint128_t operator*(const T & lhs, const uint128_t & rhs){
		return rhs * lhs;
	}

	template <typename T> T & operator*=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (rhs * lhs);
	}

	template <typename T> uint128_t operator/(const T & lhs, const uint128_t & rhs){
		return uint128_t(lhs) / rhs;
	}

	template <typename T> T & operator/=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (uint128_t(lhs) / rhs);
	}

	template <typename T> uint128_t operator%(const T & lhs, const uint128_t & rhs){
		return uint128_t(lhs) % rhs;
	}

	template <typename T> T & operator%=(T & lhs, const uint128_t & rhs){
		return lhs = static_cast <T> (uint128_t(lhs) % rhs);
	}

	// IO Operator
	std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs);
	const uint128_t uint128_0(0);
	const uint128_t uint128_1(1);

	uint128_t::uint128_t()
		: UPPER(0), LOWER(0)
	{}

	uint128_t::uint128_t(const uint128_t & rhs)
		: UPPER(rhs.UPPER), LOWER(rhs.LOWER)
	{}

	uint128_t::uint128_t(uint128_t && rhs)
		: UPPER(std::move(rhs.UPPER)), LOWER(std::move(rhs.LOWER))
	{
		if (this != &rhs){
			rhs.UPPER = 0;
			rhs.LOWER = 0;
		}
	}

	uint128_t & uint128_t::operator=(const uint128_t & rhs){
		UPPER = rhs.UPPER;
		LOWER = rhs.LOWER;
		return *this;
	}

	uint128_t & uint128_t::operator=(uint128_t && rhs){
		if (this != &rhs){
			UPPER = std::move(rhs.UPPER);
			LOWER = std::move(rhs.LOWER);
			rhs.UPPER = 0;
			rhs.LOWER = 0;
		}
		return *this;
	}

	uint128_t::operator bool() const{
		return (bool) (UPPER | LOWER);
	}

	uint128_t::operator uint8_t() const{
		return (uint8_t) LOWER;
	}

	uint128_t::operator uint16_t() const{
		return (uint16_t) LOWER;
	}

	uint128_t::operator uint32_t() const{
		return (uint32_t) LOWER;
	}

	uint128_t::operator uint64_t() const{
		return (uint64_t) LOWER;
	}

	uint128_t uint128_t::operator&(const uint128_t & rhs) const{
		return uint128_t(UPPER & rhs.UPPER, LOWER & rhs.LOWER);
	}

	uint128_t & uint128_t::operator&=(const uint128_t & rhs){
		UPPER &= rhs.UPPER;
		LOWER &= rhs.LOWER;
		return *this;
	}

	uint128_t uint128_t::operator|(const uint128_t & rhs) const{
		return uint128_t(UPPER | rhs.UPPER, LOWER | rhs.LOWER);
	}

	uint128_t & uint128_t::operator|=(const uint128_t & rhs){
		UPPER |= rhs.UPPER;
		LOWER |= rhs.LOWER;
		return *this;
	}

	uint128_t uint128_t::operator^(const uint128_t & rhs) const{
		return uint128_t(UPPER ^ rhs.UPPER, LOWER ^ rhs.LOWER);
	}

	uint128_t & uint128_t::operator^=(const uint128_t & rhs){
		UPPER ^= rhs.UPPER;
		LOWER ^= rhs.LOWER;
		return *this;
	}

	uint128_t uint128_t::operator~() const{
		return uint128_t(~UPPER, ~LOWER);
	}

	uint128_t uint128_t::operator<<(const uint128_t & rhs) const{
		const uint64_t shift = rhs.LOWER;
		if (((bool) rhs.UPPER) || (shift >= 128)){
			return uint128_0;
		}
		else if (shift == 64){
			return uint128_t(LOWER, 0);
		}
		else if (shift == 0){
			return *this;
		}
		else if (shift < 64){
			return uint128_t((UPPER << shift) + (LOWER >> (64 - shift)), LOWER << shift);
		}
		else if ((128 > shift) && (shift > 64)){
			return uint128_t(LOWER << (shift - 64), 0);
		}
		else{
			return uint128_0;
		}
	}

	uint128_t & uint128_t::operator<<=(const uint128_t & rhs){
		*this = *this << rhs;
		return *this;
	}

	uint128_t uint128_t::operator>>(const uint128_t & rhs) const{
		const uint64_t shift = rhs.LOWER;
		if (((bool) rhs.UPPER) || (shift >= 128)){
			return uint128_0;
		}
		else if (shift == 64){
			return uint128_t(0, UPPER);
		}
		else if (shift == 0){
			return *this;
		}
		else if (shift < 64){
			return uint128_t(UPPER >> shift, (UPPER << (64 - shift)) + (LOWER >> shift));
		}
		else if ((128 > shift) && (shift > 64)){
			return uint128_t(0, (UPPER >> (shift - 64)));
		}
		else{
			return uint128_0;
		}
	}

	uint128_t & uint128_t::operator>>=(const uint128_t & rhs){
		*this = *this >> rhs;
		return *this;
	}

	bool uint128_t::operator!() const{
		return !(bool) (UPPER | LOWER);
	}

	bool uint128_t::operator&&(const uint128_t & rhs) const{
		return ((bool) *this && rhs);
	}

	bool uint128_t::operator||(const uint128_t & rhs) const{
		 return ((bool) *this || rhs);
	}

	bool uint128_t::operator==(const uint128_t & rhs) const{
		return ((UPPER == rhs.UPPER) && (LOWER == rhs.LOWER));
	}

	bool uint128_t::operator!=(const uint128_t & rhs) const{
		return ((UPPER != rhs.UPPER) | (LOWER != rhs.LOWER));
	}

	bool uint128_t::operator>(const uint128_t & rhs) const{
		if (UPPER == rhs.UPPER){
			return (LOWER > rhs.LOWER);
		}
		return (UPPER > rhs.UPPER);
	}

	bool uint128_t::operator<(const uint128_t & rhs) const{
		if (UPPER == rhs.UPPER){
			return (LOWER < rhs.LOWER);
		}
		return (UPPER < rhs.UPPER);
	}

	bool uint128_t::operator>=(const uint128_t & rhs) const{
		return ((*this > rhs) | (*this == rhs));
	}

	bool uint128_t::operator<=(const uint128_t & rhs) const{
		return ((*this < rhs) | (*this == rhs));
	}

	uint128_t uint128_t::operator+(const uint128_t & rhs) const{
		return uint128_t(UPPER + rhs.UPPER + ((LOWER + rhs.LOWER) < LOWER), LOWER + rhs.LOWER);
	}

	uint128_t & uint128_t::operator+=(const uint128_t & rhs){
		UPPER += rhs.UPPER + ((LOWER + rhs.LOWER) < LOWER);
		LOWER += rhs.LOWER;
		return *this;
	}

	uint128_t uint128_t::operator-(const uint128_t & rhs) const{
		return uint128_t(UPPER - rhs.UPPER - ((LOWER - rhs.LOWER) > LOWER), LOWER - rhs.LOWER);
	}

	uint128_t & uint128_t::operator-=(const uint128_t & rhs){
		*this = *this - rhs;
		return *this;
	}

	uint128_t uint128_t::operator*(const uint128_t & rhs) const{
		// split values into 4 32-bit parts
		uint64_t top[4] = {UPPER >> 32, UPPER & 0xffffffff, LOWER >> 32, LOWER & 0xffffffff};
		uint64_t bottom[4] = {rhs.UPPER >> 32, rhs.UPPER & 0xffffffff, rhs.LOWER >> 32, rhs.LOWER & 0xffffffff};
		uint64_t products[4][4];

		// multiply each component of the values
		for(int y = 3; y > -1; y--){
			for(int x = 3; x > -1; x--){
				products[3 - x][y] = top[x] * bottom[y];
			}
		}

		// first row
		uint64_t fourth32 = (products[0][3] & 0xffffffff);
		uint64_t third32  = (products[0][2] & 0xffffffff) + (products[0][3] >> 32);
		uint64_t second32 = (products[0][1] & 0xffffffff) + (products[0][2] >> 32);
		uint64_t first32  = (products[0][0] & 0xffffffff) + (products[0][1] >> 32);

		// second row
		third32  += (products[1][3] & 0xffffffff);
		second32 += (products[1][2] & 0xffffffff) + (products[1][3] >> 32);
		first32  += (products[1][1] & 0xffffffff) + (products[1][2] >> 32);

		// third row
		second32 += (products[2][3] & 0xffffffff);
		first32  += (products[2][2] & 0xffffffff) + (products[2][3] >> 32);

		// fourth row
		first32  += (products[3][3] & 0xffffffff);

		// move carry to next digit
		third32  += fourth32 >> 32;
		second32 += third32  >> 32;
		first32  += second32 >> 32;

		// remove carry from current digit
		fourth32 &= 0xffffffff;
		third32  &= 0xffffffff;
		second32 &= 0xffffffff;
		first32  &= 0xffffffff;

		// combine components
		return uint128_t((first32 << 32) | second32, (third32 << 32) | fourth32);
	}

	uint128_t & uint128_t::operator*=(const uint128_t & rhs){
		*this = *this * rhs;
		return *this;
	}

	std::pair <uint128_t, uint128_t> uint128_t::divmod(const uint128_t & lhs, const uint128_t & rhs) const{
		// Save some calculations /////////////////////
		if (rhs == uint128_0){
			throw std::domain_error("Error: division or modulus by 0");
		}
		else if (rhs == uint128_1){
			return std::pair <uint128_t, uint128_t> (lhs, uint128_0);
		}
		else if (lhs == rhs){
			return std::pair <uint128_t, uint128_t> (uint128_1, uint128_0);
		}
		else if ((lhs == uint128_0) || (lhs < rhs)){
			return std::pair <uint128_t, uint128_t> (uint128_0, lhs);
		}

		std::pair <uint128_t, uint128_t> qr (uint128_0, uint128_0);
		for(uint8_t x = lhs.bits(); x > 0; x--){
			qr.first  <<= uint128_1;
			qr.second <<= uint128_1;

			if ((lhs >> (x - 1U)) & 1){
				qr.second++;
			}

			if (qr.second >= rhs){
				qr.second -= rhs;
				qr.first++;
			}
		}
		return qr;
	}

	uint128_t uint128_t::operator/(const uint128_t & rhs) const{
		return divmod(*this, rhs).first;
	}

	uint128_t & uint128_t::operator/=(const uint128_t & rhs){
		*this = *this / rhs;
		return *this;
	}

	uint128_t uint128_t::operator%(const uint128_t & rhs) const{
		return divmod(*this, rhs).second;
	}

	uint128_t & uint128_t::operator%=(const uint128_t & rhs){
		*this = *this % rhs;
		return *this;
	}

	uint128_t & uint128_t::operator++(){
		return *this += uint128_1;
	}

	uint128_t uint128_t::operator++(int){
		uint128_t temp(*this);
		++*this;
		return temp;
	}

	uint128_t & uint128_t::operator--(){
		return *this -= uint128_1;
	}

	uint128_t uint128_t::operator--(int){
		uint128_t temp(*this);
		--*this;
		return temp;
	}

	uint128_t uint128_t::operator+() const{
		return *this;
	}

	uint128_t uint128_t::operator-() const{
		return ~*this + uint128_1;
	}

	const uint64_t & uint128_t::upper() const{
		return UPPER;
	}

	const uint64_t & uint128_t::lower() const{
		return LOWER;
	}

	uint8_t uint128_t::bits() const{
		uint8_t out = 0;
		if (UPPER){
			out = 64;
			uint64_t up = UPPER;
			while (up){
				up >>= 1;
				out++;
			}
		}
		else{
			uint64_t low = LOWER;
			while (low){
				low >>= 1;
				out++;
			}
		}
		return out;
	}

	std::string uint128_t::str(uint8_t base, const unsigned int & len) const{
		if ((base < 2) || (base > 16)){
			throw std::invalid_argument("Base must be in the range [2, 16]");
		}
		std::string out = "";
		if (!(*this)){
			out = "0";
		}
		else{
			std::pair <uint128_t, uint128_t> qr(*this, uint128_0);
			do{
				qr = divmod(qr.first, base);
				out = "0123456789abcdef"[(uint8_t) qr.second] + out;
			} while (qr.first);
		}
		if (out.size() < len){
			out = std::string(len - out.size(), '0') + out;
		}
		return out;
	}

	uint128_t operator<<(const bool & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const uint8_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const uint16_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const uint32_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const uint64_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const int8_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const int16_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const int32_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator<<(const int64_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) << rhs;
	}

	uint128_t operator>>(const bool & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const uint8_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const uint16_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const uint32_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const uint64_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const int8_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const int16_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const int32_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	uint128_t operator>>(const int64_t & lhs, const uint128_t & rhs){
		return uint128_t(lhs) >> rhs;
	}

	std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs){
		if (stream.flags() & stream.oct){
			stream << rhs.str(8);
		}
		else if (stream.flags() & stream.dec){
			stream << rhs.str(10);
		}
		else if (stream.flags() & stream.hex){
			stream << rhs.str(16);
		}
		return stream;
	}

};

// Give uint128_t type traits
namespace std {  // This is probably not a good idea
	template <> struct is_arithmetic <Int128Impl::uint128_t> : std::true_type {};
	template <> struct is_integral   <Int128Impl::uint128_t> : std::true_type {};
	template <> struct is_unsigned   <Int128Impl::uint128_t> : std::true_type {};
};

namespace Int128Impl {
	typedef long long int64;
	typedef int       int32;

	// This class is the base class for the Doubler, it provides the
	// helper routines like
	//    int SubDouble(int128_t *A,const int128_t &B,const int borrow);
	//    int128_t DivideDouble(int128_t *A,const int128_t &B);
	//     int shiftleft(int128_t *Value,const int Carry_prm);
	//
	// Which are used by DoubleInt_t to double the size of the passed class.
	// By itself this class is fairly useful if you happen to need something
	// slightly larger than a 64-bit int (large(er) bitfields come to mind).
	//
	typedef class int128_t
	{
		public:
			// construction/casting
			int128_t()                    :Hi(0),Lo(0) {}
			int128_t(const int128_t &orig):Hi(orig.Hi),Lo(orig.Lo) {}
			int128_t(const int64    &orig):Hi(0),Lo(orig) {}
			// assignment
			int128_t &operator= (const int128_t &rhs) {Hi=rhs.Hi;Lo=rhs.Lo;return *this;}
			// compariston
			bool     operator==(const int128_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return true; return false;}
			bool     operator!=(const int128_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return false; return true;}
			bool     operator>=(const int128_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>=rhs.Lo))) return true; return false;}
			bool     operator<=(const int128_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<=rhs.Lo))) return true; return false;}
			bool     operator> (const int128_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>rhs.Lo ))) return true; return false;}
			bool     operator< (const int128_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<rhs.Lo ))) return true; return false;}
			// operations (these are exported for user use)
			int128_t &operator++() { AddDouble(this,1,0); return *this;}
			int128_t &operator--() { SubDouble(this,1,0); return *this;}

			int128_t &operator>>=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftright(this,0); return *this;}
			int128_t &operator<<=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftleft(this,0); return *this;}
			int128_t &operator-=( const int128_t &rhs) { SubDouble(this,rhs,0); return *this;}
			int128_t &operator+=( const int128_t &rhs) { AddDouble(this,rhs,0); return *this;}
			int128_t &operator*=( const int128_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			int128_t &operator/=( const int128_t &rhs) { DivideDouble(this,rhs); return *this;}
			int128_t &operator%=( const int128_t &rhs) { *this=DivideDouble(this,rhs); return *this;}

			int128_t &operator&=( const int64 &rhs) { this->Lo&=rhs; return *this;}
			int128_t &operator|=( const int64 &rhs) { this->Lo|=rhs; return *this;}
			int128_t &operator^=( const int64 &rhs) { this->Lo^=rhs; return *this;}

			int128_t &operator&=( const int128_t &rhs)  { this->Lo&=rhs.Lo; this->Hi&=rhs.Hi; return *this;}
			int128_t &operator|=( const int128_t &rhs)  { this->Lo|=rhs.Lo; this->Hi|=rhs.Hi; return *this;}
			int128_t &operator^=( const int128_t &rhs)  { this->Lo^=rhs.Lo; this->Hi^=rhs.Hi; return *this;}

	//          int128_t &operator~=( const int64 &rhs) { this->Lo~=rhs; return *this;}

			int128_t operator+(   const int128_t &rhs) { int128_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			int128_t operator-(   const int128_t &rhs) { int128_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			int128_t operator/(   const int128_t &rhs) { int128_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			int128_t operator%(   const int128_t &rhs) { int128_t tmp=*this; tmp=DivideDouble(&tmp,rhs); return tmp;}
			int128_t operator*(   const int128_t &rhs) { int128_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			int128_t operator&(   const int64    &rhs) { int128_t tmp=*this; tmp&=rhs; return tmp;}
			int128_t operator|(   const int64    &rhs) { int128_t tmp=*this; tmp|=rhs; return tmp;}
			int128_t operator^(   const int64    &rhs) { int128_t tmp=*this; tmp^=rhs; return tmp;}

			int128_t operator>>(  const int      &rhs) { int128_t tmp=*this; tmp>>=rhs; return tmp;}
			int128_t operator<<(  const int      &rhs) { int128_t tmp=*this; tmp<<=rhs; return tmp;}

			// input/output routines
			string AsString(const char *format) const;
			char GetLowByte() {return Lo&0xFF;}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(int128_t *A,const int128_t &B,const int borrow);
			static int AddDouble(int128_t *A,const int128_t &B,const int carry);
			static int128_t DivideDouble(int128_t *A,const int128_t &B);
			static int128_t MultiplyDouble(int128_t *A,const int128_t &B);
			static int shiftleft(int128_t *Value,const int Carry_prm);
			static int shiftright(int128_t *Value,const int Carry_prm);
	//  private:
			int64 Hi;
			int64 Lo;
			static const int size; //clean up the memory allocation slightly by moving this out of band..
	} int128;
	const int int128::size=128;

	/*bool operator==(const int128 &a,const int128 &b)
	{
		if ((a.Hi==b.Hi) && (a.Lo==b.Lo)) return true;
		return false;
	}*/

	//
	//
	// Assembly implementations of some functions used
	// by the int128 class.. These are just lightweight
	// inline assembly wrappers for x86 instructions
	//
	//

	static inline int64 Multiply64(int64 *A, int64 *B)
	{
		int64 ret; //unessisary the ret should be in RAX
		asm ("mul %3    \n\t"
			 :"=a" (*A), "=d" (ret)
			 : "a" (*A), "r" (*B)
			 :
		 );
		return ret;
	}

	// take a long word B:A and divide by C, result in A and remainder is returned
	// This is provided for completeness but isn't used because the
	// divide double is implemented as shifts/subtracts like the doubler is
	static inline int64 Divide64(int64 *A, int64 *B,int64 *C)
	{
		int64 ret;
		if (*C==0)
		{
			throw "Divide by zero";
		}
		if (*B>*C) //if the part in the high 64-bits is larger than the divisor then the
		{          //cannot fit in RAX.
			throw "Underflow";
		}
		asm ("div %4    \n\t"
			 :"=a" (*A), "=d" (ret)
			 : "a" (*A), "d" (*B), "r" (*C)
			 :
		 );
		return ret;
	}

	//returns carry and sum in A
	static inline int Add64(int64 *A,const int64 *B,const int64 carry)
	{
		char ret_carry =0;
		asm (
			 "add %3, %0  \n\t"
			 "adc %4, %0  \n\t"
			 "adc %1, %1  \n\t"
	//       "setc %1     \n\t" //appears slower than the add
	//       "cmovcq %3, %1   \n\t"
			 :"=r" (*A), "=r" (ret_carry)
			 :"0" (*A), "r" (carry), "r" (*B), "1" (ret_carry)
			 : "cc"
		);
		return (int)ret_carry;
	}

	// returns if it borroed one...
	static inline int Sub64(int64 *A, const int64 *B,const int borrow)
	{
		int ret_borrow=0;
		asm (
			"clc                \n\t"
			 "cmp $0,%[bor]      \n\t"
			 "je  0f             \n\t"
			 "stc                \n\t"
			 "0:                 \n\t"
			 "sbb %[src], %[dst] \n\t"
			 "adc %[ret_bor], %[ret_bor]\n\t"
			 : [dst] "=r" (*A), [ret_bor] "=r" (ret_borrow)
			 :        "0" (*A),           "1"  (ret_borrow), [src] "r" (*B), [bor] "r" (borrow)
			 : "cc"
			);
		return ret_borrow;
	}

	//
	//
	//          The int128_t methods
	//
	//
	//

	// A-=B; returns borrow
	inline int int128_t::SubDouble(int128_t *A,const int128_t &B,const int borrow)
	{
		int ret_borrow;
		ret_borrow=Sub64(&A->Lo,&B.Lo,borrow);
		ret_borrow=Sub64(&A->Hi,&B.Hi,ret_borrow);
		return ret_borrow;
	}

	inline int int128_t::AddDouble(int128_t *A,const int128_t &B,const int carry)
	{
		int carry_ret=0;
		carry_ret=Add64(&A->Lo,&B.Lo,carry);
		carry_ret=Add64(&A->Hi,&B.Hi,carry_ret);
		return carry_ret;
	}

	// Ha, this is a 256bit multiply, it takes two 128 bit sources and creates a 128bit destination and 128bit overflow..
	// this general code path can be abstracted into a template to generate any arbitraty length multiply as long as
	// we have a 64bit base class... For template testing we could create a 32-bit base class and compare the results
	// with a 64-bit result
	inline int128_t int128_t::MultiplyDouble(int128_t *A,const int128_t &B)
	{
		int128 ret;
		int64  tmp=0;
		int64  col3=0;

		// a 128 bit multiply works like when you were in grade school except that instead of the max value per column being
		// a 9 the max value is 2^64.

		//   ab
		//*  cd
		//------
		//   bd
		//  ad
		//  bc
		//+ac
		//-------
		// wxyz
		int64 a=A->Hi;
		int64 b=A->Lo;
		int64 c=B.Hi;
		int64 d=B.Lo;
		int64 w=0;
		int64 x=0;
		int64 y=0;
		int64 z=0;
		int   carry=0;
		int   carry2=0;
		int64 xp=0;

		y=Multiply64(&b,&d);
		z=b; b=A->Lo;
		x=Multiply64(&a,&d);
		carry=Add64(&y,&a,0);// y+=a;
		a=A->Hi;
		xp=Multiply64(&b,&c);
		carry=Add64(&x,&xp,carry); //x+=xp+carry;
		carry2=Add64(&y,&b,0);   //y+=b;
		w=Multiply64(&a,&c);
		carry2=Add64(&x,&a,carry2);//x+=a;
		// final w fixup
		int64 longcarry=carry2;
		Add64(&w,&longcarry,carry2);

		A->Lo=z;
		A->Hi=y;
		ret.Lo=x;
		ret.Hi=w;

		return ret;
	}

	// Sort of satanic because of all the carry nonsense being propagated in and out of the flags
	// for an arbitrary length integer the fastest way to do this is to put all the rcr's in a loop
	// or unroll them instead of putting all the carry flag checking everywhere
	int int128_t::shiftright(int128 *Value,const int Carry_prm)
	{
		int Carry_ret=0;
		if (Carry_prm)
		{
			asm ("stc\n\t"
				 "rcr $1,%0 \n\t"
				 "rcr $1,%1 \n\t"
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)    //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);
		}
		else
		{
			asm ("clc\n\t"
				 "rcr $1,%0 \n\t"
				 "rcr $1,%1 \n\t"
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)   //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);

		}
		return Carry_ret;
	}

	// This is the left shift version of the above right shift
	int int128_t::shiftleft(int128 *Value,const int Carry_prm)
	{
		char Carry_ret=0;
		if (Carry_prm)
		{
			asm ("stc\n\t"
				 "rcl $1,%1 \n\t"
				 "rcl $1,%0 \n\t"
	//           "setc %2     \n\t" //appears slower than the add
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)    //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);
		}
		else
		{
			asm ("clc\n\t"
				 "rcl $1,%1 \n\t"
				 "rcl $1,%0 \n\t"
	//           "setc %2     \n\t" //appears slower than the add
				 "adc %2,%2 \n\t"
				 : "=r" (Value->Hi), "=r" (Value->Lo), "=r" (Carry_ret)   //output
				 : "0" (Value->Hi), "1" (Value->Lo), "2" (Carry_ret)     //input
				 : "cc"                                  //clobber
				);

		}
		return Carry_ret;
	}

	// from what I understand there are really only a couple of algorithms
	// useful for really long integer divides. My original plan was to
	// do some form of broken up radix 2^64 divide, but that doesn't actually
	// work without making sure the dividend is less than 2^64, if thats the case
	// we could make a really fast arbitrarily long dividend division. I'm afraid I don't
	// want to make that restriction so, I'm back to the old school, shift and subtract method.
	// A=A/B Ret=Remainder
	int128_t int128_t::DivideDouble(int128_t *A,const int128_t &B)
	{
		int128 quotient=*A;
		int128 remainder;//==0

		if ((int128_t)(B)==0) //TODO: fix this const mess
		{
			throw "division by zero";
		}

		for (int x=0;x<A->size;x++)
		{
			int hibit=shiftleft(&quotient,0);
			shiftleft(&remainder,hibit);
			if (remainder>=B)
			{
				SubDouble(&remainder,B,0); //remainder-=*B;
				quotient.Lo|=1; //quotient|=1;
			}
		}

		*A=quotient;
		return remainder;
	}

	// this takes the 64-bit value and prints it out given the formatter
	// this is about as far from efficient as can be
	string AsString(const int64 &Value,const char *format)
	{
		char temp[256];
		string ret;
		switch (format[1])
		{
			case 'd':
				sprintf(temp,"%lld",Value);
				ret=temp;
				break;
			case 'b':
				sprintf(temp,"%llb",Value);
				ret=temp;
				break;
			case 'X':
				sprintf(temp,"%llX",Value);
				ret=temp;
				break;
			case 'x':
				sprintf(temp,"%llx",Value);
				ret=temp;
				break;
		}
		return ret;
	}

	//string int128_t::AsString(const int128_t &Value,char *format)
	string int128_t::AsString(const char *format) const
	{
		char buff[256];
		char* temp = buff;
		string ret;
		switch (format[1])
		{
			case 'd':
			break;
			case 'b':
				break;
			case 'X':
			case 'x':
			{
				int128_t tmp;
				tmp=*this;
				temp[(tmp.size>>2)]=0;
				for (int x=(tmp.size>>2)-1;x>=0;x--)
				{
					temp[x]=(tmp.Lo)&0xF;
					if (temp[x]>0x9)
					{
						temp[x]+='A'-10;
					}
					else
					{
						temp[x]+='0';
					}
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
				}
			}
			break;
		}
		printf("-- %s\n", temp);
		ret=temp;
		return ret;
	}

	template<class BaseIntT> class DoubleInt_t
	{
		public:
			// construction/casting
			DoubleInt_t()                    :Hi(0),Lo(0),size(Hi.size*2) {}
			DoubleInt_t(const DoubleInt_t &orig):Hi(orig.Hi),Lo(orig.Lo),size(Hi.size*2) {}
			DoubleInt_t(const BaseIntT    &orig):Hi(0),Lo(orig),size(Hi.size*2) {}
			DoubleInt_t(const int64       &orig):Hi(0),Lo(orig),size(Hi.size*2) {}
			// assignment
			DoubleInt_t &operator= (const DoubleInt_t &rhs) {Hi=rhs.Hi;Lo=rhs.Lo;return *this;}
			// compariston
			bool     operator==(const DoubleInt_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return true; return false;}
			bool     operator!=(const DoubleInt_t &rhs) { if ((Hi==rhs.Hi) && (Lo==rhs.Lo)) return false; return true;}
			bool     operator>=(const DoubleInt_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>=rhs.Lo))) return true; return false;}
			bool     operator<=(const DoubleInt_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<=rhs.Lo))) return true; return false;}
			bool     operator> (const DoubleInt_t &rhs) { if ((Hi>rhs.Hi) || ( (Hi==rhs.Hi) && (Lo>rhs.Lo ))) return true; return false;}
			bool     operator< (const DoubleInt_t &rhs) { if ((Hi<rhs.Hi) || ( (Hi==rhs.Hi) && (Lo<rhs.Lo ))) return true; return false;}
			// operations (these are exported for user use)
			DoubleInt_t &operator>>=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftright(this,0); return *this;}
			DoubleInt_t &operator<<=(const int      rhs)  { for (int x=0;x<rhs;x++) shiftleft(this,0); return *this;}
			DoubleInt_t &operator-=( const DoubleInt_t &rhs) { SubDouble(this,rhs,0); return *this;}
			DoubleInt_t &operator+=( const DoubleInt_t &rhs) { AddDouble(this,rhs,0); return *this;}
			DoubleInt_t &operator*=( const DoubleInt_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			DoubleInt_t &operator/=( const DoubleInt_t &rhs) { DivideDouble(this,rhs); return *this;}
			DoubleInt_t &operator%=( const DoubleInt_t &rhs) { *this=DivideDouble(this,rhs); return *this;}

			DoubleInt_t &operator&=( const int64 &rhs) { this->Lo&=rhs; return *this;}
			DoubleInt_t &operator|=( const int64 &rhs) { this->Lo|=rhs; return *this;}
			DoubleInt_t &operator^=( const int64 &rhs) { this->Lo^=rhs; return *this;}

			DoubleInt_t &operator&=( const DoubleInt_t &rhs) { this->Lo&=rhs.Lo; this->Hi&=rhs.Hi; return *this;}
			DoubleInt_t &operator|=( const DoubleInt_t &rhs) { this->Lo|=rhs.Lo; this->Hi|=rhs.Hi; return *this;}
			DoubleInt_t &operator^=( const DoubleInt_t &rhs) { this->Lo^=rhs.Lo; this->Hi^=rhs.Hi; return *this;}

			DoubleInt_t operator+(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			DoubleInt_t operator-(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			DoubleInt_t operator/(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			DoubleInt_t operator%(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; tmp=DivideDouble(&tmp,rhs); return tmp;}
			DoubleInt_t operator*(   const DoubleInt_t &rhs) { DoubleInt_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			DoubleInt_t operator&(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo&=rhs; return tmp;}
			DoubleInt_t operator|(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo|=rhs; return tmp;}
			DoubleInt_t operator^(   const int64    &rhs) { DoubleInt_t tmp=*this; tmp.Lo^=rhs; return tmp;}

			DoubleInt_t operator>>(  const int      &rhs) { DoubleInt_t tmp=*this; tmp>>=rhs; return tmp;}
			DoubleInt_t operator<<(  const int      &rhs) { DoubleInt_t tmp=*this; tmp<<=rhs; return tmp;}

			// consider overridding printf until then use AsString
			string AsString(const char *format);
			void   FromString(const char *Source_prm);
			char   GetLowByte() {return Lo.GetLowByte();}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(DoubleInt_t *A,const DoubleInt_t &B,const int borrow);
			static int AddDouble(DoubleInt_t *A,const DoubleInt_t &B,const int carry);
			static DoubleInt_t DivideDouble(DoubleInt_t *A,const DoubleInt_t &B);
			static DoubleInt_t MultiplyDouble(DoubleInt_t *A,const DoubleInt_t &B);
			static int shiftleft(DoubleInt_t *Value,const int Carry_prm);
			static int shiftright(DoubleInt_t *Value,const int Carry_prm);
	//  private:
			BaseIntT Hi;
			BaseIntT Lo;
			const int size;
	};

	// throw exceptions on overflow/underflow etc..
	template<class BaseInnT> class OverflowException_t
	{

	};

	template<class BaseIntT, class ExponentT> class Floating_t
	{
	};

	// normal number systems don't support -0 so we don't either...
	// this class just takes an unsigned base and adds a sign to it,
	// it does this without changing the bit encoding of the value, so its
	// not exactly efficient for certain operations, on the other hand we don't then have
	// to worry about sign extension or anything like that...
	template<class BaseIntT> class SignedInt_t
	{
		public:
			// construction/casting
			SignedInt_t()                    :Value(0),Negative(0) {}
			SignedInt_t(const SignedInt_t &orig):Value(orig.Value),Negative(orig.Negative) {}
			SignedInt_t(const BaseIntT    &orig):Value(orig),Negative(0) {}
			SignedInt_t(const int64       &orig):Value(orig),Negative(0) { if (orig<0) { Negative=1; Value^=BaseIntT(-1); Value+=BaseIntT(1);}} //ugly!
			// assignment
			SignedInt_t &operator= (const SignedInt_t &rhs) {Value=rhs.Value; Negative=rhs.Negative; return *this;}
			// compariston (some of thse operators could use a little teaking for efficiency)
			bool     operator==(const SignedInt_t &rhs) { if ((Value==rhs.Value) && (Negative==rhs.Negative)) return true; return false;}
			bool     operator!=(const SignedInt_t &rhs) { if ((Value==rhs.Value) && (Negative==rhs.Negative)) return false; return true;}
			bool     operator>=(const SignedInt_t &rhs) { if ((Negative==1) && (rhs.Negative==1))   {   if (Value<=rhs.Value)   {   return true;    }   return false;   }
														  else if ((Negative==0) && (rhs.Negative==0))  {   if (Value>=rhs.Value)   {   return true;    }   return false; }
														  else if (Negative==1) {   return false;   }   else    {   return true;    }   return false; }
			bool     operator<=(const SignedInt_t &rhs) { if (*this==rhs) return true; else if (*this>=rhs) return false; else return true;}
			bool     operator> (const SignedInt_t &rhs) { if (*this==rhs) return false; else return (*this>=rhs);}
			bool     operator< (const SignedInt_t &rhs) { if (*this==rhs) return false; else return !(*this>=rhs);}
			// operations (these are exported for user use), many of these operations are "wierd" because they don't affect the sign aka shift and bit instructions maintain signage
			SignedInt_t &operator>>=(const int      rhs)  { Value>>=rhs; return *this;}
			SignedInt_t &operator<<=(const int      rhs)  { Value<<=rhs; return *this;}
			SignedInt_t &operator-=( const SignedInt_t &rhs) { SubDouble(this,rhs,0);  return *this;}
			SignedInt_t &operator+=( const SignedInt_t &rhs) { AddDouble(this,rhs,0); return *this;}
			SignedInt_t &operator*=( const SignedInt_t &rhs) { MultiplyDouble(this,rhs); return *this;}
			SignedInt_t &operator/=( const SignedInt_t &rhs) { DivideDouble(this,rhs); return *this;}

			SignedInt_t &operator&=( const int64 &rhs) { Value&=rhs; return *this;}
			SignedInt_t &operator|=( const int64 &rhs) { Value|=rhs; return *this;}
			SignedInt_t &operator^=( const int64 &rhs) { Value^=rhs; return *this;}

			SignedInt_t &operator&=( const SignedInt_t &rhs) { Value&=rhs.Value; return *this;}
			SignedInt_t &operator|=( const SignedInt_t &rhs) { Value|=rhs.Value; return *this;}
			SignedInt_t &operator^=( const SignedInt_t &rhs) { Value^=rhs.Value; return *this;}

			SignedInt_t operator+(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; AddDouble(&tmp,rhs,0); return tmp;}
			SignedInt_t operator-(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; SubDouble(&tmp,rhs,0); return tmp;}
			SignedInt_t operator/(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; DivideDouble(&tmp,rhs); return tmp;}
			SignedInt_t operator*(   const SignedInt_t &rhs) { SignedInt_t tmp=*this; MultiplyDouble(&tmp,rhs); return tmp;}

			SignedInt_t operator&(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value&=rhs; return tmp;}
			SignedInt_t operator|(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value|=rhs; return tmp;}
			SignedInt_t operator^(   const int64    &rhs) { SignedInt_t tmp=*this; tmp.Value^=rhs; return tmp;}

			SignedInt_t operator>>(  const int      &rhs) { SignedInt_t tmp=*this; tmp>>=rhs; return tmp;}
			SignedInt_t operator<<(  const int      &rhs) { SignedInt_t tmp=*this; tmp<<=rhs; return tmp;}

			// the following pretty much the same as above, in both cases we are in temp hell

			// consider overridding printf until then use AsString
			//string AsString(const SignedInt_t &Value,char *format);
			string AsString(const char *format) { string ret=Value.AsString(format); if (Negative) ret.insert(0,"-"); return ret;}
			void   FromString(const char *Source_prm);
			char   GetLowByte() {return Value.GetLowByte();}
	//  protected:
			// these operations are exported for higher level use
			// they don't use the this variable...
			static int SubDouble(SignedInt_t *A,const SignedInt_t &B,const int borrow);
			static int AddDouble(SignedInt_t *A,const SignedInt_t &B,const int carry);
			static SignedInt_t DivideDouble(SignedInt_t *A,const SignedInt_t &B);
			static SignedInt_t MultiplyDouble(SignedInt_t *A,const SignedInt_t &B);
			static int shiftleft(SignedInt_t *Value_prm,const int Carry_prm) { return shiftleft(Value_prm->Value,Carry_prm);}
			static int shiftright(SignedInt_t *Value_prm,const int Carry_prm) { return shiftright(Value_prm->Value,Carry_prm);}

	//  private:
			BaseIntT Value;
			int Negative;
	};

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::SubDouble(DoubleInt_t *A,const DoubleInt_t &B,const int borrow)
	{
		int ret_borrow;
		ret_borrow=BaseIntT::SubDouble(&A->Lo,B.Lo,borrow);
		ret_borrow=BaseIntT::SubDouble(&A->Hi,B.Hi,ret_borrow);
		return ret_borrow;
	}

	//
	//
	//          The DoubleInt_t methods
	//
	//
	//

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::AddDouble(DoubleInt_t *A,const DoubleInt_t &B,const int carry)
	{
		int carry_ret=0;
		carry_ret=BaseIntT::AddDouble(&A->Lo,B.Lo,carry);
		carry_ret=BaseIntT::AddDouble(&A->Hi,B.Hi,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> DoubleInt_t<BaseIntT> DoubleInt_t<BaseIntT>::MultiplyDouble(DoubleInt_t *A,const DoubleInt_t &B)
	{
		DoubleInt_t ret;
		BaseIntT  tmp=0;
		BaseIntT  col3=0;

		// a 128 bit multiply works like when you were in grade school except that instead of the max value per column being
		// a 9 the max value is 2^64.

		//   ab
		//*  cd
		//------
		//   bd
		//  ad
		//  bc
		//+ac
		//-------
		// wxyz
		BaseIntT a=A->Hi;
		BaseIntT b=A->Lo;
		BaseIntT c=B.Hi;
		BaseIntT d=B.Lo;
		BaseIntT w=0;
		BaseIntT x=0;
		BaseIntT y=0;
		BaseIntT z=0;
		int   carry=0;
		int   carry2=0;
		BaseIntT xp=0;

		y=BaseIntT::MultiplyDouble(&b,d);
		z=b; b=A->Lo;
		x=BaseIntT::MultiplyDouble(&a,d);
		carry=BaseIntT::AddDouble(&y,a,0);//    y+=a;
		a=A->Hi;
		xp=BaseIntT::MultiplyDouble(&b,c);
		carry=BaseIntT::AddDouble(&x,xp,carry); //x+=xp+carry;
		carry2=BaseIntT::AddDouble(&y,b,0);   //y+=b;
		w=BaseIntT::MultiplyDouble(&a,c);
		carry2=BaseIntT::AddDouble(&x,a,carry2);//x+=a;
		// final w fixup
		BaseIntT longcarry=carry2;
		BaseIntT::AddDouble(&w,longcarry,carry2);

		A->Lo=z;
		A->Hi=y;
		ret.Lo=x;
		ret.Hi=w;

		return ret;
	}

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::shiftright(DoubleInt_t *Value,const int Carry_prm)
	{
		int carry_ret;
		carry_ret=BaseIntT::shiftright(&Value->Hi,Carry_prm);
		carry_ret=BaseIntT::shiftright(&Value->Lo,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> int DoubleInt_t<BaseIntT>::shiftleft(DoubleInt_t *Value,const int Carry_prm)
	{
		int carry_ret;
		carry_ret=BaseIntT::shiftleft(&Value->Lo,Carry_prm);
		carry_ret=BaseIntT::shiftleft(&Value->Hi,carry_ret);
		return carry_ret;
	}

	template<class BaseIntT> DoubleInt_t<BaseIntT> DoubleInt_t<BaseIntT>::DivideDouble(DoubleInt_t *A,const DoubleInt_t &B)
	{
		DoubleInt_t quotient=*A;
		DoubleInt_t remainder;//==0

		if ((DoubleInt_t)(B)==0) //TODO: fix this const mess
		{
			throw "division by zero";
		}

		for (int x=0;x<A->size;x++)
		{
			int hibit=shiftleft(&quotient,0);
			shiftleft(&remainder,hibit);
			if (remainder>=B)
			{
				SubDouble(&remainder,B,0); //remainder-=*B;
				quotient.Lo|=1; //quotient|=1;
			}
		}

		*A=quotient;
		return remainder;
	}

	template<class BaseIntT> string DoubleInt_t<BaseIntT>::AsString(const char *format)
	{
		string ret;
		switch (format[1])
		{
			case 'd':
			{
				char temp[2];
				DoubleInt_t tmp;
				int x=0;
				tmp=*this;
				if (tmp==0)
				{
					ret="0";
				}
				else
				{
					ret="";
					temp[1]='\0';

					while (tmp!=0)
					{
						DoubleInt_t remainder=DivideDouble(&tmp,DoubleInt_t(10)); //TODO: convert to something a little faster...
						temp[0]=remainder.GetLowByte()+'0';
						ret.insert(0,string(temp));
					}
				}
			}
			break;
			case 'b':
			{
				DoubleInt_t tmp;
				int x=0;
				tmp=*this;
				char temp[tmp.size+2];

				for (int x=0;x<tmp.size;x++)
				{
					temp[x]=tmp.GetLowByte()&0x01+'0';
					shiftright(&tmp,0);
				}
				temp[tmp.size]='\0';
				ret=temp;
			}
			break;
			case 'X':
			case 'x':
			{
				DoubleInt_t tmp;
				tmp=*this;
				char *temp=new char[(tmp.size>>2)+2];
				temp[(tmp.size>>2)]=0;
				for (int x=(tmp.size>>2)-1;x>=0;x--)
				{
					temp[x]=(tmp.Lo).GetLowByte()&0xF;
					if (temp[x]>0x9)
					{
						temp[x]+='A'-10;
					}
					else
					{
						temp[x]+='0';
					}
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
					shiftright(&tmp,0);
				}
				ret=temp;
				delete[] temp;
			}
			break;
		}
		return ret;
	}

	// takes the value as a base 10 or base 16 string and converts it to the big integer type
	template<class BaseIntT> void DoubleInt_t<BaseIntT>::FromString(const char *Source_prm)
	{
		int start=0;
		int base=10;
		Hi=Lo=0;
		while (Source_prm[start]!='\0')
		{
			if (Source_prm[start]=='0')
			{
				if (Source_prm[start+1]=='x')
				{
					base=16;
					start+=2;
				}
				break;
			}
			if ((Source_prm[start]>='0') && (Source_prm[start]<='9'))
			{
				break;
			}
			start++;
		}
		// ok we found the beginning of the string..
		if (base==10)
		{
			DoubleInt_t multconst(10);
			while ((Source_prm[start]<='9') && (Source_prm[start]>='0'))
			{
				char tmp=Source_prm[start]-'0';
				MultiplyDouble(this,multconst);
				AddDouble(this,DoubleInt_t(tmp),0);
				start++;
			}
		}
		else
		{
			char upper=toupper(Source_prm[start]);
			while (((upper<='9') && (upper>='0')) || ((upper>='A') && (upper<='F')))
			{
				char tmp;
				if (upper<='9')
				{
					tmp=upper-'0';
				}
				else
				{
					tmp=upper-'A';
				}
				shiftleft(this,0);
				shiftleft(this,0);
				shiftleft(this,0);
				shiftleft(this,0);
				Lo|=int64(tmp);
				start++;
				upper=toupper(Source_prm[start]);
			}
		}
	}

	template<class BaseIntT> int SignedInt_t<BaseIntT>::AddDouble(SignedInt_t *A,const SignedInt_t &B,const int carry)
	{
		if (carry!=0)
		{
			throw "non zero carry passed to SignedInt add";
		}
		if (A->Negative==B.Negative) //same sign.. just add them, don't modify the sign
		{
			return BaseIntT::AddDouble(&A->Value,B.Value,carry);
		}
		else
		{
			// ok now here is the fun with non two complement numbers

			// first detect which one has the larger magnitude
			if (A->Value>B.Value)
			{
				//A will maintain its sign, but subtract B from A
				if (BaseIntT::SubDouble(&A->Value,B.Value,0)!=0)
				{
					throw "borrow nessisary during add!"; //probably a bug...
				}
				return 0; //there shouldn't be a carry in this case
			}
			else
			{
				if (A->Value==B.Value)
				{
					//rare shortcut....
					A->Value=0;
					A->Negative=0;
					return 0;
				}
				else // (B.Value>A.Value)
				{
					//A will have B's Sign..

					// this is a lot sucky...
					BaseIntT tmp=A->Value;
					A->Value=B.Value;
					if (BaseIntT::SubDouble(&A->Value,tmp,0)!=0)
					{
						throw "borrow nessisary during add!"; //probably a bug...
					}
					A->Negative=B.Negative;
					return 0; //there shouldn't be a carry in this case
				}
			}
		}
	}

	// flip the sign of A and add them...
	template<class BaseIntT> int SignedInt_t<BaseIntT>::SubDouble(SignedInt_t *A,const SignedInt_t &B,const int borrow)
	{
		A->Negative^=1; //negate the sign with xor <chuckle>
		return AddDouble(A,B,0);
	}

	//TODO make a finalizer class which sits between this class and the double template and throws overflow exceptions...
	template<class BaseIntT> SignedInt_t<BaseIntT> SignedInt_t<BaseIntT>::MultiplyDouble(SignedInt_t *A,const SignedInt_t &B)
	{
		BaseIntT::MultiplyDouble(&A->Value,B.Value);
		if (A->Value==0)
		{
			A->Negative=0;
		}
		else
		{
			A->Negative^=B.Negative; //flip sign as nessiary
		}
	}

	template<class BaseIntT> SignedInt_t<BaseIntT> SignedInt_t<BaseIntT>::DivideDouble(SignedInt_t *A,const SignedInt_t &B)
	{
		SignedInt_t tmp;
		tmp.Value=BaseIntT::DivideDouble(&A->Value,B.Value);
		if (A->Value==0)
		{
			A->Negative=0;
		}
		else
		{
			A->Negative^=B.Negative; //flip sign as nessiary
		}
		return tmp.Value;
	}

	template<class BaseIntT> void SignedInt_t<BaseIntT>::FromString(const char *Source_prm)
	{
		int start=0;
		Negative=0;

		while (Source_prm[start]!='\0')
		{
			if (Source_prm[start]=='0')
			{
				if (Source_prm[start+1]=='x')
				{
					// hex start
					Value.FromString(&Source_prm[start]);
					break;
				}
			}
			if ((Source_prm[start]>='0') && (Source_prm[start]<='9'))
			{
				// normal decimal start
				Value.FromString(&Source_prm[start]);
				break;
			}
			if (Source_prm[start]=='-')
			{
				Negative=1;
			}
			start++;
		}
		if (Source_prm[start]=='\0')
		{
			Value=0;
		}
	}
};

typedef Int128Impl::SignedInt_t<Int128Impl::DoubleInt_t<Int128Impl::int128_t> > bigint;

int main(int argc, char *argv[]) {
	std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
	#if DEBUG
	freopen("/tmp/in.txt", "r", stdin);
	while(cin>>n)
	#else
	cin >> n;
	#endif
	{
		map<ll,bigint> bs, b1;
		bigint r = 0;
		bigint nbr = 0;
		bigint ts = 0;
		for(int i=0;i<n;i++) {
			ll x; cin >> x;
			bigint nbr0 = b1[x+1] + b1[x] + b1[x-1];
			bigint s0 = bs[x+1] + bs[x] + bs[x-1];

			r += (nbr - nbr0) * x - (ts-s0);

			bs[x] += x;
			b1[x] += 1;
			ts += x;
			nbr += 1;
		}

		cout << r.AsString("%d") << endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <tuple>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iomanip>
#include <sstream>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T x, T y){ if(x<y) swap(x,y); while(y>0) { T f = x%y; x = y; y = f; } return x; }
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
	if (!b) {d = a; x = 1; y = 0;}
	else {exgcd(b, a % b, d, y, x); y -= x * (a / b);}
}

ll inv(ll a, ll m) {
	ll x, y, d; exgcd(a, m, d, x, y);
	return d == 1 ? ((x%m) + m) % m : -1;
}

/*
 * http://stackoverflow.com/a/25115163
 */
std::ostream& operator<<(std::ostream& os, const __int128 i) noexcept
{
	std::ostream::sentry s(os);
	if (s) {
		unsigned __int128 tmp = i < 0 ? -i : i;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while (tmp != 0);
		if (i < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if (os.rdbuf()->sputn(d, len) != len) {
			os.setstate(std::ios_base::badbit);
		}
	}
	return os;
}

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

const int maxn = 200200;

int n;
int a[maxn];

typedef __int128 bigint;

int main(int argc, char *argv[]) {
	std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
	#if DEBUG
	freopen("/tmp/in.txt", "r", stdin);
	while(cin>>n)
	#else
	cin >> n;
	#endif
	{
		map<ll,bigint> bs, b1;
		bigint r = 0;
		bigint nbr = 0;
		bigint ts = 0;
		for(int i=0;i<n;i++) {
			ll x; cin >> x;
			bigint nbr0 = b1[x+1] + b1[x] + b1[x-1];
			bigint s0 = bs[x+1] + bs[x] + bs[x-1];

			r += (nbr - nbr0) * x - (ts-s0);

			bs[x] += x;
			b1[x] += 1;
			ts += x;
			++nbr;
		}

		cout << r << endl;
	}
	return 0;
}