#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
stack<int> sk[2];
int main() {
  int n, a;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    vec.push_back(a);
  }
  int cnt = 1;
  for (int i = 1; i < n; ++i) {
    if (vec[i] != vec[0]) break;
    cnt++;
  }
  string ans = "YES";
  sk[vec[0]].push(vec[0]);
  for (int i = 1; i < n; ++i) {
    if (vec[i] != vec[i - 1]) {
      if (sk[vec[i - 1]].size() != cnt) {
        ans = "NO";
        break;
      } else {
        while (!sk[vec[i - 1]].empty()) sk[vec[i - 1]].pop();
        sk[vec[i]].push(vec[i]);
      }
    } else {
      sk[vec[i]].push(vec[i]);
    }
  }
  if (sk[vec[n - 1]].size() != cnt) {
    ans = "NO";
  }
  cout << ans << endl;
  return 0;
}
