#include <bits/stdc++.h>
int n;
int c;
int main() {
  scanf("%d", &n);
  int cb = 0, cc = 0;
  int u = -1;
  int i;
  int bil1 = 0, bil2 = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &c);
    if (c == 1) {
      bil1 = 1;
      if (u != cb && cb != 0 && u != -1) {
        puts("NO");
        return 0;
      }
      if (u == -1 && bil2 == 1) u = cb;
      cc++;
      cb = 0;
    } else if (c == 0) {
      bil2 = 1;
      if (u != cc && cc != 0 && u != -1) {
        puts("NO");
        return 0;
      }
      if (u == -1 && bil1 == 1) u = cc;
      cb++;
      cc = 0;
    }
  }
  if (u == -1) {
    puts("YES");
    return 0;
  }
  if ((u != cb && cb != 0) || (u != cc && cc != 0)) {
    puts("NO");
    return 0;
  }
  puts("YES");
  return 0;
}
