#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
const int maxn = (int)1e6 + 13;
const long long INFL = 9223372036854775807;
const int INF = 2147483647;
const long long MOD = (int)1e9 + 87;
const long long b = 101;
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s(n, '0');
  for (auto &t : s) cin >> t;
  int cnt1 = 0, cnt0 = 0;
  int i = 0;
  if (s[0] == '1') {
    while (i < n && s[i] == '1') {
      cnt1++;
      ++i;
    }
    while (i < n && s[i] == '0') {
      cnt0++;
      ++i;
    }
  } else {
    while (i < n && s[i] == '0') {
      cnt0++;
      ++i;
    }
    while (i < n && s[i] == '1') {
      cnt1++;
      ++i;
    }
  }
  cnt0 = max(cnt0, cnt1);
  for (int j = 0; j < n; ++j) {
    int cnt = 0;
    bool f = 0;
    while (j < n && s[j] == '0') cnt++, ++j, f = 1;
    if (cnt != cnt0 && f) {
      cout << "NO\n";
      return 0;
    }
  }
  for (int j = 0; j < n; ++j) {
    int cnt = 0;
    bool f = 0;
    while (j < n && s[j] == '1') cnt++, ++j, f = 1;
    if (cnt != cnt0 && f) {
      cout << "NO\n";
      return 0;
    }
  }
  cout << "YES\n";
  return 0;
}
