#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  unsigned long long a[n];
  pair<int, unsigned long long> b[n];
  unsigned long long temp = 0;
  int count = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    temp = a[i];
    count = 0;
    while (temp % 3 == 0) temp /= 3, ++count;
    b[i] = {count, a[i]};
  }
  auto comp = [=](pair<int, unsigned long long> a,
                  pair<int, unsigned long long> b) {
    if (a.first > b.first)
      return true;
    else if (a.first == b.first)
      if (a.second < b.second) return true;
    return false;
  };
  sort(b, b + n, comp);
  for (int i = 0; i < n; i++) {
    cout << b[i].second << " ";
  }
  cout << "\n";
}
