import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.string;
import std.regex;

void main() {
	int n, x;
	int ans = 1;
	int a=0, b=0;
	int c=0, d=-1;
	scanf("%d", &n );
	for( int i = 1; i <= n; i ++ )
	{
	    scanf( "%d", &x );
	    if( d == -1 )
	    {
	        d = x;
	    }
	   if( d == x )
	   {
	        c ++;
	   }
	   else 
	   {
	    if( x == 1 )
	    {
	        if( a == 0 ) 
	            a = c;
	       if( a != c ) 
	            ans = 0;
	    }
	    else 
	    {
	        if( b == 0 )
	            b = c;
	        if( b != c )
	            ans = 0;
	    }
	    d = x;
	    c = 1;
	    }
  }
	if( d == 0 )
	{
    	if( a == 0 )
    	   a = c;
    	   if( a != c ) 
    	        ans = 0;
	}
	else 
    {
        if( b == 0 )
            b = c;
	    if( b != c )
	        ans = 0;
	}
	if( a == 0 )
	    a = b;
	if( b == 0 )
	    b = a;
	if( a != b )
	    ans = 0;
	if( ans == 1 )
	    printf( "YES" );
	else 
	    printf( "NO" );
}