#include <bits/stdc++.h>
int main() {
  int n, i, k = 1, k1, k2, f, acc;
  scanf("%d", &n);
  int lst[n];
  for (i = 0; i < n; i++) {
    scanf("%d", &lst[i]);
  }
  int array[n];
  for (i = 0; i < n; i++) {
    if (lst[i] != lst[i - 1]) k = 1;
    if (lst[i] == lst[i - 1]) k = k + 1;
    array[i] = k;
  }
  acc = 0;
  k1 = 0;
  k2 = 0;
  f = 0;
  for (i = 1; i < n; i++) {
    if (array[i] == 1) {
      if (acc == 0) {
        k1 = array[i - 1];
        k2 = array[i - 1];
        acc = acc + 1;
      }
      if (k != 0) {
        k2 = k1;
        k2 = array[i - 1];
      }
      if (k1 != k2) f = 1;
    }
  }
  if (acc == 0) {
    k1 = array[i - 1];
    k2 = array[i - 1];
    acc = acc + 1;
  }
  if (k != 0) {
    k2 = k1;
    k2 = array[i - 1];
  }
  if (k1 != k2) f = 1;
  if (f == 1)
    printf("%s", "NO");
  else
    printf("%s", "YES");
  return 0;
}
