#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n, ans, f[1000010], revf[1000010], powt[1000010];
void fix(long long& a) { a = (a % mod + mod) % mod; }
long long qp(long long a, long long b) {
  fix(a);
  long long res = 1;
  while (b) {
    if (b & 1) {
      res *= a;
      fix(res);
    }
    a *= a;
    fix(a);
    b /= 2;
  }
  return res;
}
void fact() {
  f[0] = 1;
  for (int i = 1; i < n + 2; ++i) {
    f[i] = i * f[i - 1];
    fix(f[i]);
  }
  for (int i = 0; i < n + 2; ++i) {
    revf[i] = qp(f[i], mod - 2);
  }
  powt[0] = 1;
  for (int i = 1; i < n + 2; ++i) {
    powt[i] = 3 * powt[i - 1];
    if (powt[i] >= 2 * mod) {
      powt[i] -= 2 * mod;
    } else if (powt[i] >= mod) {
      powt[i] -= mod;
    }
  }
}
long long c(long long a, long long b) {
  long long res = f[a] * revf[a - b];
  fix(res);
  res *= revf[b];
  fix(res);
  return res;
}
long long fix2(long long a) {
  fix(a);
  return a;
}
long long coef[1000010];
void gencoef() {
  for (int i = 0; i <= n; ++i) {
    coef[i] = c(n, i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  fact();
  gencoef();
  long long mone = -1;
  long long tmp;
  for (int i = 0; i < n; ++i) {
    tmp = (qp(1 - powt[i], n) - qp(-powt[i], n));
    tmp *= 3;
    if (tmp >= 2 * mod) {
      tmp -= 2 * mod;
    } else if (tmp >= mod) {
      tmp -= mod;
    }
    ans += coef[i] * mone * tmp;
    fix(ans);
    mone *= -1;
  }
  fix(ans);
  mone = 1;
  for (int i = 1; i <= n; ++i) {
    ans += 2 * coef[i] * mone * qp(3, n * (n - i) + i);
    fix(ans);
    mone *= -1;
  }
  cout << ans << "\n";
}
