#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d", &n);
  int arr[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  bool used[n];
  for (int i = 0; i < n; i++) used[i] = false;
  int length_black = 0;
  int length_white = 0;
  bool flag1 = false, flag2 = false;
  ;
  for (int i = 0; i < n; i++) {
    if (used[i]) continue;
    if (arr[i] == 0) {
      length_white = 0;
      for (int j = 0; i + j < n; j++) {
        if (arr[i + j] == 0) {
          used[i + j] = true;
          length_white++;
        } else {
          break;
        }
      }
    } else {
      length_black = 0;
      for (int j = 0; i + j < n; j++) {
        if (arr[i + j] == 1) {
          used[i + j] = true;
          length_black++;
        } else {
          break;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) used[i] = false;
  for (int i = 0; i < n; i++) {
    if (used[i]) continue;
    if (arr[i] == 0) {
      int tmp = 0;
      for (int j = 0; j + i < n; j++) {
        if (arr[i + j] == 0) {
          used[i + j] = true;
          tmp++;
        } else {
          if (tmp != length_white) {
            printf("NO");
            return 0;
          } else {
            break;
          }
        }
      }
    } else {
      int tmp = 0;
      for (int j = 0; j + i < n; j++) {
        if (arr[i + j] == 1) {
          used[i + j] = true;
          tmp++;
        } else {
          if (tmp != length_black) {
            printf("NO");
            return 0;
          } else {
            break;
          }
        }
      }
    }
  }
  if (length_black == length_white || length_black == 0 || length_white == 0)
    printf("YES");
  else
    printf("NO");
  return 0;
}
