#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int a[n], b = 0, w = 0;
  vector<long long int> v1, v2;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      if (a[i] == 1)
        b++;
      else
        w++;
    } else {
      if (a[i] == 1) {
        if (a[i - 1] == 1)
          b++;
        else {
          v1.push_back(w);
          w = 0;
          b = 1;
        }
      } else {
        if (a[i - 1] == 0)
          w++;
        else {
          v2.push_back(b);
          b = 0;
          w = 1;
        }
      }
    }
  }
  if (b != 0)
    v2.push_back(b);
  else
    v1.push_back(w);
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  long long int p = v1.size(), q = v2.size();
  if (p == 0 || q == 0 ||
      (v1[0] == v1[p - 1] && v2[0] == v2[q - 1] && v1[0] == v2[0]))
    cout << "YES\n";
  else
    cout << "NO\n";
}
