#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  long long ar[n];
  for (long long i = 0; i <= n - 1; ++i) cin >> ar[i];
  long long cnt = 1;
  set<long long> one, zero;
  for (long long i = 1; i <= n - 1; ++i) {
    if (ar[i] == ar[i - 1]) {
      cnt++;
    } else {
      if (ar[i] == 1)
        zero.insert(cnt);
      else
        one.insert(cnt);
      cnt = 1;
    }
  }
  if (ar[n - 1] == 1)
    one.insert(cnt);
  else
    zero.insert(cnt);
  if (one.size() == 0 || zero.size() == 0)
    cout << "YES";
  else if (one.size() > 1 || zero.size() > 1)
    cout << "NO";
  else if (*one.begin() == *zero.begin())
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
