#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, w = 0, b = 0;
  cin >> n;
  vector<int> vec;
  bool white = false, black = false;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a == 1) {
      black = true;
      if (white) {
        white = false;
        vec.push_back(w);
        w = 0;
      }
      b++;
    } else {
      white = true;
      if (black) {
        black = false;
        vec.push_back(b);
        b = 0;
      }
      w++;
    }
  }
  vec.push_back(max(w, b));
  if (vec.size() == 1)
    cout << "YES" << endl;
  else {
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i] != vec[i - 1]) {
        cout << "NO" << endl;
        return 0;
      }
    }
    cout << "YES" << endl;
  }
  return 0;
}
