#include <bits/stdc++.h>
using namespace std;
int main() {
  string pixels;
  getline(cin, pixels);
  getline(cin, pixels);
  int len = 0, flen = 0, last = pixels.at(0) - '0';
  bool first = false;
  for (int i = (0); i < (pixels.size()); i += (2)) {
    if ((pixels.at(i) == '0' && last == 0) ||
        (pixels.at(i) == '1' && last == 1))
      len++;
    else {
      if (!first) {
        flen = len;
        first = true;
      } else if (flen != len) {
        printf("NO");
        return 0;
      }
      len = 1;
      last = 1 - last;
    }
  }
  if (!flen || flen == len)
    printf("YES");
  else
    printf("NO");
  return 0;
}
